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 @@ -52,15 +52,14 @@ public class TurboModuleManager(
@DoNotStrip
private val mHybridData: HybridData =
initHybrid(
runtimeExecutor,
jsCallInvokerHolder as CallInvokerHolderImpl,
nativeMethodCallInvokerHolder as NativeMethodCallInvokerHolderImpl,
delegate,
)

init {

installJSIBindings(shouldEnableLegacyModuleInterop())
dispatchJSBindingInstall(runtimeExecutor)

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

Expand All @@ -72,7 +71,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 +92,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 @@ -281,13 +277,12 @@ public class TurboModuleManager(
}

private external fun initHybrid(
runtimeExecutor: RuntimeExecutor,
jsCallInvokerHolder: CallInvokerHolderImpl,
nativeMethodCallInvoker: NativeMethodCallInvokerHolderImpl,
tmmDelegate: TurboModuleManagerDelegate?,
): HybridData

private external fun installJSIBindings(shouldCreateLegacyModules: Boolean)
private external fun dispatchJSBindingInstall(runtimeExecutor: RuntimeExecutor)

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 @@ -94,24 +95,20 @@ class JMethodDescriptor : public jni::JavaClass<JMethodDescriptor> {
} // namespace

TurboModuleManager::TurboModuleManager(
RuntimeExecutor runtimeExecutor,
std::shared_ptr<CallInvoker> jsCallInvoker,
std::shared_ptr<NativeMethodCallInvoker> nativeMethodCallInvoker,
jni::alias_ref<TurboModuleManagerDelegate::javaobject> delegate)
: runtimeExecutor_(std::move(runtimeExecutor)),
jsCallInvoker_(std::move(jsCallInvoker)),
: jsCallInvoker_(std::move(jsCallInvoker)),
nativeMethodCallInvoker_(std::move(nativeMethodCallInvoker)),
delegate_(jni::make_global(delegate)) {}

jni::local_ref<TurboModuleManager::jhybriddata> TurboModuleManager::initHybrid(
jni::alias_ref<jhybridobject> /* unused */,
jni::alias_ref<JRuntimeExecutor::javaobject> runtimeExecutor,
jni::alias_ref<CallInvokerHolder::javaobject> jsCallInvokerHolder,
jni::alias_ref<NativeMethodCallInvokerHolder::javaobject>
nativeMethodCallInvokerHolder,
jni::alias_ref<TurboModuleManagerDelegate::javaobject> delegate) {
return makeCxxInstance(
runtimeExecutor->cthis()->get(),
jsCallInvokerHolder->cthis()->getCallInvoker(),
nativeMethodCallInvokerHolder->cthis()->getNativeMethodCallInvoker(),
delegate);
Expand All @@ -121,14 +118,16 @@ void TurboModuleManager::registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", TurboModuleManager::initHybrid),
makeNativeMethod(
"installJSIBindings", TurboModuleManager::installJSIBindings),
"dispatchJSBindingInstall",
TurboModuleManager::dispatchJSBindingInstall),
});
}

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 +139,7 @@ TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider(
return nullptr;
}

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

Expand Down Expand Up @@ -223,9 +222,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 @@ -310,22 +319,30 @@ std::shared_ptr<TurboModule> TurboModuleManager::getLegacyModule(
return nullptr;
}

void TurboModuleManager::installJSIBindings(
void TurboModuleManager::installJSBindings(
jsi::Runtime& runtime,
jni::alias_ref<jhybridobject> javaPart) {
TurboModuleBinding::install(
runtime,
createTurboModuleProvider(javaPart),
createLegacyModuleProvider(javaPart));
}

void TurboModuleManager::dispatchJSBindingInstall(
jni::alias_ref<jhybridobject> javaPart,
bool shouldCreateLegacyModules) {
jni::alias_ref<JRuntimeExecutor::javaobject> runtimeExecutor) {
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);
});
runtimeExecutor->cthis()->get()(
[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 @@ -27,7 +27,6 @@ class TurboModuleManager : public jni::HybridClass<TurboModuleManager> {
static auto constexpr kJavaDescriptor = "Lcom/facebook/react/internal/turbomodule/core/TurboModuleManager;";
static jni::local_ref<jhybriddata> initHybrid(
jni::alias_ref<jhybridobject> /* unused */,
jni::alias_ref<JRuntimeExecutor::javaobject> runtimeExecutor,
jni::alias_ref<CallInvokerHolder::javaobject> jsCallInvokerHolder,
jni::alias_ref<NativeMethodCallInvokerHolder::javaobject> nativeMethodCallInvokerHolder,
jni::alias_ref<TurboModuleManagerDelegate::javaobject> delegate);
Expand All @@ -52,20 +51,20 @@ class TurboModuleManager : public jni::HybridClass<TurboModuleManager> {
ModuleCache legacyModuleCache_;

explicit TurboModuleManager(
RuntimeExecutor runtimeExecutor,
std::shared_ptr<CallInvoker> jsCallInvoker,
std::shared_ptr<NativeMethodCallInvoker> nativeMethodCallInvoker,
jni::alias_ref<TurboModuleManagerDelegate::javaobject> delegate);

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

static TurboModuleProviderFunctionType createTurboModuleProvider(
static void installJSBindings(jsi::Runtime &runtime, jni::alias_ref<jhybridobject> javaPart);
static void dispatchJSBindingInstall(
jni::alias_ref<jhybridobject> javaPart,
jsi::Runtime *runtime);
jni::alias_ref<JRuntimeExecutor::javaobject> runtimeExecutor);

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,12 @@ 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.
*/
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,41 @@ class TurboModuleBinding final {
/*
* Installs TurboModuleBinding into JavaScript runtime.
* Thread synchronization must be enforced externally.
*
* @deprecated Use the overload that takes
* TurboModuleProviderFunctionTypeWithRuntime instead.
*/
[[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
Loading
Loading