From 6429d8ac91acbec22258824a5d21ba50f4c68d40 Mon Sep 17 00:00:00 2001 From: Zeya Peng Date: Thu, 20 Mar 2025 07:11:34 -0700 Subject: [PATCH] TurboModuleWithJSIBindings (#50106) Summary: ## Changelog: [General] [Added] - Create TurboModuleWithJSIBindings interface So c++ TurboModules can initialize some private members with reference to `jsi::Runtime` Reviewed By: lenaic Differential Revision: D71396842 --- .../ReactCommon/TurboModuleManager.cpp | 2 ++ .../TurboModuleWithJSIBindings.cpp | 23 +++++++++++++++ .../ReactCommon/TurboModuleWithJSIBindings.h | 29 +++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleWithJSIBindings.cpp create mode 100644 packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleWithJSIBindings.h diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp index 6a036308e4d5c6..298e37d9209aa5 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include namespace facebook::react { @@ -165,6 +166,7 @@ std::shared_ptr TurboModuleManager::getTurboModule( auto cxxModule = cxxDelegate->getTurboModule(name, jsCallInvoker_); if (cxxModule) { + TurboModuleWithJSIBindings::installJSIBindings(cxxModule, runtime); turboModuleCache_.insert({name, cxxModule}); return cxxModule; } diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleWithJSIBindings.cpp b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleWithJSIBindings.cpp new file mode 100644 index 00000000000000..5940a5429a390e --- /dev/null +++ b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleWithJSIBindings.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include "TurboModuleWithJSIBindings.h" + +#include + +namespace facebook::react { + +/* static */ void TurboModuleWithJSIBindings::installJSIBindings( + const std::shared_ptr& cxxModule, + jsi::Runtime& runtime) { + if (auto* cxxModuleWithJSIBindings = + dynamic_cast(cxxModule.get())) { + cxxModuleWithJSIBindings->installJSIBindingsWithRuntime(runtime); + } +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleWithJSIBindings.h b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleWithJSIBindings.h new file mode 100644 index 00000000000000..c7c912a6ad7bf3 --- /dev/null +++ b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleWithJSIBindings.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include +#include + +namespace facebook::react { + +class TurboModule; + +class TurboModuleWithJSIBindings { + public: + virtual ~TurboModuleWithJSIBindings() = default; + + static void installJSIBindings( + const std::shared_ptr& cxxModule, + jsi::Runtime& runtime); + + private: + virtual void installJSIBindingsWithRuntime(jsi::Runtime& runtime) = 0; +}; + +} // namespace facebook::react