From aa28ea01d22d03ba9beb8bcbd4d488ba8d7b251f Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Tue, 14 Mar 2023 21:16:22 -0700 Subject: [PATCH] Refactor: Simplify signature of TurboModuleBinding::getModule Summary: Previously, the signature of TurboModuleBinding::getModule was https://www.internalfb.com/code/fbsource/[839b8756fb58bf8cfa38a7d9f90b4b2b99889a37]/xplat/jsi/jsi/jsi.h?lines=111-112 **Problem:** TurboModuleBinding::getModule couldn't be called from [global.nativeModuleProxy](https://www.internalfb.com/code/fbsource/[cacaf79996c24c52fcedc90933b77dcf3411cf54]/xplat/ReactNative/venice/ReactInstance.cpp?lines=25-46) (i.e: jsi::HostObject::get), where only the prop name was available: https://www.internalfb.com/code/fbsource/[839b8756fb58bf8cfa38a7d9f90b4b2b99889a37]/xplat/jsi/jsi/jsi.h?lines=133 ## Changes This diff simplifies the signature of TurboModuleBinding::getModule to accept only what it needs: a jsi::Runtime, and moduleName This way, TurboModuleBinding::getModule can be called from: - jsi::HostFunction (for [global.__turboModuleProxy](https://www.internalfb.com/code/fbsource/[cc6106d532afd3f179b586d6acb4e99edb09bb96]/xplat/js/react-native-github/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp?lines=34-48)) - **new:** jsi::HostObject::get (for [global.nativeModuleProxy](https://www.internalfb.com/code/fbsource/[cacaf79996c24c52fcedc90933b77dcf3411cf54]/xplat/ReactNative/venice/ReactInstance.cpp?lines=25-46)). Changelog: [Internal] Reviewed By: javache, cortinico Differential Revision: D43993198 fbshipit-source-id: e1207a129b1033f96d2753c88d372bb7eb4364f3 --- .../core/ReactCommon/TurboModuleBinding.cpp | 17 +++++++---------- .../core/ReactCommon/TurboModuleBinding.h | 7 ++----- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp b/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp index b3f1dbdf2c4..94da446666f 100644 --- a/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp +++ b/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp @@ -44,7 +44,12 @@ void TurboModuleBinding::install( const jsi::Value &thisVal, const jsi::Value *args, size_t count) { - return binding.getModule(rt, thisVal, args, count); + if (count < 1) { + throw std::invalid_argument( + "__turboModuleProxy must be called with at least 1 argument"); + } + std::string moduleName = args[0].getString(rt).utf8(rt); + return binding.getModule(rt, moduleName); })); } @@ -54,15 +59,7 @@ TurboModuleBinding::~TurboModuleBinding() { jsi::Value TurboModuleBinding::getModule( jsi::Runtime &runtime, - const jsi::Value &thisVal, - const jsi::Value *args, - size_t count) const { - if (count < 1) { - throw std::invalid_argument( - "__turboModuleProxy must be called with at least 1 argument"); - } - std::string moduleName = args[0].getString(runtime).utf8(runtime); - + const std::string &moduleName) const { std::shared_ptr module; { SystraceSection s( diff --git a/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.h b/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.h index 27da12b506f..dcf8eaaa2d8 100644 --- a/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.h +++ b/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.h @@ -45,11 +45,8 @@ class TurboModuleBinding { * A lookup function exposed to JS to get an instance of a TurboModule * for the given name. */ - jsi::Value getModule( - jsi::Runtime &runtime, - const jsi::Value &thisVal, - const jsi::Value *args, - size_t count) const; + jsi::Value getModule(jsi::Runtime &runtime, const std::string &moduleName) + const; TurboModuleProviderFunctionType moduleProvider_; TurboModuleBindingMode bindingMode_;