Skip to content

Commit

Permalink
Provide jsi::Runtime directly to RCTTurboModuleManager (#37687)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #37687

We end up creating a stub RuntimeExecutor wherever we configure this, and might as well pass in the runtime directly, to match the other binding installer signatures.

Changelog: [iOS][Deprecated] Use -[RCTTurboModuleManager installJSBindings:] instead of -[RCTTurboModuleManager installJSBindingWithRuntimeExecutor:]

Reviewed By: philIip

Differential Revision: D46390501

fbshipit-source-id: df7644ccbd04462dbbe752c60e5d5961ab50dff5
  • Loading branch information
javache authored and facebook-github-bot committed Jun 6, 2023
1 parent ea4724b commit 7fb9e4f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ void RCTAppSetupPrepareApp(UIApplication *application, BOOL turboModuleEnabled)
if (runtimeScheduler) {
facebook::react::RuntimeSchedulerBinding::createAndInstallIfNeeded(runtime, runtimeScheduler);
}
facebook::react::RuntimeExecutor syncRuntimeExecutor =
[&](std::function<void(facebook::jsi::Runtime & runtime_)> &&callback) { callback(runtime); };
[turboModuleManager installJSBindingWithRuntimeExecutor:syncRuntimeExecutor];
[turboModuleManager installJSBindings:runtime];
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,7 @@ - (void)_start
return;
}

facebook::react::RuntimeExecutor syncRuntimeExecutor =
[&](std::function<void(facebook::jsi::Runtime & runtime_)> &&callback) { callback(runtime); };
[strongSelf->_turboModuleManager installJSBindingWithRuntimeExecutor:syncRuntimeExecutor];
[strongSelf->_turboModuleManager installJSBindings:runtime];
facebook::react::bindNativeLogger(runtime, [](const std::string &message, unsigned int logLevel) {
_RCTLogJavaScriptInternal(static_cast<RCTLogLevel>(logLevel), [NSString stringWithUTF8String:message.c_str()]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ RCT_EXTERN void RCTTurboModuleSetBindingMode(facebook::react::TurboModuleBinding
delegate:(id<RCTTurboModuleManagerDelegate>)delegate
jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker;

- (void)installJSBindings:(facebook::jsi::Runtime &)runtime;

/**
* @deprecated: use installJSBindings instead
*/
- (void)installJSBindingWithRuntimeExecutor:(facebook::react::RuntimeExecutor &)runtimeExecutor;

- (void)invalidate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,13 +846,8 @@ - (BOOL)_requiresMainQueueSetup:(Class)moduleClass
return requiresMainQueueSetup;
}

- (void)installJSBindingWithRuntimeExecutor:(facebook::react::RuntimeExecutor &)runtimeExecutor
- (void)installJSBindings:(facebook::jsi::Runtime &)runtime
{
if (!runtimeExecutor) {
// jsi::Runtime doesn't exist when attached to Chrome debugger.
return;
}

/**
* We keep TurboModuleManager alive until the JS VM is deleted.
* It is perfectly valid to only use/create TurboModules from JS.
Expand Down Expand Up @@ -886,48 +881,51 @@ - (void)installJSBindingWithRuntimeExecutor:(facebook::react::RuntimeExecutor &)
} else {
TurboModulePerfLogger::moduleJSRequireEndingFail(moduleName);
}

return turboModule;
};

if (!RCTTurboModuleInteropEnabled()) {
runtimeExecutor([turboModuleProvider = std::move(turboModuleProvider)](jsi::Runtime &runtime) {
TurboModuleBinding::install(runtime, sTurboModuleBindingMode, std::move(turboModuleProvider));
});
return;
}

auto legacyModuleProvider = [self](const std::string &name) -> std::shared_ptr<react::TurboModule> {
auto moduleName = name.c_str();

TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName);
auto moduleWasNotInitialized = ![self moduleIsInitialized:moduleName];
if (RCTTurboModuleInteropEnabled()) {
auto legacyModuleProvider = [self](const std::string &name) -> std::shared_ptr<react::TurboModule> {
auto moduleName = name.c_str();

/**
* By default, all TurboModules are long-lived.
* Additionally, if a TurboModule with the name `name` isn't found, then we
* trigger an assertion failure.
*/
auto turboModule = [self provideLegacyModule:moduleName];
TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName);
auto moduleWasNotInitialized = ![self moduleIsInitialized:moduleName];

if (moduleWasNotInitialized && [self moduleIsInitialized:moduleName]) {
[self notifyAboutTurboModuleSetup:moduleName];
}
/**
* By default, all TurboModules are long-lived.
* Additionally, if a TurboModule with the name `name` isn't found, then we
* trigger an assertion failure.
*/
auto turboModule = [self provideLegacyModule:moduleName];

if (turboModule) {
TurboModulePerfLogger::moduleJSRequireEndingEnd(moduleName);
} else {
TurboModulePerfLogger::moduleJSRequireEndingFail(moduleName);
}
if (moduleWasNotInitialized && [self moduleIsInitialized:moduleName]) {
[self notifyAboutTurboModuleSetup:moduleName];
}

return turboModule;
};
if (turboModule) {
TurboModulePerfLogger::moduleJSRequireEndingEnd(moduleName);
} else {
TurboModulePerfLogger::moduleJSRequireEndingFail(moduleName);
}
return turboModule;
};

runtimeExecutor([turboModuleProvider = std::move(turboModuleProvider),
legacyModuleProvider = std::move(legacyModuleProvider)](jsi::Runtime &runtime) {
TurboModuleBinding::install(
runtime, sTurboModuleBindingMode, std::move(turboModuleProvider), std::move(legacyModuleProvider));
});
} else {
TurboModuleBinding::install(runtime, sTurboModuleBindingMode, std::move(turboModuleProvider));
}
}

/**
* @deprecated: use installJSBindings instead
*/
- (void)installJSBindingWithRuntimeExecutor:(facebook::react::RuntimeExecutor &)runtimeExecutor
{
// jsi::Runtime doesn't exist when attached to Chrome debugger.
if (runtimeExecutor) {
runtimeExecutor([self](facebook::jsi::Runtime &runtime) { [self installJSBindings:runtime]; });
}
}

#pragma mark RCTTurboModuleRegistry
Expand Down

0 comments on commit 7fb9e4f

Please sign in to comment.