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 @@ -145,5 +145,7 @@ class JSI_EXPORT TurboModule : public jsi::HostObject {
* given a name.
*/
using TurboModuleProviderFunctionType = 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 @@ -33,24 +33,30 @@ class TurboModuleBinding final {
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