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 e092136899db4c..be37541355ba76 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 @@ -188,7 +188,6 @@ static Class getFallbackClassFromName(const char *name) @implementation RCTTurboModuleManager { std::shared_ptr _jsInvoker; - facebook::jsi::Runtime *_runtime; __weak id _delegate; __weak RCTBridge *_bridge; @@ -308,6 +307,7 @@ - (instancetype)initWithBridgeProxy:(RCTBridgeProxy *)bridgeProxy */ - (std::shared_ptr)provideTurboModule:(const char *)moduleName + withRuntime:(facebook::jsi::Runtime *)runtime { auto turboModuleLookup = _turboModuleCache.find(moduleName); if (turboModuleLookup != _turboModuleCache.end()) { @@ -411,6 +411,14 @@ - (instancetype)initWithBridgeProxy:(RCTBridgeProxy *)bridgeProxy RCTLogError(@"TurboModule \"%@\"'s getTurboModule: method returned nil.", moduleClass); } _turboModuleCache.insert({moduleName, turboModule}); + + if ([module respondsToSelector:@selector(createBindingsInstaller)]) { + RCTTurboModuleBindingsInstaller *installer = + (RCTTurboModuleBindingsInstaller *)[module performSelector:@selector(createBindingsInstaller)]; + if (installer != nil) { + installer.get(*runtime); + } + } return turboModule; } @@ -707,14 +715,6 @@ - (BOOL)_shouldCreateObjCModule:(Class)moduleClass [(id)module setCallInvoker:callInvoker]; } - if ([module respondsToSelector:@selector(createBindingsInstaller)]) { - RCTTurboModuleBindingsInstaller *installer = - (RCTTurboModuleBindingsInstaller *)[module performSelector:@selector(createBindingsInstaller)]; - if (installer != nil && _runtime) { - installer.get(*_runtime); - } - } - /** * Some modules need their own queues, but don't provide any, so we need to create it for them. * These modules typically have the following: @@ -924,8 +924,6 @@ - (BOOL)_requiresMainQueueSetup:(Class)moduleClass - (void)installJSBindings:(facebook::jsi::Runtime &)runtime { - _runtime = &runtime; - /** * We keep TurboModuleManager alive until the JS VM is deleted. * It is perfectly valid to only use/create TurboModules from JS. @@ -933,7 +931,8 @@ - (void)installJSBindings:(facebook::jsi::Runtime &)runtime * aren't any strong references to it in ObjC. Hence, we give * __turboModuleProxy a strong reference to TurboModuleManager. */ - auto turboModuleProvider = [self](const std::string &name) -> std::shared_ptr { + auto turboModuleProvider = [self, + runtime = &runtime](const std::string &name) -> std::shared_ptr { auto moduleName = name.c_str(); TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName); @@ -947,7 +946,7 @@ - (void)installJSBindings:(facebook::jsi::Runtime &)runtime * Additionally, if a TurboModule with the name `name` isn't found, then we * trigger an assertion failure. */ - auto turboModule = [self provideTurboModule:moduleName]; + auto turboModule = [self provideTurboModule:moduleName withRuntime:runtime]; if (moduleWasNotInitialized && [self moduleIsInitialized:moduleName]) { [self->_bridge.performanceLogger markStopForTag:RCTPLTurboModuleSetup]; @@ -1038,14 +1037,12 @@ - (void)bridgeDidInvalidateModules:(NSNotification *)notification } [self _invalidateModules]; - _runtime = nullptr; } - (void)invalidate { [self _enterInvalidatingState]; [self _invalidateModules]; - _runtime = nullptr; } - (void)_enterInvalidatingState