From 90296be1d4fab09a52e02dd09f34f819136d0a07 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Mon, 8 Apr 2024 05:21:27 -0700 Subject: [PATCH] Support launchOptions in bridgeless mode (#43757) Summary: Support launchOptions in bridgeless mode bypass-github-export-checks ## Changelog: [IOS] [FIXED] - Support launchOptions in bridgeless mode Pull Request resolved: https://github.com/facebook/react-native/pull/43757 Test Plan: ``` useEffect(() => { const processInitialURL = async () => { const url = await Linking.getInitialURL(); if (url !== null) { console.log(`Initial url is: ${url}`); } }; processInitialURL(); }, []); ``` Reviewed By: javache Differential Revision: D55790758 Pulled By: cipolleschi fbshipit-source-id: 0f6aa6bdcebfc5bc42d632bea9193f122c1eb84f --- .../Libraries/AppDelegate/RCTRootViewFactory.mm | 7 ++++--- packages/react-native/React/Base/RCTBridgeProxy.h | 7 ++++++- packages/react-native/React/Base/RCTBridgeProxy.mm | 6 ++++-- .../platform/ios/ReactCommon/RCTTurboModuleManager.mm | 2 +- .../ReactCommon/react/runtime/iostests/RCTHostTests.mm | 3 ++- .../react/runtime/platform/ios/ReactCommon/RCTHost.h | 3 ++- .../react/runtime/platform/ios/ReactCommon/RCTHost.mm | 10 ++++++++-- .../runtime/platform/ios/ReactCommon/RCTInstance.h | 3 ++- .../runtime/platform/ios/ReactCommon/RCTInstance.mm | 6 +++++- .../react/test_utils/ios/Shims/ShimRCTInstance.h | 2 +- .../react/test_utils/ios/Shims/ShimRCTInstance.mm | 5 +++-- 11 files changed, 38 insertions(+), 16 deletions(-) diff --git a/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm b/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm index 30aa86f91c0..f8acdcd8aed 100644 --- a/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm +++ b/packages/react-native/Libraries/AppDelegate/RCTRootViewFactory.mm @@ -123,7 +123,7 @@ static NSDictionary *updateInitialProps(NSDictionary *initialProps, BOOL isFabri RCTEnableTurboModuleInterop(YES); RCTEnableTurboModuleInteropBridgeProxy(YES); - [self createReactHostIfNeeded]; + [self createReactHostIfNeeded:launchOptions]; RCTFabricSurface *surface = [_reactHost createSurfaceWithModuleName:moduleName initialProperties:initProps]; @@ -207,7 +207,7 @@ static NSDictionary *updateInitialProps(NSDictionary *initialProps, BOOL isFabri #pragma mark - New Arch Utilities -- (void)createReactHostIfNeeded +- (void)createReactHostIfNeeded:(NSDictionary *)launchOptions { if (_reactHost) { return; @@ -219,7 +219,8 @@ static NSDictionary *updateInitialProps(NSDictionary *initialProps, BOOL isFabri turboModuleManagerDelegate:_turboModuleManagerDelegate jsEngineProvider:^std::shared_ptr() { return [weakSelf createJSRuntimeFactory]; - }]; + } + launchOptions:launchOptions]; [_reactHost setBundleURLProvider:^NSURL *() { return [weakSelf bundleURL]; }]; diff --git a/packages/react-native/React/Base/RCTBridgeProxy.h b/packages/react-native/React/Base/RCTBridgeProxy.h index 3a6de36473e..71bb9cf0831 100644 --- a/packages/react-native/React/Base/RCTBridgeProxy.h +++ b/packages/react-native/React/Base/RCTBridgeProxy.h @@ -9,6 +9,8 @@ #import "RCTBridgeModule.h" +NS_ASSUME_NONNULL_BEGIN + @class RCTBundleManager; @class RCTCallableJSModules; @class RCTModuleRegistry; @@ -22,7 +24,8 @@ callableJSModules:(RCTCallableJSModules *)callableJSModules dispatchToJSThread:(void (^)(dispatch_block_t))dispatchToJSThread registerSegmentWithId:(void (^)(NSNumber *, NSString *))registerSegmentWithId - runtime:(void *)runtime NS_DESIGNATED_INITIALIZER; + runtime:(void *)runtime + launchOptions:(nullable NSDictionary *)launchOptions NS_DESIGNATED_INITIALIZER; - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel; - (void)forwardInvocation:(NSInvocation *)invocation; @@ -37,3 +40,5 @@ - (id)moduleForName:(NSString *)moduleName lazilyLoadIfNecessary:(BOOL)lazilyLoad; @end + +NS_ASSUME_NONNULL_END diff --git a/packages/react-native/React/Base/RCTBridgeProxy.mm b/packages/react-native/React/Base/RCTBridgeProxy.mm index 27637daf654..5b3e0656929 100644 --- a/packages/react-native/React/Base/RCTBridgeProxy.mm +++ b/packages/react-native/React/Base/RCTBridgeProxy.mm @@ -35,6 +35,7 @@ using namespace facebook; RCTModuleRegistry *_moduleRegistry; RCTBundleManager *_bundleManager; RCTCallableJSModules *_callableJSModules; + NSDictionary *_launchOptions; void (^_dispatchToJSThread)(dispatch_block_t); void (^_registerSegmentWithId)(NSNumber *, NSString *); void *_runtime; @@ -47,6 +48,7 @@ using namespace facebook; dispatchToJSThread:(void (^)(dispatch_block_t))dispatchToJSThread registerSegmentWithId:(void (^)(NSNumber *, NSString *))registerSegmentWithId runtime:(void *)runtime + launchOptions:(nullable NSDictionary *)launchOptions { self = [super self]; if (self) { @@ -57,6 +59,7 @@ using namespace facebook; _dispatchToJSThread = dispatchToJSThread; _registerSegmentWithId = registerSegmentWithId; _runtime = runtime; + _launchOptions = [launchOptions copy]; } return self; } @@ -191,8 +194,7 @@ using namespace facebook; - (NSDictionary *)launchOptions { - [self logError:@"This method is not supported. Returning nil." cmd:_cmd]; - return nil; + return _launchOptions; } - (BOOL)loading diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm index d93d606fce4..9a34ab1ab7d 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm @@ -674,7 +674,7 @@ static Class getFallbackClassFromName(const char *name) */ if (_bridge) { [(id)module setValue:_bridge forKey:@"bridge"]; - } else if (_bridgeProxy && [self _isLegacyModuleClass:[module class]]) { + } else if (_bridgeProxy) { [(id)module setValue:_bridgeProxy forKey:@"bridge"]; } } @catch (NSException *exception) { diff --git a/packages/react-native/ReactCommon/react/runtime/iostests/RCTHostTests.mm b/packages/react-native/ReactCommon/react/runtime/iostests/RCTHostTests.mm index de2e016b95a..cadf6d61cdc 100644 --- a/packages/react-native/ReactCommon/react/runtime/iostests/RCTHostTests.mm +++ b/packages/react-native/ReactCommon/react/runtime/iostests/RCTHostTests.mm @@ -58,7 +58,8 @@ static ShimRCTInstance *shimmedRCTInstance; turboModuleManagerDelegate:OCMProtocolMock(@protocol(RCTTurboModuleManagerDelegate)) jsEngineProvider:^std::shared_ptr() { return std::make_shared(); - }]; + } + launchOptions:nil]; } - (void)tearDown diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.h b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.h index ea4492980f9..8f3c0460f38 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.h +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.h @@ -48,7 +48,8 @@ typedef std::shared_ptr (^RCTHostJSEngineProv - (instancetype)initWithBundleURL:(NSURL *)bundleURL hostDelegate:(id)hostDelegate turboModuleManagerDelegate:(id)turboModuleManagerDelegate - jsEngineProvider:(RCTHostJSEngineProvider)jsEngineProvider NS_DESIGNATED_INITIALIZER; + jsEngineProvider:(RCTHostJSEngineProvider)jsEngineProvider + launchOptions:(nullable NSDictionary *)launchOptions NS_DESIGNATED_INITIALIZER; @property (nonatomic, weak, nullable) id runtimeDelegate; diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm index 975906420bf..c5d9ae26b50 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm @@ -58,6 +58,8 @@ class RCTHostHostTargetDelegate : public facebook::react::jsinspector_modern::Ho RCTHostBundleURLProvider _bundleURLProvider; RCTHostJSEngineProvider _jsEngineProvider; + NSDictionary *_launchOptions; + // All the surfaces that need to be started after main bundle execution NSMutableArray *_surfaceStartBuffer; std::mutex _surfaceStartBufferMutex; @@ -85,6 +87,7 @@ class RCTHostHostTargetDelegate : public facebook::react::jsinspector_modern::Ho hostDelegate:(id)hostDelegate turboModuleManagerDelegate:(id)turboModuleManagerDelegate jsEngineProvider:(RCTHostJSEngineProvider)jsEngineProvider + launchOptions:(nullable NSDictionary *)launchOptions { if (self = [super init]) { _hostDelegate = hostDelegate; @@ -93,6 +96,7 @@ class RCTHostHostTargetDelegate : public facebook::react::jsinspector_modern::Ho _bundleManager = [RCTBundleManager new]; _moduleRegistry = [RCTModuleRegistry new]; _jsEngineProvider = [jsEngineProvider copy]; + _launchOptions = [launchOptions copy]; __weak RCTHost *weakSelf = self; @@ -204,7 +208,8 @@ class RCTHostHostTargetDelegate : public facebook::react::jsinspector_modern::Ho turboModuleManagerDelegate:_turboModuleManagerDelegate onInitialBundleLoad:_onInitialBundleLoad moduleRegistry:_moduleRegistry - parentInspectorTarget:_inspectorTarget.get()]; + parentInspectorTarget:_inspectorTarget.get() + launchOptions:_launchOptions]; [_hostDelegate hostDidStart:self]; } @@ -284,7 +289,8 @@ class RCTHostHostTargetDelegate : public facebook::react::jsinspector_modern::Ho turboModuleManagerDelegate:_turboModuleManagerDelegate onInitialBundleLoad:_onInitialBundleLoad moduleRegistry:_moduleRegistry - parentInspectorTarget:_inspectorTarget.get()]; + parentInspectorTarget:_inspectorTarget.get() + launchOptions:_launchOptions]; [_hostDelegate hostDidStart:self]; for (RCTFabricSurface *surface in [self _getAttachedSurfaces]) { diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.h b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.h index 90d6911ce8b..e66e2b2bb37 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.h +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.h @@ -62,7 +62,8 @@ typedef void (^_Null_unspecified RCTInstanceInitialBundleLoadCompletionBlock)(); turboModuleManagerDelegate:(id)turboModuleManagerDelegate onInitialBundleLoad:(RCTInstanceInitialBundleLoadCompletionBlock)onInitialBundleLoad moduleRegistry:(RCTModuleRegistry *)moduleRegistry - parentInspectorTarget:(facebook::react::jsinspector_modern::HostTarget *)parentInspectorTarget; + parentInspectorTarget:(facebook::react::jsinspector_modern::HostTarget *)parentInspectorTarget + launchOptions:(nullable NSDictionary *)launchOptions; - (void)callFunctionOnJSModule:(NSString *)moduleName method:(NSString *)method args:(NSArray *)args; diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm index 489ac30f524..deaf2124c91 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm @@ -84,6 +84,7 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags) std::mutex _invalidationMutex; std::atomic _valid; RCTJSThreadManager *_jsThreadManager; + NSDictionary *_launchOptions; // APIs supporting interop with native modules and view managers RCTBridgeModuleDecorator *_bridgeModuleDecorator; @@ -100,6 +101,7 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags) onInitialBundleLoad:(RCTInstanceInitialBundleLoadCompletionBlock)onInitialBundleLoad moduleRegistry:(RCTModuleRegistry *)moduleRegistry parentInspectorTarget:(jsinspector_modern::HostTarget *)parentInspectorTarget + launchOptions:(nullable NSDictionary *)launchOptions { if (self = [super init]) { _performanceLogger = [RCTPerformanceLogger new]; @@ -125,6 +127,7 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags) [weakSelf callFunctionOnJSModule:moduleName method:methodName args:args]; }]; } + _launchOptions = launchOptions; NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; @@ -277,7 +280,8 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags) [strongSelf registerSegmentWithId:segmentId path:path]; } } - runtime:_reactInstance->getJavaScriptContext()]; + runtime:_reactInstance->getJavaScriptContext() + launchOptions:_launchOptions]; bridgeProxy.jsCallInvoker = jsCallInvoker; [RCTBridge setCurrentBridge:(RCTBridge *)bridgeProxy]; diff --git a/packages/react-native/ReactCommon/react/test_utils/ios/Shims/ShimRCTInstance.h b/packages/react-native/ReactCommon/react/test_utils/ios/Shims/ShimRCTInstance.h index e11e88c560a..654cf17657c 100644 --- a/packages/react-native/ReactCommon/react/test_utils/ios/Shims/ShimRCTInstance.h +++ b/packages/react-native/ReactCommon/react/test_utils/ios/Shims/ShimRCTInstance.h @@ -11,7 +11,7 @@ @property int initCount; @property int invalidateCount; - +@property NSDictionary *launchOptions; @property NSString *jsModuleName; @property NSString *method; @property NSArray *args; diff --git a/packages/react-native/ReactCommon/react/test_utils/ios/Shims/ShimRCTInstance.mm b/packages/react-native/ReactCommon/react/test_utils/ios/Shims/ShimRCTInstance.mm index 78d2e9c2a76..2575f1453a2 100644 --- a/packages/react-native/ReactCommon/react/test_utils/ios/Shims/ShimRCTInstance.mm +++ b/packages/react-native/ReactCommon/react/test_utils/ios/Shims/ShimRCTInstance.mm @@ -24,7 +24,7 @@ static __weak ShimRCTInstance *weakShim = nil; [ShimRCTInstance class], @selector(initWithDelegate: jsRuntimeFactory:bundleManager:turboModuleManagerDelegate:onInitialBundleLoad:moduleRegistry - :parentInspectorTarget:)); + :parentInspectorTarget:launchOptions:)); RCTSwizzleInstanceSelector([RCTInstance class], [ShimRCTInstance class], @selector(invalidate)); RCTSwizzleInstanceSelector( [RCTInstance class], [ShimRCTInstance class], @selector(callFunctionOnJSModule:method:args:)); @@ -40,7 +40,7 @@ static __weak ShimRCTInstance *weakShim = nil; [ShimRCTInstance class], @selector(initWithDelegate: jsRuntimeFactory:bundleManager:turboModuleManagerDelegate:onInitialBundleLoad:moduleRegistry - :parentInspectorTarget:)); + :parentInspectorTarget:launchOptions:)); RCTSwizzleInstanceSelector([RCTInstance class], [ShimRCTInstance class], @selector(invalidate)); RCTSwizzleInstanceSelector( [RCTInstance class], [ShimRCTInstance class], @selector(callFunctionOnJSModule:method:args:)); @@ -55,6 +55,7 @@ static __weak ShimRCTInstance *weakShim = nil; onInitialBundleLoad:(RCTInstanceInitialBundleLoadCompletionBlock)onInitialBundleLoad moduleRegistry:(RCTModuleRegistry *)moduleRegistry parentInspectorTarget:(facebook::react::jsinspector_modern::HostTarget *)parentInspectorTarget + launchOptions:(NSDictionary *)launchOptions { weakShim.initCount++; return self;