From 5a62606ab36edaffb0a5c760d2b0ed0c8e3808a8 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Tue, 9 Jul 2024 07:34:11 -0700 Subject: [PATCH] Lookup TurboModules before fallbacks (#45320) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45320 TurboModule should be the default path, and we should only fallback to the legacy native modules if we can't find a module through the TurboModule mechanism. Changelog: [General][Changed] - TurboModules will be looked up as TurboModules first, and fallback to legacy modules after. Reviewed By: christophpurrer Differential Revision: D59465978 fbshipit-source-id: c5672d34e90dcee321de0a5acd3a50b6bb1092b8 --- .../TurboModule/TurboModuleRegistry.js | 24 +++--- .../ReactCommon/TurboModuleManager.cpp | 81 +++++++++---------- 2 files changed, 51 insertions(+), 54 deletions(-) diff --git a/packages/react-native/Libraries/TurboModule/TurboModuleRegistry.js b/packages/react-native/Libraries/TurboModule/TurboModuleRegistry.js index 8fdf999d49e759..49ddb59f12217d 100644 --- a/packages/react-native/Libraries/TurboModule/TurboModuleRegistry.js +++ b/packages/react-native/Libraries/TurboModule/TurboModuleRegistry.js @@ -35,20 +35,7 @@ function shouldReportDebugInfo() { return true; } -// TODO(148943970): Consider reversing the lookup here: -// Lookup on __turboModuleProxy, then lookup on nativeModuleProxy function requireModule(name: string): ?T { - if (!isBridgeless() || isTurboModuleInteropEnabled()) { - // Backward compatibility layer during migration. - const legacyModule = NativeModules[name]; - if (legacyModule != null) { - if (shouldReportDebugInfo()) { - moduleLoadHistory.NativeModules.push(name); - } - return ((legacyModule: $FlowFixMe): T); - } - } - if (turboModuleProxy != null) { const module: ?T = turboModuleProxy(name); if (module != null) { @@ -59,6 +46,17 @@ function requireModule(name: string): ?T { } } + if (!isBridgeless() || isTurboModuleInteropEnabled()) { + // Backward compatibility layer during migration. + const legacyModule: ?T = NativeModules[name]; + if (legacyModule != null) { + if (shouldReportDebugInfo()) { + moduleLoadHistory.NativeModules.push(name); + } + return legacyModule; + } + } + if (shouldReportDebugInfo() && !moduleLoadHistory.NotFound.includes(name)) { moduleLoadHistory.NotFound.push(name); } 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 d18685da1fc349..3ea3f3bce2ac44 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 @@ -140,28 +140,30 @@ TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider( 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 = weakDelegate.lockLocal(); - auto javaPart = weakJavaPart.lockLocal(); + const char* moduleName = name.c_str(); + TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName); - if (!turboModuleCache || !jsCallInvoker || !nativeMethodCallInvoker || - !delegate || !javaPart) { + auto turboModuleCache = turboModuleCache_.lock(); + if (!turboModuleCache) { return nullptr; } - const char* moduleName = name.c_str(); - - TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName); - - auto turboModuleLookup = turboModuleCache->find(name); + auto turboModuleLookup = turboModuleCache->find(moduleName); if (turboModuleLookup != turboModuleCache->end()) { TurboModulePerfLogger::moduleJSRequireBeginningCacheHit(moduleName); TurboModulePerfLogger::moduleJSRequireBeginningEnd(moduleName); return turboModuleLookup->second; } + auto jsCallInvoker = jsCallInvoker_.lock(); + auto nativeMethodCallInvoker = nativeMethodCallInvoker_.lock(); + auto delegate = weakDelegate.lockLocal(); + auto javaPart = weakJavaPart.lockLocal(); + + if (!jsCallInvoker || !nativeMethodCallInvoker || !delegate || !javaPart) { + return nullptr; + } + TurboModulePerfLogger::moduleJSRequireBeginningEnd(moduleName); auto cxxModule = delegate->cthis()->getTurboModule(name, jsCallInvoker); @@ -178,29 +180,11 @@ TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider( return turboModule; } - static auto getTurboLegacyCxxModule = - javaPart->getClass() - ->getMethod( - const std::string&)>("getTurboLegacyCxxModule"); - auto legacyCxxModule = getTurboLegacyCxxModule(javaPart.get(), name); - - if (legacyCxxModule) { - TurboModulePerfLogger::moduleJSRequireEndingStart(moduleName); - - auto turboModule = std::make_shared( - legacyCxxModule->cthis()->getModule(), jsCallInvoker); - turboModuleCache->insert({name, turboModule}); - - TurboModulePerfLogger::moduleJSRequireEndingEnd(moduleName); - return turboModule; - } - static auto getTurboJavaModule = javaPart->getClass() ->getMethod(const std::string&)>( "getTurboJavaModule"); auto moduleInstance = getTurboJavaModule(javaPart.get(), name); - if (moduleInstance) { TurboModulePerfLogger::moduleJSRequireEndingStart(moduleName); JavaTurboModule::InitParams params = { @@ -228,6 +212,22 @@ TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider( return turboModule; } + static auto getTurboLegacyCxxModule = + javaPart->getClass() + ->getMethod( + const std::string&)>("getTurboLegacyCxxModule"); + auto legacyCxxModule = getTurboLegacyCxxModule(javaPart.get(), name); + if (legacyCxxModule) { + TurboModulePerfLogger::moduleJSRequireEndingStart(moduleName); + + auto turboModule = std::make_shared( + legacyCxxModule->cthis()->getModule(), jsCallInvoker); + turboModuleCache->insert({name, turboModule}); + + TurboModulePerfLogger::moduleJSRequireEndingEnd(moduleName); + return turboModule; + } + return nullptr; }; } @@ -241,7 +241,17 @@ TurboModuleProviderFunctionType TurboModuleManager::createLegacyModuleProvider( weakDelegate = jni::make_weak(delegate_), weakJavaPart = jni::make_weak(javaPart)]( const std::string& name) -> std::shared_ptr { + const char* moduleName = name.c_str(); + TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName); + auto legacyModuleCache = legacyModuleCache_.lock(); + auto legacyModuleLookup = legacyModuleCache->find(name); + if (legacyModuleLookup != legacyModuleCache->end()) { + TurboModulePerfLogger::moduleJSRequireBeginningCacheHit(moduleName); + TurboModulePerfLogger::moduleJSRequireBeginningEnd(moduleName); + return legacyModuleLookup->second; + } + auto jsCallInvoker = jsCallInvoker_.lock(); auto nativeMethodCallInvoker = nativeMethodCallInvoker_.lock(); auto delegate = weakDelegate.lockLocal(); @@ -252,17 +262,6 @@ TurboModuleProviderFunctionType TurboModuleManager::createLegacyModuleProvider( return nullptr; } - const char* moduleName = name.c_str(); - - TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName); - - auto legacyModuleLookup = legacyModuleCache->find(name); - if (legacyModuleLookup != legacyModuleCache->end()) { - TurboModulePerfLogger::moduleJSRequireBeginningCacheHit(moduleName); - TurboModulePerfLogger::moduleJSRequireBeginningEnd(moduleName); - return legacyModuleLookup->second; - } - TurboModulePerfLogger::moduleJSRequireBeginningEnd(moduleName); static auto getLegacyCxxModule =