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 eab5e66512b..648df4b43aa 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 @@ -30,6 +30,7 @@ #import #import #import +#import #import #import #import @@ -300,7 +301,7 @@ static Class getFallbackClassFromName(const char *name) * (for now). */ -- (std::shared_ptr)provideTurboModule:(const char *)moduleName +- (std::shared_ptr)provideTurboModule:(const char *)moduleName runtime:(jsi::Runtime *)runtime { auto turboModuleLookup = _turboModuleCache.find(moduleName); if (turboModuleLookup != _turboModuleCache.end()) { @@ -404,6 +405,10 @@ static Class getFallbackClassFromName(const char *name) RCTLogError(@"TurboModule \"%@\"'s getTurboModule: method returned nil.", moduleClass); } _turboModuleCache.insert({moduleName, turboModule}); + + if ([module respondsToSelector:@selector(installJSIBindingsWithRuntime:)]) { + [(id)module installJSIBindingsWithRuntime:*runtime]; + } return turboModule; } @@ -766,7 +771,7 @@ static Class getFallbackClassFromName(const char *name) * Attach method queue to id object. * This is necessary because the id object can be eagerly created/initialized before the method * queue is required. The method queue is required for an id for JS -> Native calls. So, we need it - * before we create the id's TurboModule jsi::HostObject in provideTurboModule:. + * before we create the id's TurboModule jsi::HostObject in provideTurboModule:runtime:. */ objc_setAssociatedObject(module, &kAssociatedMethodQueueKey, methodQueue, OBJC_ASSOCIATION_RETAIN); @@ -916,7 +921,8 @@ static Class getFallbackClassFromName(const char *name) * 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); @@ -930,7 +936,7 @@ static Class getFallbackClassFromName(const char *name) * 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 runtime:runtime]; if (moduleWasNotInitialized && [self moduleIsInitialized:moduleName]) { [self->_bridge.performanceLogger markStopForTag:RCTPLTurboModuleSetup]; diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleWithJSIBindings.h b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleWithJSIBindings.h new file mode 100644 index 00000000000..44c8cfceb8b --- /dev/null +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleWithJSIBindings.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +#ifdef __cplusplus +#include +#endif + +@protocol RCTTurboModuleWithJSIBindings + +#ifdef __cplusplus +- (void)installJSIBindingsWithRuntime:(facebook::jsi::Runtime &)runtime; +#endif + +@end diff --git a/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleTurboModule.mm b/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleTurboModule.mm index 1c7b70a7f30..c7d9666a390 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleTurboModule.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleTurboModule.mm @@ -10,10 +10,14 @@ #import #import +#import #import using namespace facebook::react; +@interface RCTSampleTurboModule () +@end + @implementation RCTSampleTurboModule // Backward-compatible export @@ -66,6 +70,15 @@ RCT_EXPORT_MODULE() return [self getConstants]; } +#pragma mark - RCTTurboModuleWithJSIBindings + +- (void)installJSIBindingsWithRuntime:(facebook::jsi::Runtime &)runtime +{ + runtime.global().setProperty(runtime, "__SampleTurboModuleJSIBindings", "Hello JSI!"); +} + +#pragma mark - Spec Methods + RCT_EXPORT_METHOD(voidFunc) { // Nothing to do