Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share re-defined defineReadOnlyGlobal(...) #42801

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/react-native/ReactCommon/ReactCommon.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ Pod::Spec.new do |s|

ss.subspec "core" do |sss|
sss.source_files = "react/nativemodule/core/ReactCommon/**/*.{cpp,h}"
sss.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_TARGET_SRCROOT)/ReactCommon\" \"$(PODS_CONFIGURATION_BUILD_DIR)/React-debug/React_debug.framework/Headers\"" }
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\"" }
sss.dependency "React-debug", version
sss.dependency "React-utils", version
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ target_link_libraries(react_nativemodule_core
jsi
react_bridging
react_debug
react_utils
reactperflogger
reactnativejni)
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

#include "TurboModuleBinding.h"

#include <cxxreact/SystraceSection.h>
#include <react/utils/jsi.h>
#include <stdexcept>
#include <string>

#include <cxxreact/SystraceSection.h>

using namespace facebook;

namespace facebook::react {
Expand Down Expand Up @@ -62,33 +62,6 @@ class BridgelessNativeModuleProxy : public jsi::HostObject {
}
};

// TODO(148359183): Merge this with the Bridgeless defineReadOnlyGlobal util
static void defineReadOnlyGlobal(
jsi::Runtime& runtime,
std::string propName,
jsi::Value&& value) {
if (runtime.global().hasProperty(runtime, propName.c_str())) {
throw jsi::JSError(
runtime,
"Tried to redefine read-only global \"" + propName +
"\", but read-only globals can only be defined once.");
}
jsi::Object jsObject =
runtime.global().getProperty(runtime, "Object").asObject(runtime);
jsi::Function defineProperty = jsObject.getProperty(runtime, "defineProperty")
.asObject(runtime)
.asFunction(runtime);

jsi::Object descriptor = jsi::Object(runtime);
descriptor.setProperty(runtime, "value", std::move(value));
defineProperty.callWithThis(
runtime,
jsObject,
runtime.global(),
jsi::String::createFromUtf8(runtime, propName),
descriptor);
}

/**
* Public API to install the TurboModule system.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ target_link_libraries(
react_utils
jsinspector
react_featureflags
react_utils
)
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ Pod::Spec.new do |s|
s.dependency folly_dep_name, folly_version
s.dependency "React-nativeconfig"
s.dependency "React-jsitracing"
s.dependency "React-utils"
s.dependency "React-jsi"
s.dependency "React-utils"
s.dependency "React-RuntimeCore"
s.dependency "React-featureflags"
add_dependency(s, "React-jsinspector", :framework_name => 'jsinspector_modern')
Expand Down
41 changes: 2 additions & 39 deletions packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
#include <cxxreact/ErrorUtils.h>
#include <cxxreact/JSBigString.h>
#include <cxxreact/JSExecutor.h>
#include <cxxreact/ReactMarker.h>
#include <cxxreact/SystraceSection.h>
#include <glog/logging.h>
#include <jsi/JSIDynamic.h>
#include <jsi/instrumentation.h>
#include <jsireact/JSIExecutor.h>
#include <react/featureflags/ReactNativeFeatureFlags.h>
#include <react/renderer/runtimescheduler/RuntimeSchedulerBinding.h>

#include <cxxreact/ReactMarker.h>
#include <react/utils/jsi.h>
#include <iostream>
#include <tuple>
#include <utility>
Expand Down Expand Up @@ -140,43 +140,6 @@ ReactInstance::getRuntimeScheduler() noexcept {
return runtimeScheduler_;
}

/**
* Defines a property on the global object that is neither enumerable, nor
* configurable, nor writable. This ensures that the private globals exposed by
* ReactInstance cannot overwritten by third-party JavaScript code. It also
* ensures that third-party JavaScript code unaware of these globals isn't able
* to accidentally access them. In JavaScript, equivalent to:
*
* Object.defineProperty(global, propName, {
* value: value
* })
*/
static void defineReadOnlyGlobal(
jsi::Runtime& runtime,
std::string propName,
jsi::Value&& value) {
if (runtime.global().hasProperty(runtime, propName.c_str())) {
throw jsi::JSError(
runtime,
"Tried to redefine read-only global \"" + propName +
"\", but read-only globals can only be defined once.");
}
jsi::Object jsObject =
runtime.global().getProperty(runtime, "Object").asObject(runtime);
jsi::Function defineProperty = jsObject.getProperty(runtime, "defineProperty")
.asObject(runtime)
.asFunction(runtime);

jsi::Object descriptor = jsi::Object(runtime);
descriptor.setProperty(runtime, "value", std::move(value));
defineProperty.callWithThis(
runtime,
jsObject,
runtime.global(),
jsi::String::createFromUtf8(runtime, propName),
descriptor);
}

namespace {

// Copied from JSIExecutor.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ target_include_directories(react_utils PUBLIC ${REACT_COMMON_DIR})
target_link_libraries(react_utils
glog
glog_init
jsireact
react_debug)
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ Pod::Spec.new do |s|
end

s.dependency "RCT-Folly", folly_version
s.dependency "React-jsi", version
s.dependency "glog"

if ENV["USE_HERMES"] == nil || ENV["USE_HERMES"] == "1"
s.dependency "hermes-engine"
else
s.dependency "React-jsc"
end

add_dependency(s, "React-debug")
end
39 changes: 39 additions & 0 deletions packages/react-native/ReactCommon/react/utils/jsi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 "jsi.h"

namespace facebook::react {

void defineReadOnlyGlobal(
jsi::Runtime& runtime,
const std::string& propName,
jsi::Value&& value) {
auto global = runtime.global();
if (global.hasProperty(runtime, propName.c_str())) {
throw jsi::JSError(
runtime,
"Tried to redefine read-only global \"" + propName +
"\", but read-only globals can only be defined once.");
}
jsi::Object jsObject =
global.getProperty(runtime, "Object").asObject(runtime);
jsi::Function defineProperty = jsObject.getProperty(runtime, "defineProperty")
.asObject(runtime)
.asFunction(runtime);

jsi::Object descriptor = jsi::Object(runtime);
descriptor.setProperty(runtime, "value", std::move(value));
defineProperty.callWithThis(
runtime,
jsObject,
global,
jsi::String::createFromUtf8(runtime, propName),
descriptor);
}

} // namespace facebook::react
31 changes: 31 additions & 0 deletions packages/react-native/ReactCommon/react/utils/jsi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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>
#include <string>

namespace facebook::react {

/**
* Defines a property on the global object that is neither enumerable, nor
* configurable, nor writable. This ensures that the private globals exposed by
* ReactInstance cannot overwritten by third-party JavaScript code. It also
* ensures that third-party JavaScript code unaware of these globals isn't able
* to accidentally access them. In JavaScript, equivalent to:
*
* Object.defineProperty(global, propName, {
* value: value
* })
*/
void defineReadOnlyGlobal(
jsi::Runtime& runtime,
const std::string& propName,
jsi::Value&& value);

} // namespace facebook::react
Loading