diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 196cbcf3c1c1e6..2a0ba29bfdd8ae 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -354,7 +354,6 @@ public abstract class com/facebook/react/ReactPackageTurboModuleManagerDelegate public fun getModule (Ljava/lang/String;)Lcom/facebook/react/turbomodule/core/interfaces/TurboModule; public fun unstable_isLegacyModuleRegistered (Ljava/lang/String;)Z public fun unstable_isModuleRegistered (Ljava/lang/String;)Z - public fun unstable_shouldEnableLegacyModuleInterop ()Z } public abstract class com/facebook/react/ReactPackageTurboModuleManagerDelegate$Builder { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageTurboModuleManagerDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageTurboModuleManagerDelegate.kt index 75020f92207ba6..749c942f7c45d2 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageTurboModuleManagerDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageTurboModuleManagerDelegate.kt @@ -105,8 +105,6 @@ public abstract class ReactPackageTurboModuleManagerDelegate : TurboModuleManage } } - override fun unstable_shouldEnableLegacyModuleInterop(): Boolean = shouldEnableLegacyModuleInterop - override fun getModule(moduleName: String): TurboModule? { var resolvedModule: NativeModule? = null @@ -153,7 +151,7 @@ public abstract class ReactPackageTurboModuleManagerDelegate : TurboModuleManage } override fun getLegacyModule(moduleName: String): NativeModule? { - if (!unstable_shouldEnableLegacyModuleInterop()) { + if (!shouldEnableLegacyModuleInterop) { return null } @@ -191,7 +189,7 @@ public abstract class ReactPackageTurboModuleManagerDelegate : TurboModuleManage } } - private fun shouldSupportLegacyPackages(): Boolean = unstable_shouldEnableLegacyModuleInterop() + private fun shouldSupportLegacyPackages(): Boolean = shouldEnableLegacyModuleInterop public abstract class Builder { private var packages: List? = null diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/turbomodule/core/TurboModuleManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/turbomodule/core/TurboModuleManager.kt index 9581196b9f4b4d..5a55e7c8050670 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/turbomodule/core/TurboModuleManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/turbomodule/core/TurboModuleManager.kt @@ -52,7 +52,6 @@ public class TurboModuleManager( @DoNotStrip private val mHybridData: HybridData = initHybrid( - runtimeExecutor, jsCallInvokerHolder as CallInvokerHolderImpl, nativeMethodCallInvokerHolder as NativeMethodCallInvokerHolderImpl, delegate, @@ -60,7 +59,7 @@ public class TurboModuleManager( init { - installJSIBindings(shouldEnableLegacyModuleInterop()) + dispatchJSBindingInstall(runtimeExecutor) eagerInitModuleNames = delegate?.getEagerInitModuleNames() ?: emptyList() @@ -72,7 +71,7 @@ public class TurboModuleManager( ModuleProvider { moduleName: String -> delegate.getModule(moduleName) as NativeModule? } legacyModuleProvider = - if (delegate == null || !shouldEnableLegacyModuleInterop()) nullProvider + if (delegate == null) nullProvider else ModuleProvider { moduleName: String -> val nativeModule = delegate.getLegacyModule(moduleName) @@ -93,9 +92,6 @@ public class TurboModuleManager( private fun isLegacyModule(moduleName: String): Boolean = delegate?.unstable_isLegacyModuleRegistered(moduleName) == true - private fun shouldEnableLegacyModuleInterop(): Boolean = - delegate?.unstable_shouldEnableLegacyModuleInterop() == true - // used from TurboModuleManager.cpp @Suppress("unused") @DoNotStrip @@ -281,13 +277,12 @@ public class TurboModuleManager( } private external fun initHybrid( - runtimeExecutor: RuntimeExecutor, jsCallInvokerHolder: CallInvokerHolderImpl, nativeMethodCallInvoker: NativeMethodCallInvokerHolderImpl, tmmDelegate: TurboModuleManagerDelegate?, ): HybridData - private external fun installJSIBindings(shouldCreateLegacyModules: Boolean) + private external fun dispatchJSBindingInstall(runtimeExecutor: RuntimeExecutor) override fun invalidate() { /* diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/turbomodule/core/TurboModuleManagerDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/turbomodule/core/TurboModuleManagerDelegate.kt index 202e8aecb60790..a8713ea339a8ed 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/turbomodule/core/TurboModuleManagerDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/turbomodule/core/TurboModuleManagerDelegate.kt @@ -48,9 +48,6 @@ public abstract class TurboModuleManagerDelegate { public open fun getEagerInitModuleNames(): List = emptyList() - /** Can the TurboModule system create legacy modules? */ - public open fun unstable_shouldEnableLegacyModuleInterop(): Boolean = false - // TODO(T171231381): Consider removing this method: could we just use the static initializer // of derived classes instead? @Synchronized protected fun maybeLoadOtherSoLibraries(): Unit = Unit diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp index f0c0fbbbaea72c..da32c0bfca4ed8 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include namespace facebook::react { @@ -94,24 +95,20 @@ class JMethodDescriptor : public jni::JavaClass { } // namespace TurboModuleManager::TurboModuleManager( - RuntimeExecutor runtimeExecutor, std::shared_ptr jsCallInvoker, std::shared_ptr nativeMethodCallInvoker, jni::alias_ref delegate) - : runtimeExecutor_(std::move(runtimeExecutor)), - jsCallInvoker_(std::move(jsCallInvoker)), + : jsCallInvoker_(std::move(jsCallInvoker)), nativeMethodCallInvoker_(std::move(nativeMethodCallInvoker)), delegate_(jni::make_global(delegate)) {} jni::local_ref TurboModuleManager::initHybrid( jni::alias_ref /* unused */, - jni::alias_ref runtimeExecutor, jni::alias_ref jsCallInvokerHolder, jni::alias_ref nativeMethodCallInvokerHolder, jni::alias_ref delegate) { return makeCxxInstance( - runtimeExecutor->cthis()->get(), jsCallInvokerHolder->cthis()->getCallInvoker(), nativeMethodCallInvokerHolder->cthis()->getNativeMethodCallInvoker(), delegate); @@ -121,14 +118,16 @@ void TurboModuleManager::registerNatives() { registerHybrid({ makeNativeMethod("initHybrid", TurboModuleManager::initHybrid), makeNativeMethod( - "installJSIBindings", TurboModuleManager::installJSIBindings), + "dispatchJSBindingInstall", + TurboModuleManager::dispatchJSBindingInstall), }); } -TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider( - jni::alias_ref javaPart, - jsi::Runtime* runtime) { - return [runtime, weakJavaPart = jni::make_weak(javaPart)]( +TurboModuleProviderFunctionTypeWithRuntime +TurboModuleManager::createTurboModuleProvider( + jni::alias_ref javaPart) { + return [weakJavaPart = jni::make_weak(javaPart)]( + jsi::Runtime& runtime, const std::string& name) -> std::shared_ptr { auto javaPart = weakJavaPart.lockLocal(); if (!javaPart) { @@ -140,7 +139,7 @@ TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider( return nullptr; } - return cxxPart->getTurboModule(javaPart, name, *runtime); + return cxxPart->getTurboModule(javaPart, name, runtime); }; } @@ -223,9 +222,19 @@ std::shared_ptr TurboModuleManager::getTurboModule( return nullptr; } -TurboModuleProviderFunctionType TurboModuleManager::createLegacyModuleProvider( +TurboModuleProviderFunctionTypeWithRuntime +TurboModuleManager::createLegacyModuleProvider( jni::alias_ref javaPart) { + bool shouldCreateLegacyModules = + ReactNativeFeatureFlags::enableBridgelessArchitecture() && + ReactNativeFeatureFlags::useTurboModuleInterop(); + + if (!shouldCreateLegacyModules) { + return nullptr; + } + return [weakJavaPart = jni::make_weak(javaPart)]( + jsi::Runtime& /*runtime*/, const std::string& name) -> std::shared_ptr { auto javaPart = weakJavaPart.lockLocal(); if (!javaPart) { @@ -310,22 +319,30 @@ std::shared_ptr TurboModuleManager::getLegacyModule( return nullptr; } -void TurboModuleManager::installJSIBindings( +void TurboModuleManager::installJSBindings( + jsi::Runtime& runtime, + jni::alias_ref javaPart) { + TurboModuleBinding::install( + runtime, + createTurboModuleProvider(javaPart), + createLegacyModuleProvider(javaPart)); +} + +void TurboModuleManager::dispatchJSBindingInstall( jni::alias_ref javaPart, - bool shouldCreateLegacyModules) { + jni::alias_ref runtimeExecutor) { auto cxxPart = javaPart->cthis(); if (cxxPart == nullptr || !cxxPart->jsCallInvoker_) { return; // Runtime doesn't exist when attached to Chrome debugger. } - cxxPart->runtimeExecutor_([javaPart = jni::make_global(javaPart), - shouldCreateLegacyModules](jsi::Runtime& runtime) { - TurboModuleBinding::install( - runtime, - createTurboModuleProvider(javaPart, &runtime), - shouldCreateLegacyModules ? createLegacyModuleProvider(javaPart) - : nullptr); - }); + runtimeExecutor->cthis()->get()( + [javaPart = jni::make_global(javaPart)](jsi::Runtime& runtime) { + TurboModuleBinding::install( + runtime, + createTurboModuleProvider(javaPart), + createLegacyModuleProvider(javaPart)); + }); } } // namespace facebook::react diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.h b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.h index 296c2c2b0409e4..c520373f68938d 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.h +++ b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.h @@ -27,7 +27,6 @@ class TurboModuleManager : public jni::HybridClass { static auto constexpr kJavaDescriptor = "Lcom/facebook/react/internal/turbomodule/core/TurboModuleManager;"; static jni::local_ref initHybrid( jni::alias_ref /* unused */, - jni::alias_ref runtimeExecutor, jni::alias_ref jsCallInvokerHolder, jni::alias_ref nativeMethodCallInvokerHolder, jni::alias_ref delegate); @@ -52,20 +51,20 @@ class TurboModuleManager : public jni::HybridClass { ModuleCache legacyModuleCache_; explicit TurboModuleManager( - RuntimeExecutor runtimeExecutor, std::shared_ptr jsCallInvoker, std::shared_ptr nativeMethodCallInvoker, jni::alias_ref delegate); - static void installJSIBindings(jni::alias_ref javaPart, bool shouldCreateLegacyModules); - - static TurboModuleProviderFunctionType createTurboModuleProvider( + static void installJSBindings(jsi::Runtime &runtime, jni::alias_ref javaPart); + static void dispatchJSBindingInstall( jni::alias_ref javaPart, - jsi::Runtime *runtime); + jni::alias_ref runtimeExecutor); + + static TurboModuleProviderFunctionTypeWithRuntime createTurboModuleProvider(jni::alias_ref javaPart); std::shared_ptr getTurboModule(jni::alias_ref javaPart, const std::string &name, jsi::Runtime &runtime); - static TurboModuleProviderFunctionType createLegacyModuleProvider(jni::alias_ref javaPart); + static TurboModuleProviderFunctionTypeWithRuntime createLegacyModuleProvider(jni::alias_ref javaPart); std::shared_ptr getLegacyModule(jni::alias_ref javaPart, const std::string &name); }; diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h index fc622dfb5265f4..a1f1d1cb29ebf6 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h +++ b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h @@ -143,7 +143,12 @@ class JSI_EXPORT TurboModule : public jsi::HostObject { /** * An app/platform-specific provider function to get an instance of a module * given a name. + * + * @deprecated Use TurboModuleProviderFunctionTypeWithRuntime instead. */ -using TurboModuleProviderFunctionType = std::function(const std::string &name)>; +using TurboModuleProviderFunctionType [[deprecated("Use TurboModuleProviderFunctionTypeWithRuntime instead")]] = + std::function(const std::string &name)>; +using TurboModuleProviderFunctionTypeWithRuntime = + std::function(jsi::Runtime &runtime, const std::string &name)>; } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp index 195811f9876cf5..4d02c5a60f593b 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp +++ b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp @@ -19,24 +19,25 @@ namespace facebook::react { class BridgelessNativeModuleProxy : public jsi::HostObject { TurboModuleBinding turboBinding_; - std::unique_ptr legacyBinding_; + std::optional legacyBinding_; public: BridgelessNativeModuleProxy( jsi::Runtime& runtime, - TurboModuleProviderFunctionType&& moduleProvider, - TurboModuleProviderFunctionType&& legacyModuleProvider, + TurboModuleProviderFunctionTypeWithRuntime&& moduleProvider, + TurboModuleProviderFunctionTypeWithRuntime&& legacyModuleProvider, std::shared_ptr longLivedObjectCollection) : turboBinding_( runtime, std::move(moduleProvider), longLivedObjectCollection), legacyBinding_( - legacyModuleProvider ? std::make_unique( - runtime, - std::move(legacyModuleProvider), - longLivedObjectCollection) - : nullptr) {} + legacyModuleProvider + ? std::make_optional(TurboModuleBinding( + runtime, + std::move(legacyModuleProvider), + longLivedObjectCollection)) + : std::nullopt) {} jsi::Value get(jsi::Runtime& runtime, const jsi::PropNameID& name) override { /** @@ -88,7 +89,7 @@ class BridgelessNativeModuleProxy : public jsi::HostObject { TurboModuleBinding::TurboModuleBinding( jsi::Runtime& runtime, - TurboModuleProviderFunctionType&& moduleProvider, + TurboModuleProviderFunctionTypeWithRuntime&& moduleProvider, std::shared_ptr longLivedObjectCollection) : runtime_(runtime), moduleProvider_(std::move(moduleProvider)), @@ -99,6 +100,25 @@ void TurboModuleBinding::install( TurboModuleProviderFunctionType&& moduleProvider, TurboModuleProviderFunctionType&& legacyModuleProvider, std::shared_ptr longLivedObjectCollection) { + install( + runtime, + [moduleProvider = std::move(moduleProvider)]( + jsi::Runtime& runtime, const std::string& name) { + return moduleProvider(name); + }, + legacyModuleProvider == nullptr + ? (TurboModuleProviderFunctionTypeWithRuntime) nullptr + : [legacyModuleProvider = std::move(legacyModuleProvider)]( + jsi::Runtime& runtime, + const std::string& name) { return legacyModuleProvider(name); }, + longLivedObjectCollection); +} + +void TurboModuleBinding::install( + jsi::Runtime& runtime, + TurboModuleProviderFunctionTypeWithRuntime&& moduleProvider, + TurboModuleProviderFunctionTypeWithRuntime&& legacyModuleProvider, + std::shared_ptr longLivedObjectCollection) { // TODO(T208105802): We can get this information from the native side! auto isBridgeless = runtime.global().hasProperty(runtime, "RN$Bridgeless"); @@ -153,7 +173,7 @@ jsi::Value TurboModuleBinding::getModule( std::shared_ptr module; { TraceSection s("TurboModuleBinding::moduleProvider", "module", moduleName); - module = moduleProvider_(moduleName); + module = moduleProvider_(runtime, moduleName); } if (module) { TurboModuleWithJSIBindings::installJSIBindings(module, runtime); diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.h b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.h index ef94423c04eed6..e90c1515055360 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.h +++ b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.h @@ -26,23 +26,33 @@ class TurboModuleBinding final { /* * Installs TurboModuleBinding into JavaScript runtime. * Thread synchronization must be enforced externally. + * + * @deprecated Use the overload that takes + * TurboModuleProviderFunctionTypeWithRuntime instead. */ + [[deprecated("Use the overload that takes TurboModuleProviderFunctionTypeWithRuntime instead")]] static void install( jsi::Runtime &runtime, TurboModuleProviderFunctionType &&moduleProvider, TurboModuleProviderFunctionType &&legacyModuleProvider = nullptr, std::shared_ptr longLivedObjectCollection = nullptr); - TurboModuleBinding( + static void install( jsi::Runtime &runtime, - TurboModuleProviderFunctionType &&moduleProvider, - std::shared_ptr longLivedObjectCollection); + TurboModuleProviderFunctionTypeWithRuntime &&moduleProvider, + TurboModuleProviderFunctionTypeWithRuntime &&legacyModuleProvider = nullptr, + std::shared_ptr longLivedObjectCollection = nullptr); ~TurboModuleBinding(); private: friend BridgelessNativeModuleProxy; + TurboModuleBinding( + jsi::Runtime &runtime, + TurboModuleProviderFunctionTypeWithRuntime &&moduleProvider, + std::shared_ptr longLivedObjectCollection); + /** * A lookup function exposed to JS to get an instance of a TurboModule * for the given name. @@ -50,7 +60,7 @@ class TurboModuleBinding final { jsi::Value getModule(jsi::Runtime &runtime, const std::string &moduleName) const; jsi::Runtime &runtime_; - TurboModuleProviderFunctionType moduleProvider_; + TurboModuleProviderFunctionTypeWithRuntime moduleProvider_; std::shared_ptr longLivedObjectCollection_; }; 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 0f11ef114b9fbf..4f709dcfd58017 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 @@ -916,8 +916,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, - runtime = &runtime](const std::string &name) -> std::shared_ptr { + auto turboModuleProvider = + [self](jsi::Runtime &runtime, const std::string &name) -> std::shared_ptr { auto moduleName = name.c_str(); TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName); @@ -931,7 +931,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 runtime:runtime]; + auto turboModule = [self provideTurboModule:moduleName runtime:&runtime]; if (moduleWasNotInitialized && [self moduleIsInitialized:moduleName]) { [self->_bridge.performanceLogger markStopForTag:RCTPLTurboModuleSetup]; @@ -946,7 +946,8 @@ - (void)installJSBindings:(facebook::jsi::Runtime &)runtime }; if (RCTTurboModuleInteropEnabled()) { - auto legacyModuleProvider = [self](const std::string &name) -> std::shared_ptr { + auto legacyModuleProvider = + [self](jsi::Runtime & /*runtime*/, const std::string &name) -> std::shared_ptr { auto moduleName = name.c_str(); TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName);