Skip to content

Commit 28da7f9

Browse files
Share re-defined defineReadOnlyGlobal(...) (#42801)
Summary: Pull Request resolved: #42801 Fixes some tech debug Changelog: [Internal] [Fixed] Share re-defined defineReadOnlyGlobal(...) Reviewed By: javache Differential Revision: D53340274 fbshipit-source-id: 1f8021eae7403675bc045c8031af09718830c2fe
1 parent 046f664 commit 28da7f9

10 files changed

Lines changed: 87 additions & 70 deletions

File tree

packages/react-native/ReactCommon/ReactCommon.podspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ Pod::Spec.new do |s|
7171

7272
ss.subspec "core" do |sss|
7373
sss.source_files = "react/nativemodule/core/ReactCommon/**/*.{cpp,h}"
74-
sss.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_TARGET_SRCROOT)/ReactCommon\" \"$(PODS_CONFIGURATION_BUILD_DIR)/React-debug/React_debug.framework/Headers\"" }
74+
sss.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_TARGET_SRCROOT)/ReactCommon\" \"$(PODS_CONFIGURATION_BUILD_DIR)/React-debug/React_debug.framework/Headers\" \"$(PODS_CONFIGURATION_BUILD_DIR)/React-utils/React_utils.framework/Headers\"" }
7575
sss.dependency "React-debug", version
76+
sss.dependency "React-utils", version
7677
end
7778
end
7879
end

packages/react-native/ReactCommon/react/nativemodule/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ target_link_libraries(react_nativemodule_core
3535
jsi
3636
react_bridging
3737
react_debug
38+
react_utils
3839
reactperflogger
3940
reactnativejni)

packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
#include "TurboModuleBinding.h"
99

10+
#include <cxxreact/SystraceSection.h>
11+
#include <react/utils/jsi.h>
1012
#include <stdexcept>
1113
#include <string>
1214

13-
#include <cxxreact/SystraceSection.h>
14-
1515
using namespace facebook;
1616

1717
namespace facebook::react {
@@ -62,33 +62,6 @@ class BridgelessNativeModuleProxy : public jsi::HostObject {
6262
}
6363
};
6464

65-
// TODO(148359183): Merge this with the Bridgeless defineReadOnlyGlobal util
66-
static void defineReadOnlyGlobal(
67-
jsi::Runtime& runtime,
68-
std::string propName,
69-
jsi::Value&& value) {
70-
if (runtime.global().hasProperty(runtime, propName.c_str())) {
71-
throw jsi::JSError(
72-
runtime,
73-
"Tried to redefine read-only global \"" + propName +
74-
"\", but read-only globals can only be defined once.");
75-
}
76-
jsi::Object jsObject =
77-
runtime.global().getProperty(runtime, "Object").asObject(runtime);
78-
jsi::Function defineProperty = jsObject.getProperty(runtime, "defineProperty")
79-
.asObject(runtime)
80-
.asFunction(runtime);
81-
82-
jsi::Object descriptor = jsi::Object(runtime);
83-
descriptor.setProperty(runtime, "value", std::move(value));
84-
defineProperty.callWithThis(
85-
runtime,
86-
jsObject,
87-
runtime.global(),
88-
jsi::String::createFromUtf8(runtime, propName),
89-
descriptor);
90-
}
91-
9265
/**
9366
* Public API to install the TurboModule system.
9467
*/

packages/react-native/ReactCommon/react/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ target_link_libraries(
3838
react_utils
3939
jsinspector
4040
react_featureflags
41+
react_utils
4142
)

packages/react-native/ReactCommon/react/runtime/React-RuntimeHermes.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ Pod::Spec.new do |s|
4747
s.dependency folly_dep_name, folly_version
4848
s.dependency "React-nativeconfig"
4949
s.dependency "React-jsitracing"
50-
s.dependency "React-utils"
5150
s.dependency "React-jsi"
51+
s.dependency "React-utils"
5252
s.dependency "React-RuntimeCore"
5353
s.dependency "React-featureflags"
5454
add_dependency(s, "React-jsinspector", :framework_name => 'jsinspector_modern')

packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
#include <cxxreact/ErrorUtils.h>
1111
#include <cxxreact/JSBigString.h>
1212
#include <cxxreact/JSExecutor.h>
13+
#include <cxxreact/ReactMarker.h>
1314
#include <cxxreact/SystraceSection.h>
1415
#include <glog/logging.h>
1516
#include <jsi/JSIDynamic.h>
1617
#include <jsi/instrumentation.h>
1718
#include <jsireact/JSIExecutor.h>
1819
#include <react/featureflags/ReactNativeFeatureFlags.h>
1920
#include <react/renderer/runtimescheduler/RuntimeSchedulerBinding.h>
20-
21-
#include <cxxreact/ReactMarker.h>
21+
#include <react/utils/jsi.h>
2222
#include <iostream>
2323
#include <tuple>
2424
#include <utility>
@@ -140,43 +140,6 @@ ReactInstance::getRuntimeScheduler() noexcept {
140140
return runtimeScheduler_;
141141
}
142142

143-
/**
144-
* Defines a property on the global object that is neither enumerable, nor
145-
* configurable, nor writable. This ensures that the private globals exposed by
146-
* ReactInstance cannot overwritten by third-party JavaScript code. It also
147-
* ensures that third-party JavaScript code unaware of these globals isn't able
148-
* to accidentally access them. In JavaScript, equivalent to:
149-
*
150-
* Object.defineProperty(global, propName, {
151-
* value: value
152-
* })
153-
*/
154-
static void defineReadOnlyGlobal(
155-
jsi::Runtime& runtime,
156-
std::string propName,
157-
jsi::Value&& value) {
158-
if (runtime.global().hasProperty(runtime, propName.c_str())) {
159-
throw jsi::JSError(
160-
runtime,
161-
"Tried to redefine read-only global \"" + propName +
162-
"\", but read-only globals can only be defined once.");
163-
}
164-
jsi::Object jsObject =
165-
runtime.global().getProperty(runtime, "Object").asObject(runtime);
166-
jsi::Function defineProperty = jsObject.getProperty(runtime, "defineProperty")
167-
.asObject(runtime)
168-
.asFunction(runtime);
169-
170-
jsi::Object descriptor = jsi::Object(runtime);
171-
descriptor.setProperty(runtime, "value", std::move(value));
172-
defineProperty.callWithThis(
173-
runtime,
174-
jsObject,
175-
runtime.global(),
176-
jsi::String::createFromUtf8(runtime, propName),
177-
descriptor);
178-
}
179-
180143
namespace {
181144

182145
// Copied from JSIExecutor.cpp

packages/react-native/ReactCommon/react/utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ target_include_directories(react_utils PUBLIC ${REACT_COMMON_DIR})
2222
target_link_libraries(react_utils
2323
glog
2424
glog_init
25+
jsireact
2526
react_debug)

packages/react-native/ReactCommon/react/utils/React-utils.podspec

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@ Pod::Spec.new do |s|
4949
end
5050

5151
s.dependency "RCT-Folly", folly_version
52+
s.dependency "React-jsi", version
5253
s.dependency "glog"
5354

55+
if ENV["USE_HERMES"] == nil || ENV["USE_HERMES"] == "1"
56+
s.dependency "hermes-engine"
57+
else
58+
s.dependency "React-jsc"
59+
end
60+
5461
add_dependency(s, "React-debug")
5562
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "jsi.h"
9+
10+
namespace facebook::react {
11+
12+
void defineReadOnlyGlobal(
13+
jsi::Runtime& runtime,
14+
const std::string& propName,
15+
jsi::Value&& value) {
16+
auto global = runtime.global();
17+
if (global.hasProperty(runtime, propName.c_str())) {
18+
throw jsi::JSError(
19+
runtime,
20+
"Tried to redefine read-only global \"" + propName +
21+
"\", but read-only globals can only be defined once.");
22+
}
23+
jsi::Object jsObject =
24+
global.getProperty(runtime, "Object").asObject(runtime);
25+
jsi::Function defineProperty = jsObject.getProperty(runtime, "defineProperty")
26+
.asObject(runtime)
27+
.asFunction(runtime);
28+
29+
jsi::Object descriptor = jsi::Object(runtime);
30+
descriptor.setProperty(runtime, "value", std::move(value));
31+
defineProperty.callWithThis(
32+
runtime,
33+
jsObject,
34+
global,
35+
jsi::String::createFromUtf8(runtime, propName),
36+
descriptor);
37+
}
38+
39+
} // namespace facebook::react
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <jsi/jsi.h>
11+
#include <string>
12+
13+
namespace facebook::react {
14+
15+
/**
16+
* Defines a property on the global object that is neither enumerable, nor
17+
* configurable, nor writable. This ensures that the private globals exposed by
18+
* ReactInstance cannot overwritten by third-party JavaScript code. It also
19+
* ensures that third-party JavaScript code unaware of these globals isn't able
20+
* to accidentally access them. In JavaScript, equivalent to:
21+
*
22+
* Object.defineProperty(global, propName, {
23+
* value: value
24+
* })
25+
*/
26+
void defineReadOnlyGlobal(
27+
jsi::Runtime& runtime,
28+
const std::string& propName,
29+
jsi::Value&& value);
30+
31+
} // namespace facebook::react

0 commit comments

Comments
 (0)