Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@ void TurboModuleManager::registerNatives() {
});
}

TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider(
jni::alias_ref<jhybridobject> javaPart,
jsi::Runtime* runtime) {
return [runtime, weakJavaPart = jni::make_weak(javaPart)](
TurboModuleProviderFunctionTypeWithRuntime
TurboModuleManager::createTurboModuleProvider(
jni::alias_ref<jhybridobject> javaPart) {
return [weakJavaPart = jni::make_weak(javaPart)](
jsi::Runtime& runtime,
const std::string& name) -> std::shared_ptr<TurboModule> {
auto javaPart = weakJavaPart.lockLocal();
if (!javaPart) {
Expand All @@ -140,7 +141,7 @@ TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider(
return nullptr;
}

return cxxPart->getTurboModule(javaPart, name, *runtime);
return cxxPart->getTurboModule(javaPart, name, runtime);
};
}

Expand Down Expand Up @@ -223,9 +224,11 @@ std::shared_ptr<TurboModule> TurboModuleManager::getTurboModule(
return nullptr;
}

TurboModuleProviderFunctionType TurboModuleManager::createLegacyModuleProvider(
TurboModuleProviderFunctionTypeWithRuntime
TurboModuleManager::createLegacyModuleProvider(
jni::alias_ref<jhybridobject> javaPart) {
return [weakJavaPart = jni::make_weak(javaPart)](
jsi::Runtime& /*runtime*/,
const std::string& name) -> std::shared_ptr<TurboModule> {
auto javaPart = weakJavaPart.lockLocal();
if (!javaPart) {
Expand Down Expand Up @@ -322,7 +325,7 @@ void TurboModuleManager::installJSIBindings(
shouldCreateLegacyModules](jsi::Runtime& runtime) {
TurboModuleBinding::install(
runtime,
createTurboModuleProvider(javaPart, &runtime),
createTurboModuleProvider(javaPart),
shouldCreateLegacyModules ? createLegacyModuleProvider(javaPart)
: nullptr);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@ class TurboModuleManager : public jni::HybridClass<TurboModuleManager> {

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

static TurboModuleProviderFunctionType createTurboModuleProvider(
jni::alias_ref<jhybridobject> javaPart,
jsi::Runtime *runtime);
static TurboModuleProviderFunctionTypeWithRuntime createTurboModuleProvider(jni::alias_ref<jhybridobject> javaPart);
std::shared_ptr<TurboModule>
getTurboModule(jni::alias_ref<jhybridobject> javaPart, const std::string &name, jsi::Runtime &runtime);

static TurboModuleProviderFunctionType createLegacyModuleProvider(jni::alias_ref<jhybridobject> javaPart);
static TurboModuleProviderFunctionTypeWithRuntime createLegacyModuleProvider(jni::alias_ref<jhybridobject> javaPart);
std::shared_ptr<TurboModule> getLegacyModule(jni::alias_ref<jhybridobject> javaPart, const std::string &name);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ class JSI_EXPORT TurboModule : public jsi::HostObject {
/**
* An app/platform-specific provider function to get an instance of a module
* given a name.
*
* @deprecated Use TurboModuleProviderFunctionTypeWithRuntime instead.
* Remove after React Native 0.84 is released.
*/
using TurboModuleProviderFunctionType = std::function<std::shared_ptr<TurboModule>(const std::string &name)>;
using TurboModuleProviderFunctionType [[deprecated("Use TurboModuleProviderFunctionTypeWithRuntime instead")]] =
std::function<std::shared_ptr<TurboModule>(const std::string &name)>;
using TurboModuleProviderFunctionTypeWithRuntime =
std::function<std::shared_ptr<TurboModule>(jsi::Runtime &runtime, const std::string &name)>;

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,25 @@ namespace facebook::react {

class BridgelessNativeModuleProxy : public jsi::HostObject {
TurboModuleBinding turboBinding_;
std::unique_ptr<TurboModuleBinding> legacyBinding_;
std::optional<TurboModuleBinding> legacyBinding_;

public:
BridgelessNativeModuleProxy(
jsi::Runtime& runtime,
TurboModuleProviderFunctionType&& moduleProvider,
TurboModuleProviderFunctionType&& legacyModuleProvider,
TurboModuleProviderFunctionTypeWithRuntime&& moduleProvider,
TurboModuleProviderFunctionTypeWithRuntime&& legacyModuleProvider,
std::shared_ptr<LongLivedObjectCollection> longLivedObjectCollection)
: turboBinding_(
runtime,
std::move(moduleProvider),
longLivedObjectCollection),
legacyBinding_(
legacyModuleProvider ? std::make_unique<TurboModuleBinding>(
runtime,
std::move(legacyModuleProvider),
longLivedObjectCollection)
: nullptr) {}
legacyModuleProvider
? std::make_optional<TurboModuleBinding>(TurboModuleBinding(
runtime,
std::move(legacyModuleProvider),
longLivedObjectCollection))
: std::nullopt) {}

jsi::Value get(jsi::Runtime& runtime, const jsi::PropNameID& name) override {
/**
Expand Down Expand Up @@ -88,7 +89,7 @@ class BridgelessNativeModuleProxy : public jsi::HostObject {

TurboModuleBinding::TurboModuleBinding(
jsi::Runtime& runtime,
TurboModuleProviderFunctionType&& moduleProvider,
TurboModuleProviderFunctionTypeWithRuntime&& moduleProvider,
std::shared_ptr<LongLivedObjectCollection> longLivedObjectCollection)
: runtime_(runtime),
moduleProvider_(std::move(moduleProvider)),
Expand All @@ -99,6 +100,25 @@ void TurboModuleBinding::install(
TurboModuleProviderFunctionType&& moduleProvider,
TurboModuleProviderFunctionType&& legacyModuleProvider,
std::shared_ptr<LongLivedObjectCollection> longLivedObjectCollection) {
install(
runtime,
[moduleProvider = std::move(moduleProvider)](
jsi::Runtime& runtime, const std::string& name) {
return moduleProvider(name);
},
legacyModuleProvider == nullptr
? (TurboModuleProviderFunctionTypeWithRuntime) nullptr
: [legacyModuleProvider = std::move(legacyModuleProvider)](
jsi::Runtime& runtime,
const std::string& name) { return legacyModuleProvider(name); },
longLivedObjectCollection);
}

void TurboModuleBinding::install(
jsi::Runtime& runtime,
TurboModuleProviderFunctionTypeWithRuntime&& moduleProvider,
TurboModuleProviderFunctionTypeWithRuntime&& legacyModuleProvider,
std::shared_ptr<LongLivedObjectCollection> longLivedObjectCollection) {
// TODO(T208105802): We can get this information from the native side!
auto isBridgeless = runtime.global().hasProperty(runtime, "RN$Bridgeless");

Expand Down Expand Up @@ -153,7 +173,7 @@ jsi::Value TurboModuleBinding::getModule(
std::shared_ptr<TurboModule> module;
{
TraceSection s("TurboModuleBinding::moduleProvider", "module", moduleName);
module = moduleProvider_(moduleName);
module = moduleProvider_(runtime, moduleName);
}
if (module) {
TurboModuleWithJSIBindings::installJSIBindings(module, runtime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,42 @@ class TurboModuleBinding final {
/*
* Installs TurboModuleBinding into JavaScript runtime.
* Thread synchronization must be enforced externally.
*
* @deprecated Use the overload that takes
* TurboModuleProviderFunctionTypeWithRuntime instead.
* Remove after React Native 0.84 is released.
*/
[[deprecated("Use the overload that takes TurboModuleProviderFunctionTypeWithRuntime instead")]]
static void install(
jsi::Runtime &runtime,
TurboModuleProviderFunctionType &&moduleProvider,
TurboModuleProviderFunctionType &&legacyModuleProvider = nullptr,
std::shared_ptr<LongLivedObjectCollection> longLivedObjectCollection = nullptr);

TurboModuleBinding(
static void install(
jsi::Runtime &runtime,
TurboModuleProviderFunctionType &&moduleProvider,
std::shared_ptr<LongLivedObjectCollection> longLivedObjectCollection);
TurboModuleProviderFunctionTypeWithRuntime &&moduleProvider,
TurboModuleProviderFunctionTypeWithRuntime &&legacyModuleProvider = nullptr,
std::shared_ptr<LongLivedObjectCollection> longLivedObjectCollection = nullptr);

~TurboModuleBinding();

private:
friend BridgelessNativeModuleProxy;

TurboModuleBinding(
jsi::Runtime &runtime,
TurboModuleProviderFunctionTypeWithRuntime &&moduleProvider,
std::shared_ptr<LongLivedObjectCollection> longLivedObjectCollection);

/**
* A lookup function exposed to JS to get an instance of a TurboModule
* for the given name.
*/
jsi::Value getModule(jsi::Runtime &runtime, const std::string &moduleName) const;

jsi::Runtime &runtime_;
TurboModuleProviderFunctionType moduleProvider_;
TurboModuleProviderFunctionTypeWithRuntime moduleProvider_;
std::shared_ptr<LongLivedObjectCollection> longLivedObjectCollection_;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,8 @@ - (void)installJSBindings:(facebook::jsi::Runtime &)runtime
* aren't any strong references to it in ObjC. Hence, we give
* __turboModuleProxy a strong reference to TurboModuleManager.
*/
auto turboModuleProvider = [self,
runtime = &runtime](const std::string &name) -> std::shared_ptr<react::TurboModule> {
auto turboModuleProvider =
[self](jsi::Runtime &runtime, const std::string &name) -> std::shared_ptr<react::TurboModule> {
auto moduleName = name.c_str();

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

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

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

TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName);
Expand Down
Loading