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
This commit is contained in:
Pieter De Baets
2025-01-07 07:40:40 -08:00
committed by Facebook GitHub Bot
parent 65bda54232
commit dbb75e36dc
4 changed files with 30 additions and 166 deletions
@@ -7,28 +7,10 @@
#include "SurfaceRegistryBinding.h"
#include <cxxreact/TraceSection.h>
#include <react/renderer/uimanager/bindingUtils.h>
#include <react/renderer/uimanager/primitives.h>
#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
@@ -19,8 +19,6 @@
#include <utility>
#include "bindingUtils.h"
namespace facebook::react {
void UIManagerBinding::createAndInstallIfNeeded(
@@ -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 <glog/logging.h>
#include <react/debug/react_native_assert.h>
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<jsi::Value> 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
@@ -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 <jsi/jsi.h>
namespace facebook::react {
jsi::Value callMethodOfModule(
jsi::Runtime& runtime,
const std::string& moduleName,
const std::string& methodName,
std::initializer_list<jsi::Value> args);
} // namespace facebook::react