1313
1414namespace facebook ::react {
1515
16+ namespace {
17+
18+ void throwIfBridgeless (
19+ jsi::Runtime &runtime,
20+ jsi::Object &global,
21+ const char *methodName) {
22+ auto isBridgeless = global.getProperty (runtime, " RN$Bridgeless" );
23+ if (isBridgeless.isBool () && isBridgeless.asBool ()) {
24+ throw std::runtime_error (
25+ " SurfaceRegistryBinding::" + std::string (methodName) +
26+ " failed. Global was not installed." );
27+ }
28+ }
29+
30+ } // namespace
31+
1632void SurfaceRegistryBinding::startSurface (
1733 jsi::Runtime &runtime,
1834 SurfaceId surfaceId,
@@ -26,43 +42,24 @@ void SurfaceRegistryBinding::startSurface(
2642 parameters[" fabric" ] = true ;
2743
2844 auto global = runtime.global ();
29- auto isBridgeless = global.hasProperty (runtime, " RN$Bridgeless" ) &&
30- global.getProperty (runtime, " RN$Bridgeless" ).asBool ();
31-
32- if (isBridgeless) {
33- if (!global.hasProperty (runtime, " RN$SurfaceRegistry" )) {
34- throw std::runtime_error (
35- " SurfaceRegistryBinding::startSurface: Failed to start Surface \" " +
36- moduleName + " \" . global.RN$SurfaceRegistry was not installed." );
37- }
38-
39- auto registry = global.getPropertyAsObject (runtime, " RN$SurfaceRegistry" );
40- auto method = registry.getPropertyAsFunction (runtime, " renderSurface" );
45+ auto registry = global.getProperty (runtime, " RN$AppRegistry" );
46+ if (registry.isObject ()) {
47+ auto method = std::move (registry).asObject (runtime).getPropertyAsFunction (
48+ runtime, " runApplication" );
4149 method.call (
4250 runtime,
4351 {jsi::String::createFromUtf8 (runtime, moduleName),
4452 jsi::valueFromDynamic (runtime, parameters),
4553 jsi::Value (runtime, displayModeToInt (displayMode))});
4654 } else {
47- if (moduleName != " LogBox" &&
48- global.hasProperty (runtime, " RN$SurfaceRegistry" )) {
49- auto registry = global.getPropertyAsObject (runtime, " RN$SurfaceRegistry" );
50- auto method = registry.getPropertyAsFunction (runtime, " renderSurface" );
51-
52- method.call (
53- runtime,
54- {jsi::String::createFromUtf8 (runtime, moduleName),
55- jsi::valueFromDynamic (runtime, parameters),
56- jsi::Value (runtime, displayModeToInt (displayMode))});
57- } else {
58- callMethodOfModule (
59- runtime,
60- " AppRegistry" ,
61- " runApplication" ,
62- {jsi::String::createFromUtf8 (runtime, moduleName),
63- jsi::valueFromDynamic (runtime, parameters),
64- jsi::Value (runtime, displayModeToInt (displayMode))});
65- }
55+ throwIfBridgeless (runtime, global, " startSurface" );
56+ callMethodOfModule (
57+ runtime,
58+ " AppRegistry" ,
59+ " runApplication" ,
60+ {jsi::String::createFromUtf8 (runtime, moduleName),
61+ jsi::valueFromDynamic (runtime, parameters),
62+ jsi::Value (runtime, displayModeToInt (displayMode))});
6663 }
6764}
6865
@@ -79,63 +76,40 @@ void SurfaceRegistryBinding::setSurfaceProps(
7976 parameters[" fabric" ] = true ;
8077
8178 auto global = runtime.global ();
82- auto isBridgeless = global.hasProperty (runtime, " RN$Bridgeless" ) &&
83- global.getProperty (runtime, " RN$Bridgeless" ).asBool ();
84-
85- if (isBridgeless) {
86- if (!global.hasProperty (runtime, " RN$SurfaceRegistry" )) {
87- throw std::runtime_error (
88- " SurfaceRegistryBinding::setSurfaceProps: Failed to set Surface props for \" " +
89- moduleName + " \" . global.RN$SurfaceRegistry was not installed." );
90- }
91-
92- auto registry = global.getPropertyAsObject (runtime, " RN$SurfaceRegistry" );
93- auto method = registry.getPropertyAsFunction (runtime, " setSurfaceProps" );
94-
79+ auto registry = global.getProperty (runtime, " RN$AppRegistry" );
80+ if (registry.isObject ()) {
81+ auto method = std::move (registry).asObject (runtime).getPropertyAsFunction (
82+ runtime, " setSurfaceProps" );
9583 method.call (
9684 runtime,
9785 {jsi::String::createFromUtf8 (runtime, moduleName),
9886 jsi::valueFromDynamic (runtime, parameters),
9987 jsi::Value (runtime, displayModeToInt (displayMode))});
10088 } else {
101- if (moduleName != " LogBox" &&
102- global.hasProperty (runtime, " RN$SurfaceRegistry" )) {
103- auto registry = global.getPropertyAsObject (runtime, " RN$SurfaceRegistry" );
104- auto method = registry.getPropertyAsFunction (runtime, " setSurfaceProps" );
105-
106- method.call (
107- runtime,
108- {jsi::String::createFromUtf8 (runtime, moduleName),
109- jsi::valueFromDynamic (runtime, parameters),
110- jsi::Value (runtime, displayModeToInt (displayMode))});
111- } else {
112- callMethodOfModule (
113- runtime,
114- " AppRegistry" ,
115- " setSurfaceProps" ,
116- {jsi::String::createFromUtf8 (runtime, moduleName),
117- jsi::valueFromDynamic (runtime, parameters),
118- jsi::Value (runtime, displayModeToInt (displayMode))});
119- }
89+ throwIfBridgeless (runtime, global, " setSurfaceProps" );
90+ callMethodOfModule (
91+ runtime,
92+ " AppRegistry" ,
93+ " setSurfaceProps" ,
94+ {jsi::String::createFromUtf8 (runtime, moduleName),
95+ jsi::valueFromDynamic (runtime, parameters),
96+ jsi::Value (runtime, displayModeToInt (displayMode))});
12097 }
12198}
12299
123100void SurfaceRegistryBinding::stopSurface (
124101 jsi::Runtime &runtime,
125102 SurfaceId surfaceId) {
126103 auto global = runtime.global ();
127- auto isBridgeless = global.hasProperty (runtime, " RN$Bridgeless" ) &&
128- global.getProperty (runtime, " RN$Bridgeless" ).asBool ();
129-
130- if (isBridgeless) {
131- if (!global.hasProperty (runtime, " RN$stopSurface" )) {
132- // ReactFabric module has not been loaded yet; there's no surface to stop.
133- return ;
134- }
135- // Bridgeless mode uses a custom JSI binding instead of callable module.
136- global.getPropertyAsFunction (runtime, " RN$stopSurface" )
104+ auto stopFunction = global.getProperty (runtime, " RN$stopSurface" );
105+ if (stopFunction.isObject () &&
106+ stopFunction.asObject (runtime).isFunction (runtime)) {
107+ std::move (stopFunction)
108+ .asObject (runtime)
109+ .asFunction (runtime)
137110 .call (runtime, {jsi::Value{surfaceId}});
138111 } else {
112+ throwIfBridgeless (runtime, global, " stopSurface" );
139113 callMethodOfModule (
140114 runtime,
141115 " ReactFabric" ,
0 commit comments