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 cd9f033fe1d833..6b2a7ec2d1f6fe 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 @@ -93,28 +93,25 @@ class JMethodDescriptor : public jni::JavaClass { } // namespace TurboModuleManager::TurboModuleManager( - jni::alias_ref jThis, RuntimeExecutor runtimeExecutor, std::shared_ptr jsCallInvoker, std::shared_ptr nativeMethodCallInvoker, jni::alias_ref delegate) - : javaPart_(jni::make_global(jThis)), - runtimeExecutor_(runtimeExecutor), - jsCallInvoker_(jsCallInvoker), - nativeMethodCallInvoker_(nativeMethodCallInvoker), + : runtimeExecutor_(std::move(runtimeExecutor)), + jsCallInvoker_(std::move(jsCallInvoker)), + nativeMethodCallInvoker_(std::move(nativeMethodCallInvoker)), delegate_(jni::make_global(delegate)), turboModuleCache_(std::make_shared()), legacyModuleCache_(std::make_shared()) {} jni::local_ref TurboModuleManager::initHybrid( - jni::alias_ref jThis, + jni::alias_ref /* unused */, jni::alias_ref runtimeExecutor, jni::alias_ref jsCallInvokerHolder, jni::alias_ref nativeMethodCallInvokerHolder, jni::alias_ref delegate) { return makeCxxInstance( - jThis, runtimeExecutor->cthis()->get(), jsCallInvokerHolder->cthis()->getCallInvoker(), nativeMethodCallInvokerHolder->cthis()->getNativeMethodCallInvoker(), @@ -130,20 +127,21 @@ void TurboModuleManager::registerNatives() { } TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider( + jni::alias_ref javaPart, bool enableSyncVoidMethods) { return [turboModuleCache_ = std::weak_ptr(turboModuleCache_), jsCallInvoker_ = std::weak_ptr(jsCallInvoker_), nativeMethodCallInvoker_ = std::weak_ptr(nativeMethodCallInvoker_), - delegate_ = jni::make_weak(delegate_), - javaPart_ = jni::make_weak(javaPart_), + weakDelegate = jni::make_weak(delegate_), + weakJavaPart = jni::make_weak(javaPart), enableSyncVoidMethods]( const std::string& name) -> std::shared_ptr { auto turboModuleCache = turboModuleCache_.lock(); auto jsCallInvoker = jsCallInvoker_.lock(); auto nativeMethodCallInvoker = nativeMethodCallInvoker_.lock(); - auto delegate = delegate_.lockLocal(); - auto javaPart = javaPart_.lockLocal(); + auto delegate = weakDelegate.lockLocal(); + auto javaPart = weakJavaPart.lockLocal(); if (!turboModuleCache || !jsCallInvoker || !nativeMethodCallInvoker || !delegate || !javaPart) { @@ -219,20 +217,20 @@ TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider( }; } -TurboModuleProviderFunctionType -TurboModuleManager::createLegacyModuleProvider() { +TurboModuleProviderFunctionType TurboModuleManager::createLegacyModuleProvider( + jni::alias_ref javaPart) { return [legacyModuleCache_ = std::weak_ptr(legacyModuleCache_), jsCallInvoker_ = std::weak_ptr(jsCallInvoker_), nativeMethodCallInvoker_ = std::weak_ptr(nativeMethodCallInvoker_), - delegate_ = jni::make_weak(delegate_), - javaPart_ = jni::make_weak(javaPart_)]( + weakDelegate = jni::make_weak(delegate_), + weakJavaPart = jni::make_weak(javaPart)]( const std::string& name) -> std::shared_ptr { auto legacyModuleCache = legacyModuleCache_.lock(); auto jsCallInvoker = jsCallInvoker_.lock(); auto nativeMethodCallInvoker = nativeMethodCallInvoker_.lock(); - auto delegate = delegate_.lockLocal(); - auto javaPart = javaPart_.lockLocal(); + auto delegate = weakDelegate.lockLocal(); + auto javaPart = weakJavaPart.lockLocal(); if (!legacyModuleCache || !jsCallInvoker || !nativeMethodCallInvoker || !delegate || !javaPart) { @@ -313,26 +311,24 @@ TurboModuleManager::createLegacyModuleProvider() { } void TurboModuleManager::installJSIBindings( + jni::alias_ref javaPart, bool shouldCreateLegacyModules, bool enableSyncVoidMethods) { - if (!jsCallInvoker_) { + auto cxxPart = javaPart->cthis(); + if (!cxxPart || !cxxPart->jsCallInvoker_) { return; // Runtime doesn't exist when attached to Chrome debugger. } - bool isInteropLayerDisabled = !shouldCreateLegacyModules; - - runtimeExecutor_([this, isInteropLayerDisabled, enableSyncVoidMethods]( - jsi::Runtime& runtime) { - if (isInteropLayerDisabled) { - TurboModuleBinding::install( - runtime, createTurboModuleProvider(enableSyncVoidMethods)); - return; - } - + cxxPart->runtimeExecutor_([cxxPart, + javaPart = jni::make_global(javaPart), + shouldCreateLegacyModules, + enableSyncVoidMethods](jsi::Runtime& runtime) { TurboModuleBinding::install( runtime, - createTurboModuleProvider(enableSyncVoidMethods), - createLegacyModuleProvider()); + cxxPart->createTurboModuleProvider(javaPart, enableSyncVoidMethods), + shouldCreateLegacyModules + ? cxxPart->createLegacyModuleProvider(javaPart) + : nullptr); }); } 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 d77f5dce80e407..9cc27b76a44f4a 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 @@ -28,7 +28,7 @@ class TurboModuleManager : public jni::HybridClass { static auto constexpr kJavaDescriptor = "Lcom/facebook/react/internal/turbomodule/core/TurboModuleManager;"; static jni::local_ref initHybrid( - jni::alias_ref jThis, + jni::alias_ref /* unused */, jni::alias_ref runtimeExecutor, jni::alias_ref jsCallInvokerHolder, jni::alias_ref @@ -38,7 +38,6 @@ class TurboModuleManager : public jni::HybridClass { private: friend HybridBase; - jni::global_ref javaPart_; RuntimeExecutor runtimeExecutor_; std::shared_ptr jsCallInvoker_; std::shared_ptr nativeMethodCallInvoker_; @@ -56,19 +55,21 @@ class TurboModuleManager : public jni::HybridClass { std::shared_ptr turboModuleCache_; std::shared_ptr legacyModuleCache_; - void installJSIBindings( + static void installJSIBindings( + jni::alias_ref javaPart, bool shouldCreateLegacyModules, bool enableSyncVoidMethods); explicit TurboModuleManager( - jni::alias_ref jThis, RuntimeExecutor runtimeExecutor, std::shared_ptr jsCallInvoker, std::shared_ptr nativeMethodCallInvoker, jni::alias_ref delegate); TurboModuleProviderFunctionType createTurboModuleProvider( + jni::alias_ref javaPart, bool enableSyncVoidMethods); - TurboModuleProviderFunctionType createLegacyModuleProvider(); + TurboModuleProviderFunctionType createLegacyModuleProvider( + jni::alias_ref javaPart); }; } // namespace facebook::react