From dbb75e36dce60b45dadc0dfe6b9f7082c8d0e11e Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Tue, 7 Jan 2025 07:40:40 -0800 Subject: [PATCH] Always use AppRegistry globals in SurfaceRegistryBinding (#48336) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48336 `RN$AppRegistry` and `RN$stopSurface` are always set on the init path, regardless of bridgeless or not, so we can remove the fallback path and cleanup this code. Changelog: [Internal] Reviewed By: rshest Differential Revision: D67342498 fbshipit-source-id: db47e52fee5075f11258364d82474579d2bb21f4 --- .../uimanager/SurfaceRegistryBinding.cpp | 95 ++++++------------- .../renderer/uimanager/UIManagerBinding.cpp | 2 - .../react/renderer/uimanager/bindingUtils.cpp | 79 --------------- .../react/renderer/uimanager/bindingUtils.h | 20 ---- 4 files changed, 30 insertions(+), 166 deletions(-) delete mode 100644 packages/react-native/ReactCommon/react/renderer/uimanager/bindingUtils.cpp delete mode 100644 packages/react-native/ReactCommon/react/renderer/uimanager/bindingUtils.h diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/SurfaceRegistryBinding.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/SurfaceRegistryBinding.cpp index 5b9d62bea6c..eeed20b043b 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/SurfaceRegistryBinding.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/SurfaceRegistryBinding.cpp @@ -7,28 +7,10 @@ #include "SurfaceRegistryBinding.h" #include -#include #include -#include "bindingUtils.h" namespace facebook::react { -namespace { - -void throwIfBridgeless( - jsi::Runtime& runtime, - jsi::Object& global, - const char* methodName) { - auto isBridgeless = global.getProperty(runtime, "RN$Bridgeless"); - if (isBridgeless.isBool() && isBridgeless.asBool()) { - throw std::runtime_error( - "SurfaceRegistryBinding::" + std::string(methodName) + - " failed. Global was not installed."); - } -} - -} // namespace - void SurfaceRegistryBinding::startSurface( jsi::Runtime& runtime, SurfaceId surfaceId, @@ -44,24 +26,17 @@ void SurfaceRegistryBinding::startSurface( auto global = runtime.global(); auto registry = global.getProperty(runtime, "RN$AppRegistry"); - if (registry.isObject()) { - auto method = std::move(registry).asObject(runtime).getPropertyAsFunction( - runtime, "runApplication"); - method.call( - runtime, - {jsi::String::createFromUtf8(runtime, moduleName), - std::move(parameters), - jsi::Value(runtime, displayModeToInt(displayMode))}); - } else { - throwIfBridgeless(runtime, global, "startSurface"); - callMethodOfModule( - runtime, - "AppRegistry", - "runApplication", - {jsi::String::createFromUtf8(runtime, moduleName), - std::move(parameters), - jsi::Value(runtime, displayModeToInt(displayMode))}); + if (!registry.isObject()) { + throw std::runtime_error( + "SurfaceRegistryBinding::startSurface failed. Global was not installed."); } + auto method = std::move(registry).asObject(runtime).getPropertyAsFunction( + runtime, "runApplication"); + method.call( + runtime, + {jsi::String::createFromUtf8(runtime, moduleName), + std::move(parameters), + jsi::Value(runtime, displayModeToInt(displayMode))}); } void SurfaceRegistryBinding::setSurfaceProps( @@ -79,24 +54,18 @@ void SurfaceRegistryBinding::setSurfaceProps( auto global = runtime.global(); auto registry = global.getProperty(runtime, "RN$AppRegistry"); - if (registry.isObject()) { - auto method = std::move(registry).asObject(runtime).getPropertyAsFunction( - runtime, "setSurfaceProps"); - method.call( - runtime, - {jsi::String::createFromUtf8(runtime, moduleName), - std::move(parameters), - jsi::Value(runtime, displayModeToInt(displayMode))}); - } else { - throwIfBridgeless(runtime, global, "setSurfaceProps"); - callMethodOfModule( - runtime, - "AppRegistry", - "setSurfaceProps", - {jsi::String::createFromUtf8(runtime, moduleName), - std::move(parameters), - jsi::Value(runtime, displayModeToInt(displayMode))}); + if (!registry.isObject()) { + throw std::runtime_error( + "SurfaceRegistryBinding::setSurfaceProps failed. Global was not installed."); } + + auto method = std::move(registry).asObject(runtime).getPropertyAsFunction( + runtime, "setSurfaceProps"); + method.call( + runtime, + {jsi::String::createFromUtf8(runtime, moduleName), + std::move(parameters), + jsi::Value(runtime, displayModeToInt(displayMode))}); } void SurfaceRegistryBinding::stopSurface( @@ -104,20 +73,16 @@ void SurfaceRegistryBinding::stopSurface( SurfaceId surfaceId) { auto global = runtime.global(); auto stopFunction = global.getProperty(runtime, "RN$stopSurface"); - if (stopFunction.isObject() && - stopFunction.asObject(runtime).isFunction(runtime)) { - std::move(stopFunction) - .asObject(runtime) - .asFunction(runtime) - .call(runtime, {jsi::Value{surfaceId}}); - } else { - throwIfBridgeless(runtime, global, "stopSurface"); - callMethodOfModule( - runtime, - "ReactFabric", - "unmountComponentAtNode", - {jsi::Value{surfaceId}}); + if (!stopFunction.isObject() || + !stopFunction.asObject(runtime).isFunction(runtime)) { + throw std::runtime_error( + "SurfaceRegistryBinding::stopSurface failed. Global was not installed."); } + + std::move(stopFunction) + .asObject(runtime) + .asFunction(runtime) + .call(runtime, {jsi::Value{surfaceId}}); } } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp index ed3215bb65b..e43f94b645e 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp @@ -19,8 +19,6 @@ #include -#include "bindingUtils.h" - namespace facebook::react { void UIManagerBinding::createAndInstallIfNeeded( diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/bindingUtils.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/bindingUtils.cpp deleted file mode 100644 index 3aa55476294..00000000000 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/bindingUtils.cpp +++ /dev/null @@ -1,79 +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 "bindingUtils.h" - -#include -#include - -namespace facebook::react { - -static jsi::Value getModule( - jsi::Runtime& runtime, - const std::string& moduleName) { - auto batchedBridge = - runtime.global().getPropertyAsObject(runtime, "__fbBatchedBridge"); - auto getCallableModule = - batchedBridge.getPropertyAsFunction(runtime, "getCallableModule"); - auto moduleAsValue = getCallableModule.callWithThis( - runtime, - batchedBridge, - {jsi::String::createFromUtf8(runtime, moduleName)}); - if (!moduleAsValue.isObject()) { - LOG(ERROR) << "getModule of " << moduleName << " is not an object"; - } - react_native_assert(moduleAsValue.isObject()); - return moduleAsValue; -} - -static bool checkBatchedBridgeIsActive(jsi::Runtime& runtime) { - if (!runtime.global().hasProperty(runtime, "__fbBatchedBridge")) { - LOG(ERROR) - << "getPropertyAsObject: property '__fbBatchedBridge' is undefined, expected an Object"; - return false; - } - return true; -} - -static bool checkGetCallableModuleIsActive(jsi::Runtime& runtime) { - if (!checkBatchedBridgeIsActive(runtime)) { - return false; - } - auto batchedBridge = - runtime.global().getPropertyAsObject(runtime, "__fbBatchedBridge"); - if (!batchedBridge.hasProperty(runtime, "getCallableModule")) { - LOG(ERROR) - << "getPropertyAsFunction: function 'getCallableModule' is undefined, expected a Function"; - return false; - } - return true; -} - -jsi::Value callMethodOfModule( - jsi::Runtime& runtime, - const std::string& moduleName, - const std::string& methodName, - std::initializer_list args) { - if (checkGetCallableModuleIsActive(runtime)) { - auto module = getModule(runtime, moduleName); - if (module.isObject()) { - jsi::Object object = module.asObject(runtime); - react_native_assert(object.hasProperty(runtime, methodName.c_str())); - if (object.hasProperty(runtime, methodName.c_str())) { - auto method = object.getPropertyAsFunction(runtime, methodName.c_str()); - return method.callWithThis(runtime, object, args); - } else { - LOG(ERROR) << "getPropertyAsFunction: property '" << methodName - << "' is undefined, expected a Function"; - } - } - } - - return jsi::Value::undefined(); -} - -} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/bindingUtils.h b/packages/react-native/ReactCommon/react/renderer/uimanager/bindingUtils.h deleted file mode 100644 index 60afbfff9b0..00000000000 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/bindingUtils.h +++ /dev/null @@ -1,20 +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 - -namespace facebook::react { - -jsi::Value callMethodOfModule( - jsi::Runtime& runtime, - const std::string& moduleName, - const std::string& methodName, - std::initializer_list args); - -} // namespace facebook::react