Skip to content

Commit 2196e3e

Browse files
RSNarafacebook-github-bot
authored andcommitted
Stop storing runtime ptr in turbomodule provider
Summary: Holding a reference to the runtime is discouraged. Let's just use the reference provided to the turbomodule binding instead. Changelog: [General][Deprecated] Deprecate TurboModuleProviderFunctionType Reviewed By: christophpurrer Differential Revision: D89751220
1 parent 41ad94b commit 2196e3e

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ void TurboModuleManager::registerNatives() {
125125
});
126126
}
127127

128-
TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider(
129-
jni::alias_ref<jhybridobject> javaPart,
130-
jsi::Runtime* runtime) {
131-
return [runtime, weakJavaPart = jni::make_weak(javaPart)](
128+
TurboModuleProviderFunctionTypeWithRuntime
129+
TurboModuleManager::createTurboModuleProvider(
130+
jni::alias_ref<jhybridobject> javaPart) {
131+
return [weakJavaPart = jni::make_weak(javaPart)](
132+
jsi::Runtime& runtime,
132133
const std::string& name) -> std::shared_ptr<TurboModule> {
133134
auto javaPart = weakJavaPart.lockLocal();
134135
if (!javaPart) {
@@ -140,7 +141,7 @@ TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider(
140141
return nullptr;
141142
}
142143

143-
return cxxPart->getTurboModule(javaPart, name, *runtime);
144+
return cxxPart->getTurboModule(javaPart, name, runtime);
144145
};
145146
}
146147

@@ -223,9 +224,11 @@ std::shared_ptr<TurboModule> TurboModuleManager::getTurboModule(
223224
return nullptr;
224225
}
225226

226-
TurboModuleProviderFunctionType TurboModuleManager::createLegacyModuleProvider(
227+
TurboModuleProviderFunctionTypeWithRuntime
228+
TurboModuleManager::createLegacyModuleProvider(
227229
jni::alias_ref<jhybridobject> javaPart) {
228230
return [weakJavaPart = jni::make_weak(javaPart)](
231+
jsi::Runtime& /*runtime*/,
229232
const std::string& name) -> std::shared_ptr<TurboModule> {
230233
auto javaPart = weakJavaPart.lockLocal();
231234
if (!javaPart) {
@@ -322,7 +325,7 @@ void TurboModuleManager::installJSIBindings(
322325
shouldCreateLegacyModules](jsi::Runtime& runtime) {
323326
TurboModuleBinding::install(
324327
runtime,
325-
createTurboModuleProvider(javaPart, &runtime),
328+
createTurboModuleProvider(javaPart),
326329
shouldCreateLegacyModules ? createLegacyModuleProvider(javaPart)
327330
: nullptr);
328331
});

packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,11 @@ class TurboModuleManager : public jni::HybridClass<TurboModuleManager> {
5959

6060
static void installJSIBindings(jni::alias_ref<jhybridobject> javaPart, bool shouldCreateLegacyModules);
6161

62-
static TurboModuleProviderFunctionType createTurboModuleProvider(
63-
jni::alias_ref<jhybridobject> javaPart,
64-
jsi::Runtime *runtime);
62+
static TurboModuleProviderFunctionTypeWithRuntime createTurboModuleProvider(jni::alias_ref<jhybridobject> javaPart);
6563
std::shared_ptr<TurboModule>
6664
getTurboModule(jni::alias_ref<jhybridobject> javaPart, const std::string &name, jsi::Runtime &runtime);
6765

68-
static TurboModuleProviderFunctionType createLegacyModuleProvider(jni::alias_ref<jhybridobject> javaPart);
66+
static TurboModuleProviderFunctionTypeWithRuntime createLegacyModuleProvider(jni::alias_ref<jhybridobject> javaPart);
6967
std::shared_ptr<TurboModule> getLegacyModule(jni::alias_ref<jhybridobject> javaPart, const std::string &name);
7068
};
7169

packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,12 @@ class JSI_EXPORT TurboModule : public jsi::HostObject {
143143
/**
144144
* An app/platform-specific provider function to get an instance of a module
145145
* given a name.
146+
*
147+
* @deprecated Use TurboModuleProviderFunctionTypeWithRuntime instead.
148+
* Remove after React Native 0.84 is released.
146149
*/
147-
using TurboModuleProviderFunctionType = std::function<std::shared_ptr<TurboModule>(const std::string &name)>;
150+
using TurboModuleProviderFunctionType [[deprecated("Use TurboModuleProviderFunctionTypeWithRuntime instead")]] =
151+
std::function<std::shared_ptr<TurboModule>(const std::string &name)>;
148152
using TurboModuleProviderFunctionTypeWithRuntime =
149153
std::function<std::shared_ptr<TurboModule>(jsi::Runtime &runtime, const std::string &name)>;
150154

packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ class TurboModuleBinding final {
2626
/*
2727
* Installs TurboModuleBinding into JavaScript runtime.
2828
* Thread synchronization must be enforced externally.
29+
*
30+
* @deprecated Use the overload that takes
31+
* TurboModuleProviderFunctionTypeWithRuntime instead.
32+
* Remove after React Native 0.84 is released.
2933
*/
34+
[[deprecated("Use the overload that takes TurboModuleProviderFunctionTypeWithRuntime instead")]]
3035
static void install(
3136
jsi::Runtime &runtime,
3237
TurboModuleProviderFunctionType &&moduleProvider,

packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -916,8 +916,8 @@ - (void)installJSBindings:(facebook::jsi::Runtime &)runtime
916916
* aren't any strong references to it in ObjC. Hence, we give
917917
* __turboModuleProxy a strong reference to TurboModuleManager.
918918
*/
919-
auto turboModuleProvider = [self,
920-
runtime = &runtime](const std::string &name) -> std::shared_ptr<react::TurboModule> {
919+
auto turboModuleProvider =
920+
[self](jsi::Runtime &runtime, const std::string &name) -> std::shared_ptr<react::TurboModule> {
921921
auto moduleName = name.c_str();
922922

923923
TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName);
@@ -931,7 +931,7 @@ - (void)installJSBindings:(facebook::jsi::Runtime &)runtime
931931
* Additionally, if a TurboModule with the name `name` isn't found, then we
932932
* trigger an assertion failure.
933933
*/
934-
auto turboModule = [self provideTurboModule:moduleName runtime:runtime];
934+
auto turboModule = [self provideTurboModule:moduleName runtime:&runtime];
935935

936936
if (moduleWasNotInitialized && [self moduleIsInitialized:moduleName]) {
937937
[self->_bridge.performanceLogger markStopForTag:RCTPLTurboModuleSetup];
@@ -946,7 +946,8 @@ - (void)installJSBindings:(facebook::jsi::Runtime &)runtime
946946
};
947947

948948
if (RCTTurboModuleInteropEnabled()) {
949-
auto legacyModuleProvider = [self](const std::string &name) -> std::shared_ptr<react::TurboModule> {
949+
auto legacyModuleProvider =
950+
[self](jsi::Runtime & /*runtime*/, const std::string &name) -> std::shared_ptr<react::TurboModule> {
950951
auto moduleName = name.c_str();
951952

952953
TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName);

0 commit comments

Comments
 (0)