diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboCxxModule.cpp b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboCxxModule.cpp deleted file mode 100644 index cdcb08d5962..00000000000 --- a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboCxxModule.cpp +++ /dev/null @@ -1,212 +0,0 @@ -/* - * 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 "TurboCxxModule.h" - -#include - -#include -#include -#include - -using namespace facebook; -using namespace facebook::xplat::module; - -namespace facebook::react { - -namespace { -CxxModule::Callback makeTurboCxxModuleCallback( - std::weak_ptr weakWrapper) { - return [weakWrapper, wrapperWasCalled = false]( - const std::vector& args) mutable { - if (wrapperWasCalled) { - LOG(FATAL) << "callback arg cannot be called more than once"; - } - - auto strongWrapper = weakWrapper.lock(); - if (!strongWrapper) { - return; - } - - strongWrapper->jsInvoker().invokeAsync( - [weakWrapper, args](jsi::Runtime& rt) { - auto strongWrapper2 = weakWrapper.lock(); - if (!strongWrapper2) { - return; - } - - std::vector innerArgs; - innerArgs.reserve(args.size()); - for (auto& a : args) { - innerArgs.push_back(jsi::valueFromDynamic(rt, a)); - } - strongWrapper2->callback().call( - rt, (const jsi::Value*)innerArgs.data(), innerArgs.size()); - - strongWrapper2->destroy(); - }); - - wrapperWasCalled = true; - }; -} -} // namespace - -TurboCxxModule::TurboCxxModule( - std::unique_ptr cxxModule, - std::shared_ptr jsInvoker) - : TurboModule(cxxModule->getName(), std::move(jsInvoker)), - cxxMethods_(cxxModule->getMethods()), - cxxModule_(std::move(cxxModule)) {} - -jsi::Value TurboCxxModule::create( - jsi::Runtime& runtime, - const jsi::PropNameID& propName) { - std::string propNameUtf8 = propName.utf8(runtime); - - if (propNameUtf8 == "getConstants") { - // This is special cased because `getConstants()` is already a part of - // CxxModule. - return jsi::Function::createFromHostFunction( - runtime, - propName, - 0, - [this]( - jsi::Runtime& rt, - const jsi::Value& /*thisVal*/, - const jsi::Value* /*args*/, - size_t /*count*/) { - jsi::Object result(rt); - auto constants = cxxModule_->getConstants(); - for (auto& pair : constants) { - result.setProperty( - rt, pair.first.c_str(), jsi::valueFromDynamic(rt, pair.second)); - } - return result; - }); - } else { - for (auto& method : cxxMethods_) { - if (method.name == propNameUtf8) { - return jsi::Function::createFromHostFunction( - runtime, - propName, - 0, - [this, propNameUtf8]( - jsi::Runtime& rt, - const jsi::Value& /*thisVal*/, - const jsi::Value* args, - size_t count) { - return invokeMethod(rt, propNameUtf8, args, count); - }); - } - } - } - - return jsi::Value::undefined(); -} - -std::vector TurboCxxModule::getPropertyNames( - jsi::Runtime& runtime) { - std::vector result; - result.reserve(cxxMethods_.size() + 1); - result.push_back(jsi::PropNameID::forUtf8(runtime, "getConstants")); - for (auto& cxxMethod : cxxMethods_) { - result.push_back(jsi::PropNameID::forUtf8(runtime, cxxMethod.name)); - } - return result; -} - -jsi::Value TurboCxxModule::invokeMethod( - jsi::Runtime& runtime, - const std::string& methodName, - const jsi::Value* args, - size_t count) { - auto it = cxxMethods_.begin(); - for (; it != cxxMethods_.end(); it++) { - auto method = *it; - if (method.name == methodName) { - break; - } - } - - if (it == cxxMethods_.end()) { - throw std::runtime_error( - "Function '" + methodName + "' cannot be found on cxxmodule: " + name_); - } - - auto method = *it; - - if (method.syncFunc) { - auto innerArgs = folly::dynamic::array(); - for (size_t i = 0; i < count; i++) { - innerArgs.push_back(jsi::dynamicFromValue(runtime, args[i])); - } - return jsi::valueFromDynamic( - runtime, method.syncFunc(std::move(innerArgs))); - } else if (method.func && !method.isPromise) { - // Async method. - CxxModule::Callback first; - CxxModule::Callback second; - - if (count < method.callbacks) { - throw std::invalid_argument( - "Expected " + std::to_string(method.callbacks) + - " callbacks, but only " + std::to_string(count) + - " parameters provided"); - } - - if (method.callbacks == 1) { - auto wrapper = CallbackWrapper::createWeak( - args[count - 1].getObject(runtime).getFunction(runtime), - runtime, - jsInvoker_); - first = makeTurboCxxModuleCallback(wrapper); - } else if (method.callbacks == 2) { - auto wrapper1 = CallbackWrapper::createWeak( - args[count - 2].getObject(runtime).getFunction(runtime), - runtime, - jsInvoker_); - auto wrapper2 = CallbackWrapper::createWeak( - args[count - 1].getObject(runtime).getFunction(runtime), - runtime, - jsInvoker_); - first = makeTurboCxxModuleCallback(wrapper1); - second = makeTurboCxxModuleCallback(wrapper2); - } - - auto innerArgs = folly::dynamic::array(); - for (size_t i = 0; i < count - method.callbacks; i++) { - innerArgs.push_back(jsi::dynamicFromValue(runtime, args[i])); - } - - method.func(std::move(innerArgs), first, second); - } else if (method.isPromise) { - return createPromiseAsJSIValue( - runtime, - [method, args, count, this]( - jsi::Runtime& rt, std::shared_ptr promise) { - auto resolveWrapper = CallbackWrapper::createWeak( - promise->resolve_.getFunction(rt), rt, jsInvoker_); - auto rejectWrapper = CallbackWrapper::createWeak( - promise->reject_.getFunction(rt), rt, jsInvoker_); - CxxModule::Callback resolve = - makeTurboCxxModuleCallback(resolveWrapper); - CxxModule::Callback reject = - makeTurboCxxModuleCallback(rejectWrapper); - - auto innerArgs = folly::dynamic::array(); - for (size_t i = 0; i < count; i++) { - innerArgs.push_back(jsi::dynamicFromValue(rt, args[i])); - } - - method.func(std::move(innerArgs), resolve, reject); - }); - } - - return jsi::Value::undefined(); -} - -} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboCxxModule.h b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboCxxModule.h deleted file mode 100644 index df3800bb1b9..00000000000 --- a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboCxxModule.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 - -#include - -#include "TurboModule.h" - -namespace facebook::react { - -/** - * A helper class to convert the legacy CxxModule instance to a TurboModule - * instance. This should be used only for migration purpose (to TurboModule), - * since it's not very performant due to a lot of back-and-forth value - * conversions between folly::dynamic and jsi::Value. - */ -class JSI_EXPORT TurboCxxModule : public TurboModule { - public: - TurboCxxModule(std::unique_ptr cxxModule, std::shared_ptr jsInvoker); - - facebook::jsi::Value create(facebook::jsi::Runtime &runtime, const facebook::jsi::PropNameID &propName) override; - - std::vector getPropertyNames(facebook::jsi::Runtime &runtime) override; - - jsi::Value invokeMethod(jsi::Runtime &runtime, const std::string &methodName, const jsi::Value *args, size_t count); - - private: - std::vector cxxMethods_; - std::unique_ptr cxxModule_; -}; - -} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h index 59e852c2e5f..fc622dfb526 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h +++ b/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h @@ -38,7 +38,6 @@ enum TurboModuleMethodValueKind { */ TurboModuleMethodValueKind getTurboModuleMethodValueKind(jsi::Runtime &rt, const jsi::Value *value); -class TurboCxxModule; class TurboModuleBinding; /** diff --git a/packages/rn-tester/js/examples/TurboModule/TurboCxxModuleExample.js b/packages/rn-tester/js/examples/TurboModule/TurboCxxModuleExample.js deleted file mode 100644 index 65771081a17..00000000000 --- a/packages/rn-tester/js/examples/TurboModule/TurboCxxModuleExample.js +++ /dev/null @@ -1,29 +0,0 @@ -/** - * 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. - * - * @flow - * @format - */ - -'use strict'; - -import type {RNTesterModuleExample} from '../../types/RNTesterTypes'; - -const NativeCxxModuleExampleExample = require('./NativeCxxModuleExampleExample'); -const React = require('react'); - -exports.displayName = (undefined: ?string); -exports.title = 'Cxx TurboModule'; -exports.category = 'Basic'; -exports.description = 'Usage of Cxx TurboModule'; -exports.examples = [ - { - title: 'TurboCxxModuleExample', - render: function (): React.MixedElement { - return ; - }, - }, -] as Array; diff --git a/packages/rn-tester/js/utils/RNTesterList.android.js b/packages/rn-tester/js/utils/RNTesterList.android.js index 0b89ec8703a..5c4536e8b55 100644 --- a/packages/rn-tester/js/utils/RNTesterList.android.js +++ b/packages/rn-tester/js/utils/RNTesterList.android.js @@ -363,11 +363,6 @@ const APIs: Array = ([ key: 'LegacyModuleExample', module: require('../examples/TurboModule/LegacyModuleExample'), }, - { - key: 'TurboCxxModuleExample', - category: 'Basic', - module: require('../examples/TurboModule/TurboCxxModuleExample'), - }, // Basic check to detect the availability of the IntersectionObserver API. // $FlowExpectedError[cannot-resolve-name] ...(typeof IntersectionObserver === 'function' diff --git a/packages/rn-tester/js/utils/RNTesterList.ios.js b/packages/rn-tester/js/utils/RNTesterList.ios.js index 3d569a50a1d..342c7e028e1 100644 --- a/packages/rn-tester/js/utils/RNTesterList.ios.js +++ b/packages/rn-tester/js/utils/RNTesterList.ios.js @@ -326,10 +326,6 @@ const APIs: Array = ([ key: 'LegacyModuleExample', module: require('../examples/TurboModule/LegacyModuleExample'), }, - { - key: 'TurboCxxModuleExample', - module: require('../examples/TurboModule/TurboCxxModuleExample'), - }, { key: 'VibrationExample', module: require('../examples/Vibration/VibrationExample'), diff --git a/scripts/cxx-api/ReactNativeCPP.api b/scripts/cxx-api/ReactNativeCPP.api index ca72b926a1a..b745f444de1 100644 --- a/scripts/cxx-api/ReactNativeCPP.api +++ b/scripts/cxx-api/ReactNativeCPP.api @@ -16240,28 +16240,6 @@ void registerCxxModuleToGlobalModuleMap( std::shared_ptr jsInvoker)> moduleProviderFunc); } // namespace facebook::react -/// @src {packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboCxxModule.h}: -/// @dep {packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h} - -namespace facebook::react { -class JSI_EXPORT TurboCxxModule : public TurboModule { - public: - TurboCxxModule( - std::unique_ptr cxxModule, - std::shared_ptr jsInvoker); - facebook::jsi::Value create( - facebook::jsi::Runtime& runtime, - const facebook::jsi::PropNameID& propName) override; - std::vector getPropertyNames( - facebook::jsi::Runtime& runtime) override; - jsi::Value invokeMethod( - jsi::Runtime& runtime, - const std::string& methodName, - const jsi::Value* args, - size_t count); -}; -} // namespace facebook::react - /// @src {packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h}: namespace facebook::react { enum TurboModuleMethodValueKind {