Skip to content

Commit

Permalink
Lookup TurboModules before fallbacks (#45320)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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
  • Loading branch information
javache authored and facebook-github-bot committed Jul 9, 2024
1 parent 5b03453 commit 5a62606
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 54 deletions.
24 changes: 11 additions & 13 deletions packages/react-native/Libraries/TurboModule/TurboModuleRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,7 @@ function shouldReportDebugInfo() {
return true;
}

// TODO(148943970): Consider reversing the lookup here:
// Lookup on __turboModuleProxy, then lookup on nativeModuleProxy
function requireModule<T: TurboModule>(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) {
Expand All @@ -59,6 +46,17 @@ function requireModule<T: TurboModule>(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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,30 @@ TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider(
weakJavaPart = jni::make_weak(javaPart),
enableSyncVoidMethods](
const std::string& name) -> std::shared_ptr<TurboModule> {
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);
Expand All @@ -178,29 +180,11 @@ TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider(
return turboModule;
}

static auto getTurboLegacyCxxModule =
javaPart->getClass()
->getMethod<jni::alias_ref<CxxModuleWrapper::javaobject>(
const std::string&)>("getTurboLegacyCxxModule");
auto legacyCxxModule = getTurboLegacyCxxModule(javaPart.get(), name);

if (legacyCxxModule) {
TurboModulePerfLogger::moduleJSRequireEndingStart(moduleName);

auto turboModule = std::make_shared<react::TurboCxxModule>(
legacyCxxModule->cthis()->getModule(), jsCallInvoker);
turboModuleCache->insert({name, turboModule});

TurboModulePerfLogger::moduleJSRequireEndingEnd(moduleName);
return turboModule;
}

static auto getTurboJavaModule =
javaPart->getClass()
->getMethod<jni::alias_ref<JTurboModule>(const std::string&)>(
"getTurboJavaModule");
auto moduleInstance = getTurboJavaModule(javaPart.get(), name);

if (moduleInstance) {
TurboModulePerfLogger::moduleJSRequireEndingStart(moduleName);
JavaTurboModule::InitParams params = {
Expand Down Expand Up @@ -228,6 +212,22 @@ TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider(
return turboModule;
}

static auto getTurboLegacyCxxModule =
javaPart->getClass()
->getMethod<jni::alias_ref<CxxModuleWrapper::javaobject>(
const std::string&)>("getTurboLegacyCxxModule");
auto legacyCxxModule = getTurboLegacyCxxModule(javaPart.get(), name);
if (legacyCxxModule) {
TurboModulePerfLogger::moduleJSRequireEndingStart(moduleName);

auto turboModule = std::make_shared<react::TurboCxxModule>(
legacyCxxModule->cthis()->getModule(), jsCallInvoker);
turboModuleCache->insert({name, turboModule});

TurboModulePerfLogger::moduleJSRequireEndingEnd(moduleName);
return turboModule;
}

return nullptr;
};
}
Expand All @@ -241,7 +241,17 @@ TurboModuleProviderFunctionType TurboModuleManager::createLegacyModuleProvider(
weakDelegate = jni::make_weak(delegate_),
weakJavaPart = jni::make_weak(javaPart)](
const std::string& name) -> std::shared_ptr<TurboModule> {
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();
Expand All @@ -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 =
Expand Down

0 comments on commit 5a62606

Please sign in to comment.