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
1 change: 0 additions & 1 deletion packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ public abstract class com/facebook/react/ReactPackageTurboModuleManagerDelegate
public fun getModule (Ljava/lang/String;)Lcom/facebook/react/turbomodule/core/interfaces/TurboModule;
public fun unstable_isLegacyModuleRegistered (Ljava/lang/String;)Z
public fun unstable_isModuleRegistered (Ljava/lang/String;)Z
public fun unstable_shouldEnableLegacyModuleInterop ()Z
}

public abstract class com/facebook/react/ReactPackageTurboModuleManagerDelegate$Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ public abstract class ReactPackageTurboModuleManagerDelegate : TurboModuleManage
}
}

override fun unstable_shouldEnableLegacyModuleInterop(): Boolean = shouldEnableLegacyModuleInterop

override fun getModule(moduleName: String): TurboModule? {
var resolvedModule: NativeModule? = null

Expand Down Expand Up @@ -153,7 +151,7 @@ public abstract class ReactPackageTurboModuleManagerDelegate : TurboModuleManage
}

override fun getLegacyModule(moduleName: String): NativeModule? {
if (!unstable_shouldEnableLegacyModuleInterop()) {
if (!shouldEnableLegacyModuleInterop) {
return null
}

Expand Down Expand Up @@ -191,7 +189,7 @@ public abstract class ReactPackageTurboModuleManagerDelegate : TurboModuleManage
}
}

private fun shouldSupportLegacyPackages(): Boolean = unstable_shouldEnableLegacyModuleInterop()
private fun shouldSupportLegacyPackages(): Boolean = shouldEnableLegacyModuleInterop

public abstract class Builder {
private var packages: List<ReactPackage>? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class TurboModuleManager(

init {

installJSIBindings(shouldEnableLegacyModuleInterop())
installJSIBindings()

eagerInitModuleNames = delegate?.getEagerInitModuleNames() ?: emptyList()

Expand All @@ -72,7 +72,7 @@ public class TurboModuleManager(
ModuleProvider { moduleName: String -> delegate.getModule(moduleName) as NativeModule? }

legacyModuleProvider =
if (delegate == null || !shouldEnableLegacyModuleInterop()) nullProvider
if (delegate == null) nullProvider
else
ModuleProvider { moduleName: String ->
val nativeModule = delegate.getLegacyModule(moduleName)
Expand All @@ -93,9 +93,6 @@ public class TurboModuleManager(
private fun isLegacyModule(moduleName: String): Boolean =
delegate?.unstable_isLegacyModuleRegistered(moduleName) == true

private fun shouldEnableLegacyModuleInterop(): Boolean =
delegate?.unstable_shouldEnableLegacyModuleInterop() == true

// used from TurboModuleManager.cpp
@Suppress("unused")
@DoNotStrip
Expand Down Expand Up @@ -287,7 +284,7 @@ public class TurboModuleManager(
tmmDelegate: TurboModuleManagerDelegate?,
): HybridData

private external fun installJSIBindings(shouldCreateLegacyModules: Boolean)
private external fun installJSIBindings()

override fun invalidate() {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ public abstract class TurboModuleManagerDelegate {

public open fun getEagerInitModuleNames(): List<String> = emptyList()

/** Can the TurboModule system create legacy modules? */
public open fun unstable_shouldEnableLegacyModuleInterop(): Boolean = false

// TODO(T171231381): Consider removing this method: could we just use the static initializer
// of derived classes instead?
@Synchronized protected fun maybeLoadOtherSoLibraries(): Unit = Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <ReactCommon/JavaInteropTurboModule.h>
#include <ReactCommon/TurboModuleBinding.h>
#include <ReactCommon/TurboModulePerfLogger.h>
#include <react/featureflags/ReactNativeFeatureFlags.h>
#include <react/jni/CxxModuleWrapperBase.h>

namespace facebook::react {
Expand Down Expand Up @@ -125,10 +126,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 +142,7 @@ TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider(
return nullptr;
}

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

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

TurboModuleProviderFunctionType TurboModuleManager::createLegacyModuleProvider(
TurboModuleProviderFunctionTypeWithRuntime
TurboModuleManager::createLegacyModuleProvider(
jni::alias_ref<jhybridobject> javaPart) {
bool shouldCreateLegacyModules =
ReactNativeFeatureFlags::enableBridgelessArchitecture() &&
ReactNativeFeatureFlags::useTurboModuleInterop();

if (!shouldCreateLegacyModules) {
return nullptr;
}

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 @@ -311,21 +323,19 @@ std::shared_ptr<TurboModule> TurboModuleManager::getLegacyModule(
}

void TurboModuleManager::installJSIBindings(
jni::alias_ref<jhybridobject> javaPart,
bool shouldCreateLegacyModules) {
jni::alias_ref<jhybridobject> javaPart) {
auto cxxPart = javaPart->cthis();
if (cxxPart == nullptr || !cxxPart->jsCallInvoker_) {
return; // Runtime doesn't exist when attached to Chrome debugger.
}

cxxPart->runtimeExecutor_([javaPart = jni::make_global(javaPart),
shouldCreateLegacyModules](jsi::Runtime& runtime) {
TurboModuleBinding::install(
runtime,
createTurboModuleProvider(javaPart, &runtime),
shouldCreateLegacyModules ? createLegacyModuleProvider(javaPart)
: nullptr);
});
cxxPart->runtimeExecutor_(
[javaPart = jni::make_global(javaPart)](jsi::Runtime& runtime) {
TurboModuleBinding::install(
runtime,
createTurboModuleProvider(javaPart),
createLegacyModuleProvider(javaPart));
});
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,13 @@ class TurboModuleManager : public jni::HybridClass<TurboModuleManager> {
std::shared_ptr<NativeMethodCallInvoker> nativeMethodCallInvoker,
jni::alias_ref<TurboModuleManagerDelegate::javaobject> delegate);

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

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