first commit

This commit is contained in:
2026-03-10 16:18:05 +00:00
commit 11f9c069b5
31635 changed files with 3187747 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
# 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.
##################
### jsi ###
##################
cmake_minimum_required(VERSION 3.13)
set(CMAKE_VERBOSE_MAKEFILE on)
include(${REACT_COMMON_DIR}/cmake-utils/react-native-flags.cmake)
file(GLOB jsi_SRC CONFIGURE_DEPENDS jsi/*.cpp)
add_library(jsi SHARED ${jsi_SRC})
target_include_directories(jsi PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(jsi
folly_runtime
glog)
target_compile_reactnative_options(jsi PRIVATE)
target_compile_options(jsi PRIVATE -O3 -Wno-unused-lambda-capture)

View File

@@ -0,0 +1,50 @@
# 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.
require "json"
package = JSON.parse(File.read(File.join(__dir__, "..", "..", "package.json")))
version = package['version']
source = { :git => 'https://github.com/facebook/react-native.git' }
if version == '1000.0.0'
# This is an unpublished version, use the latest commit hash of the react-native repo, which were presumably in.
source[:commit] = `git rev-parse HEAD`.strip if system("git rev-parse --git-dir > /dev/null 2>&1")
else
source[:tag] = "v#{version}"
end
Pod::Spec.new do |s|
s.name = "React-jsi"
s.version = version
s.summary = "JavaScript Interface layer for React Native"
s.homepage = "https://reactnative.dev/"
s.license = package["license"]
s.author = "Meta Platforms, Inc. and its affiliates"
s.platforms = min_supported_versions
s.source = source
s.header_dir = "jsi"
s.pod_target_xcconfig = {
"CLANG_CXX_LANGUAGE_STANDARD" => rct_cxx_language_standard(),
"DEFINES_MODULE" => "YES"
}
s.source_files = podspec_sources("**/*.{cpp,h}", "**/*.h")
files_to_exclude = [
"jsi/jsilib-posix.cpp",
"jsi/jsilib-windows.cpp",
"**/test/*"
]
if use_hermes()
# JSI is a part of hermes-engine. Including them also in react-native will violate the One Definition Rulle.
files_to_exclude += [ "jsi/jsi.cpp" ]
s.dependency "hermes-engine"
end
s.exclude_files = files_to_exclude
add_rn_third_party_dependencies(s)
add_rncore_dependency(s)
end

View File

@@ -0,0 +1,28 @@
# 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.
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(jsi
jsi.cpp)
target_include_directories(jsi PUBLIC ..)
set(jsi_compile_flags "")
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR
"${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
list(APPEND jsi_compile_flags "-Wno-non-virtual-dtor")
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
# Turn on Error Handling in MSVC, otherwise objects are not destructed
# when they go out of scope due to exceptions.
list(APPEND jsi_compile_flags "/EHsc")
endif()
target_compile_options(jsi PRIVATE ${jsi_compile_flags})
install(DIRECTORY "${PROJECT_SOURCE_DIR}/API/jsi/" DESTINATION include
FILES_MATCHING PATTERN "*.h"
PATTERN "test" EXCLUDE)

View File

@@ -0,0 +1,212 @@
/*
* 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 "JSIDynamic.h"
#include <glog/logging.h>
#include <folly/dynamic.h>
#include <jsi/jsi.h>
using namespace facebook::jsi;
namespace facebook {
namespace jsi {
namespace {
struct FromDynamic {
FromDynamic(const folly::dynamic* dynArg, Object objArg)
: dyn(dynArg), obj(std::move(objArg)) {}
const folly::dynamic* dyn;
Object obj;
};
// This converts one element. If it's a collection, it gets pushed onto
// the stack for later processing.
Value valueFromDynamicShallow(
Runtime& runtime,
std::vector<FromDynamic>& stack,
const folly::dynamic& dyn) {
switch (dyn.type()) {
case folly::dynamic::NULLT:
return Value::null();
case folly::dynamic::ARRAY: {
Object arr = Array(runtime, dyn.size());
Value ret = Value(runtime, arr);
stack.emplace_back(&dyn, std::move(arr));
return ret;
}
case folly::dynamic::BOOL:
return Value(dyn.getBool());
case folly::dynamic::DOUBLE:
return dyn.getDouble();
case folly::dynamic::INT64:
return Value((double)dyn.getInt());
case folly::dynamic::OBJECT: {
auto obj = Object(runtime);
Value ret = Value(runtime, obj);
stack.emplace_back(&dyn, std::move(obj));
return ret;
}
case folly::dynamic::STRING:
return Value(String::createFromUtf8(runtime, dyn.getString()));
}
CHECK(false);
}
} // namespace
Value valueFromDynamic(Runtime& runtime, const folly::dynamic& dynInput) {
std::vector<FromDynamic> stack;
Value ret = valueFromDynamicShallow(runtime, stack, dynInput);
while (!stack.empty()) {
auto top = std::move(stack.back());
stack.pop_back();
switch (top.dyn->type()) {
case folly::dynamic::ARRAY: {
Array arr = std::move(top.obj).getArray(runtime);
for (size_t i = 0; i < top.dyn->size(); ++i) {
arr.setValueAtIndex(
runtime,
i,
valueFromDynamicShallow(runtime, stack, (*top.dyn)[i]));
}
break;
}
case folly::dynamic::OBJECT: {
Object obj = std::move(top.obj);
for (const auto& element : top.dyn->items()) {
if (element.first.isNumber() || element.first.isString()) {
obj.setProperty(
runtime,
PropNameID::forUtf8(runtime, element.first.asString()),
valueFromDynamicShallow(runtime, stack, element.second));
}
}
break;
}
default:
CHECK(false);
}
}
return ret;
}
namespace {
struct FromValue {
FromValue(folly::dynamic* dynArg, Object objArg)
: dyn(dynArg), obj(std::move(objArg)) {}
folly::dynamic* dyn;
Object obj;
};
// This converts one element. If it's a collection, it gets pushed
// onto the stack for later processing. The output is created by
// mutating the output argument, because we need its actual pointer to
// push onto the stack.
void dynamicFromValueShallow(
Runtime& runtime,
std::vector<FromValue>& stack,
const jsi::Value& value,
folly::dynamic& output) {
if (value.isUndefined() || value.isNull()) {
output = nullptr;
} else if (value.isBool()) {
output = value.getBool();
} else if (value.isNumber()) {
output = value.getNumber();
} else if (value.isString()) {
output = value.getString(runtime).utf8(runtime);
} else if (value.isObject()) {
Object obj = value.getObject(runtime);
if (obj.isArray(runtime)) {
output = folly::dynamic::array();
} else if (obj.isFunction(runtime)) {
throw JSError(runtime, "JS Functions are not convertible to dynamic");
} else {
output = folly::dynamic::object();
}
stack.emplace_back(&output, std::move(obj));
} else if (value.isBigInt()) {
throw JSError(runtime, "JS BigInts are not convertible to dynamic");
} else if (value.isSymbol()) {
throw JSError(runtime, "JS Symbols are not convertible to dynamic");
} else {
throw JSError(runtime, "Value is not convertible to dynamic");
}
}
} // namespace
folly::dynamic dynamicFromValue(
Runtime& runtime,
const Value& valueInput,
const std::function<bool(const std::string&)>& filterObjectKeys) {
std::vector<FromValue> stack;
folly::dynamic ret;
dynamicFromValueShallow(runtime, stack, valueInput, ret);
while (!stack.empty()) {
auto top = std::move(stack.back());
stack.pop_back();
if (top.obj.isArray(runtime)) {
// Inserting into a dyn can invalidate references into it, so we
// need to insert new elements up front, then push stuff onto
// the stack.
Array array = top.obj.getArray(runtime);
size_t arraySize = array.size(runtime);
for (size_t i = 0; i < arraySize; ++i) {
top.dyn->push_back(nullptr);
}
for (size_t i = 0; i < arraySize; ++i) {
dynamicFromValueShallow(
runtime, stack, array.getValueAtIndex(runtime, i), top.dyn->at(i));
}
} else {
Array names = top.obj.getPropertyNames(runtime);
std::vector<std::pair<std::string, jsi::Value>> props;
for (size_t i = 0; i < names.size(runtime); ++i) {
String name = names.getValueAtIndex(runtime, i).getString(runtime);
Value prop = top.obj.getProperty(runtime, name);
if (prop.isUndefined()) {
continue;
}
auto nameStr = name.utf8(runtime);
if (filterObjectKeys && filterObjectKeys(nameStr)) {
continue;
}
// The JSC conversion uses JSON.stringify, which substitutes
// null for a function, so we do the same here. Just dropping
// the pair might also work, but would require more testing.
if (prop.isObject() && prop.getObject(runtime).isFunction(runtime)) {
prop = Value::null();
}
props.emplace_back(std::move(nameStr), std::move(prop));
top.dyn->insert(props.back().first, nullptr);
}
for (const auto& prop : props) {
dynamicFromValueShallow(
runtime, stack, prop.second, (*top.dyn)[prop.first]);
}
}
}
return ret;
}
} // namespace jsi
} // namespace facebook

View File

@@ -0,0 +1,26 @@
/*
* 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 <folly/dynamic.h>
#include <jsi/jsi.h>
namespace facebook {
namespace jsi {
facebook::jsi::Value valueFromDynamic(
facebook::jsi::Runtime& runtime,
const folly::dynamic& dyn);
folly::dynamic dynamicFromValue(
facebook::jsi::Runtime& runtime,
const facebook::jsi::Value& value,
const std::function<bool(const std::string&)>& filterObjectKeys = nullptr);
} // namespace jsi
} // namespace facebook

1064
node_modules/react-native/ReactCommon/jsi/jsi/decorator.h generated vendored Normal file

File diff suppressed because it is too large Load Diff

189
node_modules/react-native/ReactCommon/jsi/jsi/hermes.h generated vendored Normal file
View File

@@ -0,0 +1,189 @@
/*
* 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>
struct SHUnit;
struct SHRuntime;
using SHUnitCreator = SHUnit* (*)();
namespace hermes::vm {
class GCExecTrace;
}
namespace facebook::hermes {
namespace sampling_profiler {
class Profile;
}
namespace debugger {
class Debugger;
}
/// Interface for Hermes-specific runtime methods.The actual implementations of
/// the pure virtual methods are provided by Hermes API.
class JSI_EXPORT IHermes : public jsi::ICast {
public:
static constexpr jsi::UUID uuid{
0xe85cfa22,
0xdfae,
0x11ef,
0xa6f7,
0x325096b39f47};
struct DebugFlags {
// Looking for the .lazy flag? It's no longer necessary.
// Source is evaluated lazily by default. See
// RuntimeConfig::CompilationMode.
};
/// Evaluate the given code in an unoptimized form, used for debugging.
/// This will be no-op if the implementation does not have debugger enabled.
virtual void debugJavaScript(
const std::string& src,
const std::string& sourceURL,
const DebugFlags& debugFlags) = 0;
/// Return a ICast pointer to an object that be cast into the interface
/// IHermesRootAPI. This root API object has static lifetime.
virtual ICast* getHermesRootAPI() = 0;
/// Dump sampled stack trace for a given runtime to a data structure that can
/// be used by third parties.
virtual sampling_profiler::Profile dumpSampledTraceToProfile() = 0;
/// Serialize the sampled stack to the format expected by DevTools'
/// Profiler.stop return type.
virtual void sampledTraceToStreamInDevToolsFormat(std::ostream& stream) = 0;
/// Resets the timezone offset cache used by Hermes for performance
/// optimization. Hermes maintains a cached timezone offset to accelerate date
/// and time calculations. However, this cache does not automatically detect
/// changes to the system timezone. When the system timezone changes, the
/// integration layer (e.g., React Native) must call this method to invalidate
/// the cache and ensure correct time calculations.
///
/// \note Call this method immediately after detecting any timezone change in
/// the integrator.
virtual void resetTimezoneCache() = 0;
/// Load a new segment into the Runtime.
/// The \param context must be a valid RequireContext retrieved from JS
/// using `require.context`.
virtual void loadSegment(
std::unique_ptr<const jsi::Buffer> buffer,
const jsi::Value& context) = 0;
/// Gets a guaranteed unique id for an Object (or, respectively, String
/// or PropNameId), which is assigned at allocation time and is
/// static throughout that object's (or string's, or PropNameID's)
/// lifetime.
virtual uint64_t getUniqueID(const jsi::Object& o) const = 0;
virtual uint64_t getUniqueID(const jsi::BigInt& s) const = 0;
virtual uint64_t getUniqueID(const jsi::String& s) const = 0;
virtual uint64_t getUniqueID(const jsi::PropNameID& pni) const = 0;
virtual uint64_t getUniqueID(const jsi::Symbol& sym) const = 0;
/// Same as the other \c getUniqueID, except it can return 0 for some values.
/// 0 means there is no ID associated with the value.
virtual uint64_t getUniqueID(const jsi::Value& val) const = 0;
/// From an ID retrieved from \p getUniqueID, go back to the object.
/// NOTE: This is much slower in general than the reverse operation, and takes
/// up more memory. Don't use this unless it's absolutely necessary.
/// \return a jsi::Object if a matching object is found, else returns null.
virtual jsi::Value getObjectForID(uint64_t id) = 0;
/// Get a structure representing the execution history (currently just of
/// GC, but will be generalized as necessary), to aid in debugging
/// non-deterministic execution.
virtual const ::hermes::vm::GCExecTrace& getGCExecTrace() const = 0;
/// Get IO tracking (aka HBC page access) info as a JSON string.
/// See hermes::vm::Runtime::getIOTrackingInfoJSON() for conditions
/// needed for there to be useful output.
virtual std::string getIOTrackingInfoJSON() = 0;
/// \return a reference to the Debugger for this Runtime.
virtual debugger::Debugger& getDebugger() = 0;
/// Register this runtime and thread for sampling profiler. Before using the
/// runtime on another thread, invoke this function again from the new thread
/// to make the sampling profiler target the new thread (and forget the old
/// thread).
virtual void registerForProfiling() = 0;
/// Unregister this runtime for sampling profiler.
virtual void unregisterForProfiling() = 0;
/// Define methods to interrupt JS execution and set time limits.
/// All JS compiled to bytecode via prepareJS, or evaluateJS, will support
/// interruption and time limit monitoring if the runtime is configured with
/// AsyncBreakCheckInEval. If JS prepared in other ways is executed, care must
/// be taken to ensure that it is compiled in a mode that supports it (i.e.,
/// the emitted code contains async break checks).
/// Asynchronously terminates the current execution. This can be called on
/// any thread.
virtual void asyncTriggerTimeout() = 0;
/// Register this runtime for execution time limit monitoring, with a time
/// limit of \p timeoutInMs milliseconds.
/// See compilation notes above.
virtual void watchTimeLimit(uint32_t timeoutInMs) = 0;
/// Unregister this runtime for execution time limit monitoring.
virtual void unwatchTimeLimit() = 0;
/// Same as \c evaluate JavaScript but with a source map, which will be
/// applied to exception traces and debug information.
///
/// This is an experimental Hermes-specific API. In the future it may be
/// renamed, moved or combined with another API, but the provided
/// functionality will continue to be available in some form.
virtual jsi::Value evaluateJavaScriptWithSourceMap(
const std::shared_ptr<const jsi::Buffer>& buffer,
const std::shared_ptr<const jsi::Buffer>& sourceMapBuf,
const std::string& sourceURL) = 0;
/// Associate the SHUnit returned by \p shUnitCreator with this runtime and
/// run its initialization code. The unit will be freed when the runtime is
/// destroyed.
virtual jsi::Value evaluateSHUnit(SHUnitCreator shUnitCreator) = 0;
/// Retrieve the underlying SHRuntime.
virtual SHRuntime* getSHRuntime() noexcept = 0;
/// Returns the underlying low level Hermes VM runtime instance.
/// This function is considered unsafe and unstable.
/// Direct use of a vm::Runtime should be avoided as the lower level APIs are
/// unsafe and they can change without notice.
virtual void* getVMRuntimeUnsafe() const = 0;
protected:
~IHermes() = default;
};
/// Interface for provide Hermes backend specific methods.
class IHermesSHUnit : public jsi::ICast {
public:
static constexpr jsi::UUID uuid{
0x52a2d522,
0xcbc6,
0x4236,
0x8d5d,
0x2636c320ed65,
};
/// Get the unit creating function pointer which can be passed to
/// evaluateSHUnit() for evaluation.
virtual SHUnitCreator getSHUnitCreator() const = 0;
protected:
~IHermesSHUnit() = default;
};
} // namespace facebook::hermes

View File

@@ -0,0 +1,132 @@
/*
* 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 <chrono>
#include <iosfwd>
#include <string>
#include <tuple>
#include <unordered_map>
#include <jsi/jsi.h>
namespace facebook {
namespace jsi {
/// Methods for starting and collecting instrumentation, an \c Instrumentation
/// instance is associated with a particular \c Runtime instance, which it
/// controls the instrumentation of.
/// None of these functions should return newly created jsi values, nor should
/// it modify the values of any jsi values in the heap (although GCs are fine).
class JSI_EXPORT Instrumentation {
public:
/// Additional options controlling what to include when capturing a heap
/// snapshot.
struct HeapSnapshotOptions {
bool captureNumericValue{false};
};
virtual ~Instrumentation() = default;
/// Returns GC statistics as a JSON-encoded string, with an object containing
/// "type" and "version" fields outermost. "type" is a string, unique to a
/// particular implementation of \c jsi::Instrumentation, and "version" is a
/// number to indicate any revision to that implementation and its output
/// format.
///
/// \pre This call can only be made on the instrumentation instance of a
/// runtime initialised to collect GC statistics.
///
/// \post All cumulative measurements mentioned in the output are accumulated
/// across the entire lifetime of the Runtime.
///
/// \return the GC statistics collected so far, as a JSON-encoded string.
virtual std::string getRecordedGCStats() = 0;
/// Request statistics about the current state of the runtime's heap. This
/// function can be called at any time, and should produce information that is
/// correct at the instant it is called (i.e, not stale).
///
/// \return a map from a string key to a number associated with that
/// statistic.
virtual std::unordered_map<std::string, int64_t> getHeapInfo(
bool includeExpensive) = 0;
/// Perform a full garbage collection.
/// \param cause The cause of this collection, as it should be reported in
/// logs.
virtual void collectGarbage(std::string cause) = 0;
/// A HeapStatsUpdate is a tuple of the fragment index, the number of objects
/// in that fragment, and the number of bytes used by those objects.
/// A "fragment" is a view of all objects allocated within a time slice.
using HeapStatsUpdate = std::tuple<uint64_t, uint64_t, uint64_t>;
/// Start capturing JS stack-traces for all JS heap allocated objects. These
/// can be accessed via \c ::createSnapshotToFile().
/// \param fragmentCallback If present, invoke this callback every so often
/// with the most recently seen object ID, and a list of fragments that have
/// been updated. This callback will be invoked on the same thread that the
/// runtime is using.
virtual void startTrackingHeapObjectStackTraces(
std::function<void(
uint64_t lastSeenObjectID,
std::chrono::microseconds timestamp,
std::vector<HeapStatsUpdate> stats)> fragmentCallback) = 0;
/// Stop capture JS stack-traces for JS heap allocated objects.
virtual void stopTrackingHeapObjectStackTraces() = 0;
/// Start a heap sampling profiler that will sample heap allocations, and the
/// stack trace they were allocated at. Reports a summary of which functions
/// allocated the most.
/// \param samplingInterval The number of bytes allocated to wait between
/// samples. This will be used as the expected value of a poisson
/// distribution.
virtual void startHeapSampling(size_t samplingInterval) = 0;
/// Turns off the heap sampling profiler previously enabled via
/// \c startHeapSampling. Writes the output of the sampling heap profiler to
/// \p os. The output is a JSON formatted string.
virtual void stopHeapSampling(std::ostream& os) = 0;
/// Captures the heap to a file
///
/// \param path to save the heap capture.
/// \param options additional options for what to capture.
virtual void createSnapshotToFile(
const std::string& path,
const HeapSnapshotOptions& options = {false}) = 0;
/// Captures the heap to an output stream
///
/// \param os output stream to write to.
/// \param options additional options for what to capture.
virtual void createSnapshotToStream(
std::ostream& os,
const HeapSnapshotOptions& options = {false}) = 0;
/// If the runtime has been created to trace to a temp file, flush
/// any unwritten parts of the trace of bridge traffic to the file,
/// and return the name of the file. Otherwise, return the empty string.
/// Tracing is disabled after this call.
virtual std::string flushAndDisableBridgeTrafficTrace() = 0;
/// Write basic block profile trace to the given file name.
virtual void writeBasicBlockProfileTraceToFile(
const std::string& fileName) const = 0;
/// Write the opcode stats to the given stream.
virtual void dumpOpcodeStats(std::ostream& os) const = 0;
/// Dump external profiler symbols to the given file name.
virtual void dumpProfilerSymbolsToFile(const std::string& fileName) const = 0;
};
} // namespace jsi
} // namespace facebook

405
node_modules/react-native/ReactCommon/jsi/jsi/jsi-inl.h generated vendored Normal file
View File

@@ -0,0 +1,405 @@
/*
* 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
namespace facebook {
namespace jsi {
namespace detail {
inline Value toValue(Runtime&, std::nullptr_t) {
return Value::null();
}
inline Value toValue(Runtime&, bool b) {
return Value(b);
}
inline Value toValue(Runtime&, double d) {
return Value(d);
}
inline Value toValue(Runtime&, float f) {
return Value(static_cast<double>(f));
}
inline Value toValue(Runtime&, int i) {
return Value(i);
}
inline Value toValue(Runtime& runtime, const char* str) {
return String::createFromAscii(runtime, str);
}
inline Value toValue(Runtime& runtime, const std::string& str) {
return String::createFromUtf8(runtime, str);
}
template <typename T>
inline Value toValue(Runtime& runtime, const T& other) {
static_assert(
std::is_base_of<Pointer, T>::value,
"This type cannot be converted to Value");
return Value(runtime, other);
}
inline Value toValue(Runtime& runtime, const Value& value) {
return Value(runtime, value);
}
inline Value&& toValue(Runtime&, Value&& value) {
return std::move(value);
}
inline PropNameID toPropNameID(Runtime& runtime, const char* name) {
return PropNameID::forAscii(runtime, name);
}
inline PropNameID toPropNameID(Runtime& runtime, const std::string& name) {
return PropNameID::forUtf8(runtime, name);
}
inline PropNameID&& toPropNameID(Runtime&, PropNameID&& name) {
return std::move(name);
}
/// Helper to throw while still compiling with exceptions turned off.
template <typename E, typename... Args>
[[noreturn]] inline void throwOrDie(Args&&... args) {
std::rethrow_exception(
std::make_exception_ptr(E{std::forward<Args>(args)...}));
}
} // namespace detail
template <typename T>
inline T Runtime::make(Runtime::PointerValue* pv) {
return T(pv);
}
inline Runtime::PointerValue* Runtime::getPointerValue(jsi::Pointer& pointer) {
return pointer.ptr_;
}
inline const Runtime::PointerValue* Runtime::getPointerValue(
const jsi::Pointer& pointer) {
return pointer.ptr_;
}
inline const Runtime::PointerValue* Runtime::getPointerValue(
const jsi::Value& value) {
return value.data_.pointer.ptr_;
}
inline void Runtime::setRuntimeData(
const UUID& uuid,
const std::shared_ptr<void>& data) {
auto* dataPtr = new std::shared_ptr<void>(data);
setRuntimeDataImpl(uuid, dataPtr, [](const void* data) {
delete (const std::shared_ptr<void>*)data;
});
}
inline std::shared_ptr<void> Runtime::getRuntimeData(const UUID& uuid) {
auto* data = (const std::shared_ptr<void>*)getRuntimeDataImpl(uuid);
return data ? *data : nullptr;
}
Value Object::getPrototype(Runtime& runtime) const {
return runtime.getPrototypeOf(*this);
}
inline Value Object::getProperty(Runtime& runtime, const char* name) const {
return getProperty(runtime, String::createFromAscii(runtime, name));
}
inline Value Object::getProperty(Runtime& runtime, const String& name) const {
return runtime.getProperty(*this, name);
}
inline Value Object::getProperty(Runtime& runtime, const PropNameID& name)
const {
return runtime.getProperty(*this, name);
}
inline Value Object::getProperty(Runtime& runtime, const Value& name) const {
return runtime.getProperty(*this, name);
}
inline bool Object::hasProperty(Runtime& runtime, const char* name) const {
return hasProperty(runtime, String::createFromAscii(runtime, name));
}
inline bool Object::hasProperty(Runtime& runtime, const String& name) const {
return runtime.hasProperty(*this, name);
}
inline bool Object::hasProperty(Runtime& runtime, const PropNameID& name)
const {
return runtime.hasProperty(*this, name);
}
inline bool Object::hasProperty(Runtime& runtime, const Value& name) const {
return runtime.hasProperty(*this, name);
}
template <typename T>
void Object::setProperty(Runtime& runtime, const char* name, T&& value) const {
setProperty(
runtime, String::createFromAscii(runtime, name), std::forward<T>(value));
}
template <typename T>
void Object::setProperty(Runtime& runtime, const String& name, T&& value)
const {
setPropertyValue(
runtime, name, detail::toValue(runtime, std::forward<T>(value)));
}
template <typename T>
void Object::setProperty(Runtime& runtime, const PropNameID& name, T&& value)
const {
setPropertyValue(
runtime, name, detail::toValue(runtime, std::forward<T>(value)));
}
template <typename T>
void Object::setProperty(Runtime& runtime, const Value& name, T&& value) const {
setPropertyValue(
runtime, name, detail::toValue(runtime, std::forward<T>(value)));
}
inline void Object::deleteProperty(Runtime& runtime, const char* name) const {
deleteProperty(runtime, String::createFromAscii(runtime, name));
}
inline void Object::deleteProperty(Runtime& runtime, const String& name) const {
runtime.deleteProperty(*this, name);
}
inline void Object::deleteProperty(Runtime& runtime, const PropNameID& name)
const {
runtime.deleteProperty(*this, name);
}
inline void Object::deleteProperty(Runtime& runtime, const Value& name) const {
runtime.deleteProperty(*this, name);
}
inline Array Object::getArray(Runtime& runtime) const& {
assert(runtime.isArray(*this));
(void)runtime; // when assert is disabled we need to mark this as used
return Array(runtime.cloneObject(ptr_));
}
inline Array Object::getArray(Runtime& runtime) && {
assert(runtime.isArray(*this));
(void)runtime; // when assert is disabled we need to mark this as used
Runtime::PointerValue* value = ptr_;
ptr_ = nullptr;
return Array(value);
}
inline ArrayBuffer Object::getArrayBuffer(Runtime& runtime) const& {
assert(runtime.isArrayBuffer(*this));
(void)runtime; // when assert is disabled we need to mark this as used
return ArrayBuffer(runtime.cloneObject(ptr_));
}
inline ArrayBuffer Object::getArrayBuffer(Runtime& runtime) && {
assert(runtime.isArrayBuffer(*this));
(void)runtime; // when assert is disabled we need to mark this as used
Runtime::PointerValue* value = ptr_;
ptr_ = nullptr;
return ArrayBuffer(value);
}
inline Function Object::getFunction(Runtime& runtime) const& {
assert(runtime.isFunction(*this));
return Function(runtime.cloneObject(ptr_));
}
inline Function Object::getFunction(Runtime& runtime) && {
assert(runtime.isFunction(*this));
(void)runtime; // when assert is disabled we need to mark this as used
Runtime::PointerValue* value = ptr_;
ptr_ = nullptr;
return Function(value);
}
template <typename T>
inline bool Object::isHostObject(Runtime& runtime) const {
return runtime.isHostObject(*this) &&
std::dynamic_pointer_cast<T>(runtime.getHostObject(*this));
}
template <>
inline bool Object::isHostObject<HostObject>(Runtime& runtime) const {
return runtime.isHostObject(*this);
}
template <typename T>
inline std::shared_ptr<T> Object::getHostObject(Runtime& runtime) const {
assert(isHostObject<T>(runtime));
return std::static_pointer_cast<T>(runtime.getHostObject(*this));
}
template <typename T>
inline std::shared_ptr<T> Object::asHostObject(Runtime& runtime) const {
if (!isHostObject<T>(runtime)) {
detail::throwOrDie<JSINativeException>(
"Object is not a HostObject of desired type");
}
return std::static_pointer_cast<T>(runtime.getHostObject(*this));
}
template <>
inline std::shared_ptr<HostObject> Object::getHostObject<HostObject>(
Runtime& runtime) const {
assert(runtime.isHostObject(*this));
return runtime.getHostObject(*this);
}
template <typename T>
inline bool Object::hasNativeState(Runtime& runtime) const {
return runtime.hasNativeState(*this) &&
std::dynamic_pointer_cast<T>(runtime.getNativeState(*this));
}
template <>
inline bool Object::hasNativeState<NativeState>(Runtime& runtime) const {
return runtime.hasNativeState(*this);
}
template <typename T>
inline std::shared_ptr<T> Object::getNativeState(Runtime& runtime) const {
assert(hasNativeState<T>(runtime));
return std::static_pointer_cast<T>(runtime.getNativeState(*this));
}
inline void Object::setNativeState(
Runtime& runtime,
std::shared_ptr<NativeState> state) const {
runtime.setNativeState(*this, state);
}
inline void Object::setExternalMemoryPressure(Runtime& runtime, size_t amt)
const {
runtime.setExternalMemoryPressure(*this, amt);
}
inline Array Object::getPropertyNames(Runtime& runtime) const {
return runtime.getPropertyNames(*this);
}
inline Value WeakObject::lock(Runtime& runtime) const {
return runtime.lockWeakObject(*this);
}
template <typename T>
void Array::setValueAtIndex(Runtime& runtime, size_t i, T&& value) const {
setValueAtIndexImpl(
runtime, i, detail::toValue(runtime, std::forward<T>(value)));
}
inline Value Array::getValueAtIndex(Runtime& runtime, size_t i) const {
return runtime.getValueAtIndex(*this, i);
}
inline Function Function::createFromHostFunction(
Runtime& runtime,
const jsi::PropNameID& name,
unsigned int paramCount,
jsi::HostFunctionType func) {
return runtime.createFunctionFromHostFunction(
name, paramCount, std::move(func));
}
inline Value Function::call(Runtime& runtime, const Value* args, size_t count)
const {
return runtime.call(*this, Value::undefined(), args, count);
}
inline Value Function::call(Runtime& runtime, std::initializer_list<Value> args)
const {
return call(runtime, args.begin(), args.size());
}
template <typename... Args>
inline Value Function::call(Runtime& runtime, Args&&... args) const {
// A more awesome version of this would be able to create raw values
// which can be used directly without wrapping and unwrapping, but
// this will do for now.
return call(runtime, {detail::toValue(runtime, std::forward<Args>(args))...});
}
inline Value Function::callWithThis(
Runtime& runtime,
const Object& jsThis,
const Value* args,
size_t count) const {
return runtime.call(*this, Value(runtime, jsThis), args, count);
}
inline Value Function::callWithThis(
Runtime& runtime,
const Object& jsThis,
std::initializer_list<Value> args) const {
return callWithThis(runtime, jsThis, args.begin(), args.size());
}
template <typename... Args>
inline Value Function::callWithThis(
Runtime& runtime,
const Object& jsThis,
Args&&... args) const {
// A more awesome version of this would be able to create raw values
// which can be used directly without wrapping and unwrapping, but
// this will do for now.
return callWithThis(
runtime, jsThis, {detail::toValue(runtime, std::forward<Args>(args))...});
}
template <typename... Args>
inline Array Array::createWithElements(Runtime& runtime, Args&&... args) {
return createWithElements(
runtime, {detail::toValue(runtime, std::forward<Args>(args))...});
}
template <typename... Args>
inline std::vector<PropNameID> PropNameID::names(
Runtime& runtime,
Args&&... args) {
return names({detail::toPropNameID(runtime, std::forward<Args>(args))...});
}
template <size_t N>
inline std::vector<PropNameID> PropNameID::names(
PropNameID (&&propertyNames)[N]) {
std::vector<PropNameID> result;
result.reserve(N);
for (auto& name : propertyNames) {
result.push_back(std::move(name));
}
return result;
}
inline Value Function::callAsConstructor(
Runtime& runtime,
const Value* args,
size_t count) const {
return runtime.callAsConstructor(*this, args, count);
}
inline Value Function::callAsConstructor(
Runtime& runtime,
std::initializer_list<Value> args) const {
return callAsConstructor(runtime, args.begin(), args.size());
}
template <typename... Args>
inline Value Function::callAsConstructor(Runtime& runtime, Args&&... args)
const {
return callAsConstructor(
runtime, {detail::toValue(runtime, std::forward<Args>(args))...});
}
String BigInt::toString(Runtime& runtime, int radix) const {
return runtime.bigintToString(*this, radix);
}
} // namespace jsi
} // namespace facebook

925
node_modules/react-native/ReactCommon/jsi/jsi/jsi.cpp generated vendored Normal file
View File

@@ -0,0 +1,925 @@
/*
* 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 <cassert>
#include <cmath>
#include <cstdlib>
#include <map>
#include <mutex>
#include <stdexcept>
#include <jsi/instrumentation.h>
#include <jsi/jsi.h>
namespace facebook {
namespace jsi {
namespace {
/// A global map used to store custom runtime data for VMs that do not provide
/// their own default implementation of setRuntimeData and getRuntimeData.
struct RuntimeDataGlobal {
/// Mutex protecting the Runtime data map
std::mutex mutex_{};
/// Maps a runtime pointer to a map of its custom data. At destruction of the
/// runtime, its entry will be removed from the global map.
std::unordered_map<
Runtime*,
std::unordered_map<
UUID,
std::pair<const void*, void (*)(const void* data)>,
UUID::Hash>>
dataMap_;
};
RuntimeDataGlobal& getRuntimeDataGlobal() {
static RuntimeDataGlobal runtimeData{};
return runtimeData;
}
/// A host object that, when destructed, will remove the runtime's custom data
/// entry from the global map of custom data.
class RemoveRuntimeDataHostObject : public jsi::HostObject {
public:
explicit RemoveRuntimeDataHostObject(Runtime* runtime) : runtime_(runtime) {}
RemoveRuntimeDataHostObject(const RemoveRuntimeDataHostObject&) = default;
RemoveRuntimeDataHostObject(RemoveRuntimeDataHostObject&&) = default;
RemoveRuntimeDataHostObject& operator=(const RemoveRuntimeDataHostObject&) =
default;
RemoveRuntimeDataHostObject& operator=(RemoveRuntimeDataHostObject&&) =
default;
~RemoveRuntimeDataHostObject() override {
auto& runtimeDataGlobal = getRuntimeDataGlobal();
std::lock_guard<std::mutex> lock(runtimeDataGlobal.mutex_);
auto runtimeMapIt = runtimeDataGlobal.dataMap_.find(runtime_);
// We install the RemoveRuntimeDataHostObject only when the first custom
// data for the runtime is added, and only this object is responsible for
// clearing runtime data. Thus, we should always be able to find the data
// entry.
assert(
runtimeMapIt != runtimeDataGlobal.dataMap_.end() &&
"Custom runtime data not found for this runtime");
for (auto [_, entry] : runtimeMapIt->second) {
auto* deleter = entry.second;
deleter(entry.first);
}
runtimeDataGlobal.dataMap_.erase(runtime_);
}
private:
Runtime* runtime_;
};
// This is used for generating short exception strings.
std::string kindToString(const Value& v, Runtime* rt = nullptr) {
if (v.isUndefined()) {
return "undefined";
} else if (v.isNull()) {
return "null";
} else if (v.isBool()) {
return v.getBool() ? "true" : "false";
} else if (v.isNumber()) {
return "a number";
} else if (v.isString()) {
return "a string";
} else if (v.isSymbol()) {
return "a symbol";
} else if (v.isBigInt()) {
return "a bigint";
} else {
assert(v.isObject() && "Expecting object.");
return rt != nullptr && v.getObject(*rt).isFunction(*rt) ? "a function"
: "an object";
}
}
// getPropertyAsFunction() will try to create a JSError. If the
// failure is in building a JSError, this will lead to infinite
// recursion. This function is used in place of getPropertyAsFunction
// when building JSError, to avoid that infinite recursion.
Value callGlobalFunction(Runtime& runtime, const char* name, const Value& arg) {
Value v = runtime.global().getProperty(runtime, name);
if (!v.isObject()) {
throw JSINativeException(
std::string("callGlobalFunction: JS global property '") + name +
"' is " + kindToString(v, &runtime) + ", expected a Function");
}
Object o = v.getObject(runtime);
if (!o.isFunction(runtime)) {
throw JSINativeException(
std::string("callGlobalFunction: JS global property '") + name +
"' is a non-callable Object, expected a Function");
}
Function f = std::move(o).getFunction(runtime);
return f.call(runtime, arg);
}
// Given a sequence of UTF8 encoded bytes, advance the input to past where a
// 32-bit unicode codepoint as been decoded and return the codepoint. If the
// UTF8 encoding is invalid, then return the value with the unicode replacement
// character (U+FFFD). This decoder also relies on zero termination at end of
// the input for bound checks.
// \param input char pointer pointing to the current character
// \return Unicode codepoint
uint32_t decodeUTF8(const char*& input) {
uint32_t ch = (unsigned char)input[0];
if (ch <= 0x7f) {
input += 1;
return ch;
}
uint32_t ret;
constexpr uint32_t replacementCharacter = 0xFFFD;
if ((ch & 0xE0) == 0xC0) {
uint32_t ch1 = (unsigned char)input[1];
if ((ch1 & 0xC0) != 0x80) {
input += 1;
return replacementCharacter;
}
ret = ((ch & 0x1F) << 6) | (ch1 & 0x3F);
input += 2;
if (ret <= 0x7F) {
return replacementCharacter;
}
} else if ((ch & 0xF0) == 0xE0) {
uint32_t ch1 = (unsigned char)input[1];
if ((ch1 & 0x40) != 0 || (ch1 & 0x80) == 0) {
input += 1;
return replacementCharacter;
}
uint32_t ch2 = (unsigned char)input[2];
if ((ch2 & 0x40) != 0 || (ch2 & 0x80) == 0) {
input += 2;
return replacementCharacter;
}
ret = ((ch & 0x0F) << 12) | ((ch1 & 0x3F) << 6) | (ch2 & 0x3F);
input += 3;
if (ret <= 0x7FF) {
return replacementCharacter;
}
} else if ((ch & 0xF8) == 0xF0) {
uint32_t ch1 = (unsigned char)input[1];
if ((ch1 & 0x40) != 0 || (ch1 & 0x80) == 0) {
input += 1;
return replacementCharacter;
}
uint32_t ch2 = (unsigned char)input[2];
if ((ch2 & 0x40) != 0 || (ch2 & 0x80) == 0) {
input += 2;
return replacementCharacter;
}
uint32_t ch3 = (unsigned char)input[3];
if ((ch3 & 0x40) != 0 || (ch3 & 0x80) == 0) {
input += 3;
return replacementCharacter;
}
ret = ((ch & 0x07) << 18) | ((ch1 & 0x3F) << 12) | ((ch2 & 0x3F) << 6) |
(ch3 & 0x3F);
input += 4;
if (ret <= 0xFFFF) {
return replacementCharacter;
}
if (ret > 0x10FFFF) {
return replacementCharacter;
}
} else {
input += 1;
return replacementCharacter;
}
return ret;
}
// Given a valid 32-bit unicode codepoint, encode it as UTF-16 into the output.
void encodeUTF16(std::u16string& out, uint32_t cp) {
if (cp < 0x10000) {
out.push_back((uint16_t)cp);
return;
}
cp -= 0x10000;
uint16_t highSurrogate = 0xD800 + ((cp >> 10) & 0x3FF);
out.push_back(highSurrogate);
uint16_t lowSurrogate = 0xDC00 + (cp & 0x3FF);
out.push_back(lowSurrogate);
}
// Convert the UTF8 encoded string into a UTF16 encoded string. If the
// input is not valid UTF8, the replacement character (U+FFFD) is used to
// represent the invalid sequence.
std::u16string convertUTF8ToUTF16(const std::string& utf8) {
std::u16string ret;
const char* curr = utf8.data();
const char* end = curr + utf8.length();
while (curr < end) {
auto cp = decodeUTF8(curr);
encodeUTF16(ret, cp);
}
return ret;
}
// Given a unsigned number, which is less than 16, return the hex character.
inline char hexDigit(unsigned x) {
return x < 10 ? '0' + x : 'A' + (x - 10);
}
// Given a sequence of UTF 16 code units, return true if all code units are
// ASCII characters
bool isAllASCII(const char16_t* utf16, size_t length) {
for (const char16_t* e = utf16 + length; utf16 != e; ++utf16) {
if (*utf16 > 0x7F)
return false;
}
return true;
}
// Given a sequences of UTF 16 code units, return a string that explicitly
// expresses the code units
std::string getUtf16CodeUnitString(const char16_t* utf16, size_t length) {
// Every character will need 4 hex digits + the character escape "\u".
// Plus 2 character for the opening and closing single quote.
std::string s = std::string(6 * length + 2, 0);
s.front() = '\'';
for (size_t i = 0; i != length; ++i) {
char16_t ch = utf16[i];
size_t start = (6 * i) + 1;
s[start] = '\\';
s[start + 1] = 'u';
s[start + 2] = hexDigit((ch >> 12) & 0x000f);
s[start + 3] = hexDigit((ch >> 8) & 0x000f);
s[start + 4] = hexDigit((ch >> 4) & 0x000f);
s[start + 5] = hexDigit(ch & 0x000f);
}
s.back() = '\'';
return s;
}
} // namespace
Buffer::~Buffer() = default;
MutableBuffer::~MutableBuffer() = default;
PreparedJavaScript::~PreparedJavaScript() = default;
Value HostObject::get(Runtime&, const PropNameID&) {
return Value();
}
void HostObject::set(Runtime& rt, const PropNameID& name, const Value&) {
std::string msg("TypeError: Cannot assign to property '");
msg += name.utf8(rt);
msg += "' on HostObject with default setter";
throw JSError(rt, msg);
}
HostObject::~HostObject() {}
NativeState::~NativeState() {}
Runtime::~Runtime() {}
ICast* Runtime::castInterface(const UUID& /*interfaceUUID*/) {
return nullptr;
}
Instrumentation& Runtime::instrumentation() {
class NoInstrumentation : public Instrumentation {
std::string getRecordedGCStats() override {
return "";
}
std::unordered_map<std::string, int64_t> getHeapInfo(bool) override {
return std::unordered_map<std::string, int64_t>{};
}
void collectGarbage(std::string) override {}
void startTrackingHeapObjectStackTraces(
std::function<void(
uint64_t,
std::chrono::microseconds,
std::vector<HeapStatsUpdate>)>) override {}
void stopTrackingHeapObjectStackTraces() override {}
void startHeapSampling(size_t) override {}
void stopHeapSampling(std::ostream&) override {}
void createSnapshotToFile(
const std::string& /*path*/,
const HeapSnapshotOptions& /*options*/) override {
throw JSINativeException(
"Default instrumentation cannot create a heap snapshot");
}
void createSnapshotToStream(
std::ostream& /*os*/,
const HeapSnapshotOptions& /*options*/) override {
throw JSINativeException(
"Default instrumentation cannot create a heap snapshot");
}
std::string flushAndDisableBridgeTrafficTrace() override {
std::abort();
}
void writeBasicBlockProfileTraceToFile(const std::string&) const override {
std::abort();
}
void dumpOpcodeStats(std::ostream&) const override {
std::abort();
}
void dumpProfilerSymbolsToFile(const std::string&) const override {
std::abort();
}
};
static NoInstrumentation sharedInstance;
return sharedInstance;
}
Value Runtime::createValueFromJsonUtf8(const uint8_t* json, size_t length) {
Function parseJson = global()
.getPropertyAsObject(*this, "JSON")
.getPropertyAsFunction(*this, "parse");
return parseJson.call(*this, String::createFromUtf8(*this, json, length));
}
String Runtime::createStringFromUtf16(const char16_t* utf16, size_t length) {
if (isAllASCII(utf16, length)) {
std::string buffer(utf16, utf16 + length);
return createStringFromAscii(buffer.data(), length);
}
auto s = getUtf16CodeUnitString(utf16, length);
return global()
.getPropertyAsFunction(*this, "eval")
.call(*this, s)
.getString(*this);
}
PropNameID Runtime::createPropNameIDFromUtf16(
const char16_t* utf16,
size_t length) {
auto jsString = createStringFromUtf16(utf16, length);
return createPropNameIDFromString(jsString);
}
std::u16string Runtime::utf16(const PropNameID& sym) {
auto utf8Str = utf8(sym);
return convertUTF8ToUTF16(utf8Str);
}
std::u16string Runtime::utf16(const String& str) {
auto utf8Str = utf8(str);
return convertUTF8ToUTF16(utf8Str);
}
void Runtime::getStringData(
const jsi::String& str,
void* ctx,
void (*cb)(void* ctx, bool ascii, const void* data, size_t num)) {
auto utf16Str = utf16(str);
cb(ctx, false, utf16Str.data(), utf16Str.size());
}
void Runtime::getPropNameIdData(
const jsi::PropNameID& sym,
void* ctx,
void (*cb)(void* ctx, bool ascii, const void* data, size_t num)) {
auto utf16Str = utf16(sym);
cb(ctx, false, utf16Str.data(), utf16Str.size());
}
void Runtime::setPrototypeOf(const Object& object, const Value& prototype) {
auto setPrototypeOfFn = global()
.getPropertyAsObject(*this, "Object")
.getPropertyAsFunction(*this, "setPrototypeOf");
setPrototypeOfFn.call(*this, object, prototype).asObject(*this);
}
Value Runtime::getPrototypeOf(const Object& object) {
auto setPrototypeOfFn = global()
.getPropertyAsObject(*this, "Object")
.getPropertyAsFunction(*this, "getPrototypeOf");
return setPrototypeOfFn.call(*this, object);
}
Object Runtime::createObjectWithPrototype(const Value& prototype) {
auto createFn = global()
.getPropertyAsObject(*this, "Object")
.getPropertyAsFunction(*this, "create");
return createFn.call(*this, prototype).asObject(*this);
}
void Runtime::deleteProperty(const Object& object, const PropNameID& name) {
auto nameStr = String::createFromUtf16(*this, name.utf16(*this));
auto deleteFn = global()
.getPropertyAsObject(*this, "Reflect")
.getPropertyAsFunction(*this, "deleteProperty");
auto res = deleteFn.call(*this, object, nameStr).getBool();
if (!res) {
throw JSError(*this, "Failed to delete property");
}
}
void Runtime::deleteProperty(const Object& object, const String& name) {
auto deleteFn = global()
.getPropertyAsObject(*this, "Reflect")
.getPropertyAsFunction(*this, "deleteProperty");
auto res = deleteFn.call(*this, object, name).getBool();
if (!res) {
throw JSError(*this, "Failed to delete property");
}
}
void Runtime::deleteProperty(const Object& object, const Value& name) {
auto deleteFn = global()
.getPropertyAsObject(*this, "Reflect")
.getPropertyAsFunction(*this, "deleteProperty");
auto res = deleteFn.call(*this, object, name).getBool();
if (!res) {
throw JSError(*this, "Failed to delete property");
}
}
void Runtime::setRuntimeDataImpl(
const UUID& uuid,
const void* data,
void (*deleter)(const void* data)) {
auto& runtimeDataGlobal = getRuntimeDataGlobal();
std::lock_guard<std::mutex> lock(runtimeDataGlobal.mutex_);
if (auto it = runtimeDataGlobal.dataMap_.find(this);
it != runtimeDataGlobal.dataMap_.end()) {
auto& map = it->second;
if (auto entryIt = map.find(uuid); entryIt != map.end()) {
// Free the old data
auto oldData = entryIt->second.first;
auto oldDataDeleter = entryIt->second.second;
oldDataDeleter(oldData);
}
map[uuid] = {data, deleter};
return;
}
// No custom data entry exist for this runtime in the global map, so create
// one.
runtimeDataGlobal.dataMap_[this][uuid] = {data, deleter};
// The first time data is added for this runtime is added to the map, install
// a host object on the global object of the runtime. This host object is used
// to release the runtime's entry from the global custom data map when the
// runtime is destroyed.
// Also, try to protect the host object by making it non-configurable,
// non-enumerable, and non-writable. These JSI operations are purposely
// performed after runtime-specific data map is added and the host object is
// created to prevent data leaks if any operations fail.
Object ho = Object::createFromHostObject(
*this, std::make_shared<RemoveRuntimeDataHostObject>(this));
global().setProperty(*this, "_jsiRuntimeDataCleanUp", ho);
auto definePropertyFn = global()
.getPropertyAsObject(*this, "Object")
.getPropertyAsFunction(*this, "defineProperty");
auto desc = Object(*this);
desc.setProperty(*this, "configurable", Value(false));
desc.setProperty(*this, "enumerable", Value(false));
desc.setProperty(*this, "writable", Value(false));
definePropertyFn.call(*this, global(), "_jsiRuntimeDataCleanUp", desc);
}
const void* Runtime::getRuntimeDataImpl(const UUID& uuid) {
auto& runtimeDataGlobal = getRuntimeDataGlobal();
std::lock_guard<std::mutex> lock(runtimeDataGlobal.mutex_);
if (auto runtimeMapIt = runtimeDataGlobal.dataMap_.find(this);
runtimeMapIt != runtimeDataGlobal.dataMap_.end()) {
if (auto customDataIt = runtimeMapIt->second.find(uuid);
customDataIt != runtimeMapIt->second.end()) {
return customDataIt->second.first;
}
}
return nullptr;
}
Value Runtime::getProperty(const Object& object, const Value& name) {
auto getFn = global()
.getPropertyAsObject(*this, "Reflect")
.getPropertyAsFunction(*this, "get");
return getFn.call(*this, object, name);
}
bool Runtime::hasProperty(const Object& object, const Value& name) {
auto hasFn = global()
.getPropertyAsObject(*this, "Reflect")
.getPropertyAsFunction(*this, "has");
return hasFn.call(*this, object, name).getBool();
}
void Runtime::setPropertyValue(
const Object& object,
const Value& name,
const Value& value) {
auto setFn = global()
.getPropertyAsObject(*this, "Reflect")
.getPropertyAsFunction(*this, "set");
auto setResult = setFn.call(*this, object, name, value).getBool();
if (!setResult) {
throw JSError(*this, "Failed to set the property");
}
}
Pointer& Pointer::operator=(Pointer&& other) noexcept {
if (ptr_) {
ptr_->invalidate();
}
ptr_ = other.ptr_;
other.ptr_ = nullptr;
return *this;
}
Object Object::getPropertyAsObject(Runtime& runtime, const char* name) const {
Value v = getProperty(runtime, name);
if (!v.isObject()) {
throw JSError(
runtime,
std::string("getPropertyAsObject: property '") + name + "' is " +
kindToString(v, &runtime) + ", expected an Object");
}
return v.getObject(runtime);
}
Function Object::getPropertyAsFunction(Runtime& runtime, const char* name)
const {
Object obj = getPropertyAsObject(runtime, name);
if (!obj.isFunction(runtime)) {
throw JSError(
runtime,
std::string("getPropertyAsFunction: property '") + name + "' is " +
kindToString(std::move(obj), &runtime) + ", expected a Function");
};
return std::move(obj).getFunction(runtime);
}
Array Object::asArray(Runtime& runtime) const& {
if (!isArray(runtime)) {
throw JSError(
runtime,
"Object is " + kindToString(Value(runtime, *this), &runtime) +
", expected an array");
}
return getArray(runtime);
}
Array Object::asArray(Runtime& runtime) && {
if (!isArray(runtime)) {
throw JSError(
runtime,
"Object is " + kindToString(Value(runtime, *this), &runtime) +
", expected an array");
}
return std::move(*this).getArray(runtime);
}
Function Object::asFunction(Runtime& runtime) const& {
if (!isFunction(runtime)) {
throw JSError(
runtime,
"Object is " + kindToString(Value(runtime, *this), &runtime) +
", expected a function");
}
return getFunction(runtime);
}
Function Object::asFunction(Runtime& runtime) && {
if (!isFunction(runtime)) {
throw JSError(
runtime,
"Object is " + kindToString(Value(runtime, *this), &runtime) +
", expected a function");
}
return std::move(*this).getFunction(runtime);
}
Value::Value(Value&& other) noexcept : Value(other.kind_) {
if (kind_ == BooleanKind) {
data_.boolean = other.data_.boolean;
} else if (kind_ == NumberKind) {
data_.number = other.data_.number;
} else if (kind_ >= PointerKind) {
new (&data_.pointer) Pointer(std::move(other.data_.pointer));
}
// when the other's dtor runs, nothing will happen.
other.kind_ = UndefinedKind;
}
Value::Value(Runtime& runtime, const Value& other) : Value(other.kind_) {
// data_ is uninitialized, so use placement new to create non-POD
// types in it. Any other kind of initialization will call a dtor
// first, which is incorrect.
if (kind_ == BooleanKind) {
data_.boolean = other.data_.boolean;
} else if (kind_ == NumberKind) {
data_.number = other.data_.number;
} else if (kind_ == SymbolKind) {
new (&data_.pointer) Pointer(runtime.cloneSymbol(other.data_.pointer.ptr_));
} else if (kind_ == BigIntKind) {
new (&data_.pointer) Pointer(runtime.cloneBigInt(other.data_.pointer.ptr_));
} else if (kind_ == StringKind) {
new (&data_.pointer) Pointer(runtime.cloneString(other.data_.pointer.ptr_));
} else if (kind_ >= ObjectKind) {
new (&data_.pointer) Pointer(runtime.cloneObject(other.data_.pointer.ptr_));
}
}
Value::~Value() {
if (kind_ >= PointerKind) {
data_.pointer.~Pointer();
}
}
bool Value::strictEquals(Runtime& runtime, const Value& a, const Value& b) {
if (a.kind_ != b.kind_) {
return false;
}
switch (a.kind_) {
case UndefinedKind:
case NullKind:
return true;
case BooleanKind:
return a.data_.boolean == b.data_.boolean;
case NumberKind:
return a.data_.number == b.data_.number;
case SymbolKind:
return runtime.strictEquals(
static_cast<const Symbol&>(a.data_.pointer),
static_cast<const Symbol&>(b.data_.pointer));
case BigIntKind:
return runtime.strictEquals(
static_cast<const BigInt&>(a.data_.pointer),
static_cast<const BigInt&>(b.data_.pointer));
case StringKind:
return runtime.strictEquals(
static_cast<const String&>(a.data_.pointer),
static_cast<const String&>(b.data_.pointer));
case ObjectKind:
return runtime.strictEquals(
static_cast<const Object&>(a.data_.pointer),
static_cast<const Object&>(b.data_.pointer));
}
return false;
}
bool Value::asBool() const {
if (!isBool()) {
throw JSINativeException(
"Value is " + kindToString(*this) + ", expected a boolean");
}
return getBool();
}
double Value::asNumber() const {
if (!isNumber()) {
throw JSINativeException(
"Value is " + kindToString(*this) + ", expected a number");
}
return getNumber();
}
Object Value::asObject(Runtime& rt) const& {
if (!isObject()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected an Object");
}
return getObject(rt);
}
Object Value::asObject(Runtime& rt) && {
if (!isObject()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected an Object");
}
auto ptr = data_.pointer.ptr_;
data_.pointer.ptr_ = nullptr;
return static_cast<Object>(ptr);
}
Symbol Value::asSymbol(Runtime& rt) const& {
if (!isSymbol()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected a Symbol");
}
return getSymbol(rt);
}
Symbol Value::asSymbol(Runtime& rt) && {
if (!isSymbol()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected a Symbol");
}
return std::move(*this).getSymbol(rt);
}
BigInt Value::asBigInt(Runtime& rt) const& {
if (!isBigInt()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected a BigInt");
}
return getBigInt(rt);
}
BigInt Value::asBigInt(Runtime& rt) && {
if (!isBigInt()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected a BigInt");
}
return std::move(*this).getBigInt(rt);
}
String Value::asString(Runtime& rt) const& {
if (!isString()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected a String");
}
return getString(rt);
}
String Value::asString(Runtime& rt) && {
if (!isString()) {
throw JSError(
rt, "Value is " + kindToString(*this, &rt) + ", expected a String");
}
return std::move(*this).getString(rt);
}
String Value::toString(Runtime& runtime) const {
Function toString = runtime.global().getPropertyAsFunction(runtime, "String");
return toString.call(runtime, *this).getString(runtime);
}
uint64_t BigInt::asUint64(Runtime& runtime) const {
if (!isUint64(runtime)) {
throw JSError(runtime, "Lossy truncation in BigInt64::asUint64");
}
return getUint64(runtime);
}
int64_t BigInt::asInt64(Runtime& runtime) const {
if (!isInt64(runtime)) {
throw JSError(runtime, "Lossy truncation in BigInt64::asInt64");
}
return getInt64(runtime);
}
Array Array::createWithElements(
Runtime& rt,
std::initializer_list<Value> elements) {
Array result(rt, elements.size());
size_t index = 0;
for (const auto& element : elements) {
result.setValueAtIndex(rt, index++, element);
}
return result;
}
std::vector<PropNameID> HostObject::getPropertyNames(Runtime&) {
return {};
}
Runtime::ScopeState* Runtime::pushScope() {
return nullptr;
}
void Runtime::popScope(ScopeState*) {}
JSError::JSError(Runtime& rt, Value&& value) {
setValue(rt, std::move(value));
}
JSError::JSError(Runtime& rt, std::string msg) : message_(std::move(msg)) {
try {
setValue(
rt,
callGlobalFunction(rt, "Error", String::createFromUtf8(rt, message_)));
} catch (const JSIException& ex) {
message_ = std::string(ex.what()) + " (while raising " + message_ + ")";
setValue(rt, String::createFromUtf8(rt, message_));
}
}
JSError::JSError(Runtime& rt, std::string msg, std::string stack)
: message_(std::move(msg)), stack_(std::move(stack)) {
try {
Object e(rt);
e.setProperty(rt, "message", String::createFromUtf8(rt, message_));
e.setProperty(rt, "stack", String::createFromUtf8(rt, stack_));
setValue(rt, std::move(e));
} catch (const JSIException& ex) {
setValue(rt, String::createFromUtf8(rt, ex.what()));
}
}
JSError::JSError(std::string what, Runtime& rt, Value&& value)
: JSIException(std::move(what)) {
setValue(rt, std::move(value));
}
JSError::JSError(Value&& value, std::string message, std::string stack)
: JSIException(message + "\n\n" + stack),
value_(std::make_shared<Value>(std::move(value))),
message_(std::move(message)),
stack_(std::move(stack)) {}
void JSError::setValue(Runtime& rt, Value&& value) {
value_ = std::make_shared<Value>(std::move(value));
if ((message_.empty() || stack_.empty()) && value_->isObject()) {
auto obj = value_->getObject(rt);
if (message_.empty()) {
try {
Value message = obj.getProperty(rt, "message");
if (!message.isUndefined() && !message.isString()) {
message = callGlobalFunction(rt, "String", message);
}
if (message.isString()) {
message_ = message.getString(rt).utf8(rt);
} else if (!message.isUndefined()) {
message_ = "String(e.message) is a " + kindToString(message, &rt);
}
} catch (const JSIException& ex) {
message_ = std::string("[Exception while creating message string: ") +
ex.what() + "]";
}
}
if (stack_.empty()) {
try {
Value stack = obj.getProperty(rt, "stack");
if (!stack.isUndefined() && !stack.isString()) {
stack = callGlobalFunction(rt, "String", stack);
}
if (stack.isString()) {
stack_ = stack.getString(rt).utf8(rt);
} else if (!stack.isUndefined()) {
stack_ = "String(e.stack) is a " + kindToString(stack, &rt);
}
} catch (const JSIException& ex) {
message_ = std::string("[Exception while creating stack string: ") +
ex.what() + "]";
}
}
}
if (message_.empty()) {
try {
if (value_->isString()) {
message_ = value_->getString(rt).utf8(rt);
} else {
Value message = callGlobalFunction(rt, "String", *value_);
if (message.isString()) {
message_ = message.getString(rt).utf8(rt);
} else {
message_ = "String(e) is a " + kindToString(message, &rt);
}
}
} catch (const JSIException& ex) {
message_ = std::string("[Exception while creating message string: ") +
ex.what() + "]";
}
}
if (stack_.empty()) {
stack_ = "no stack";
}
if (what_.empty()) {
what_ = message_ + "\n\n" + stack_;
}
}
JSIException::~JSIException() {}
JSINativeException::~JSINativeException() {}
JSError::~JSError() {}
} // namespace jsi
} // namespace facebook

1864
node_modules/react-native/ReactCommon/jsi/jsi/jsi.h generated vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,108 @@
/*
* 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.
*/
#ifndef _WINDOWS
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <cerrno>
#include <cstdarg>
#include <stdexcept>
#include <jsi/jsilib.h>
namespace facebook {
namespace jsi {
namespace {
constexpr size_t kErrorBufferSize = 512;
__attribute__((format(printf, 1, 2))) void throwFormattedError(
const char* fmt,
...) {
char logBuffer[kErrorBufferSize];
va_list va_args;
va_start(va_args, fmt);
int result = vsnprintf(logBuffer, sizeof(logBuffer), fmt, va_args);
va_end(va_args);
if (result < 0) {
throw JSINativeException(
std::string("Failed to format error message: ") + fmt);
}
throw JSINativeException(logBuffer);
}
class ScopedFile {
public:
ScopedFile(const std::string& path)
: path_(path), fd_(::open(path.c_str(), O_RDONLY)) {
if (fd_ == -1) {
throwFormattedError(
"Could not open %s: %s", path.c_str(), strerror(errno));
}
}
~ScopedFile() {
::close(fd_);
}
size_t size() {
struct stat fileInfo;
if (::fstat(fd_, &fileInfo) == -1) {
throwFormattedError(
"Could not stat %s: %s", path_.c_str(), strerror(errno));
}
return fileInfo.st_size;
}
uint8_t* mmap(size_t size) {
void* result = ::mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd_, 0);
if (result == MAP_FAILED) {
throwFormattedError(
"Could not mmap %s: %s", path_.c_str(), strerror(errno));
}
return reinterpret_cast<uint8_t*>(result);
}
const std::string& path_;
const int fd_;
};
} // namespace
FileBuffer::FileBuffer(const std::string& path) {
ScopedFile file(path);
size_ = file.size();
data_ = file.mmap(size_);
}
FileBuffer::~FileBuffer() {
if (::munmap(data_, size_)) {
// terminate the program with pending exception
try {
throwFormattedError(
"Could not unmap memory (%p, %zu bytes): %s",
data_,
size_,
strerror(errno));
} catch (...) {
std::terminate();
}
}
}
} // namespace jsi
} // namespace facebook
#endif // !defined(_WINDOWS)

View File

@@ -0,0 +1,27 @@
/*
* 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.
*/
#ifdef _WINDOWS
#include <jsi/jsilib.h>
namespace facebook {
namespace jsi {
FileBuffer::FileBuffer(const std::string&) {
// TODO(T41045067) Implement this on Windows
throw new JSINativeException("FileBuffer is not implemented on Windows");
}
FileBuffer::~FileBuffer() {
assert(false && "FileBuffer is not implemented on Windows");
}
} // namespace jsi
} // namespace facebook
#endif //_WINDOWS

59
node_modules/react-native/ReactCommon/jsi/jsi/jsilib.h generated vendored Normal file
View File

@@ -0,0 +1,59 @@
/*
* 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 {
namespace jsi {
class FileBuffer : public Buffer {
public:
FileBuffer(const std::string& path);
~FileBuffer() override;
size_t size() const override {
return size_;
}
const uint8_t* data() const override {
return data_;
}
private:
size_t size_;
uint8_t* data_;
};
// A trivial implementation of PreparedJavaScript that simply stores the source
// buffer and URL.
class SourceJavaScriptPreparation final : public jsi::PreparedJavaScript,
public jsi::Buffer {
std::shared_ptr<const jsi::Buffer> buf_;
std::string sourceURL_;
public:
SourceJavaScriptPreparation(
std::shared_ptr<const jsi::Buffer> buf,
std::string sourceURL)
: buf_(std::move(buf)), sourceURL_(std::move(sourceURL)) {}
const std::string& sourceURL() const {
return sourceURL_;
}
size_t size() const override {
return buf_->size();
}
const uint8_t* data() const override {
return buf_->data();
}
};
} // namespace jsi
} // namespace facebook

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,49 @@
/*
* 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 <functional>
#include <memory>
#include <vector>
#include <gtest/gtest.h>
#include <jsi/jsi.h>
namespace facebook {
namespace jsi {
class Runtime;
using RuntimeFactory = std::function<std::shared_ptr<Runtime>()>;
std::vector<RuntimeFactory> runtimeGenerators();
class JSITestBase : public ::testing::TestWithParam<RuntimeFactory> {
public:
JSITestBase() : factory(GetParam()), runtime(factory()), rt(*runtime) {}
Value eval(const char* code) {
return rt.global().getPropertyAsFunction(rt, "eval").call(rt, code);
}
Function function(const std::string& code) {
return eval(("(" + code + ")").c_str()).getObject(rt).getFunction(rt);
}
bool checkValue(const Value& value, const std::string& jsValue) {
return function("function(value) { return value == " + jsValue + "; }")
.call(rt, std::move(value))
.getBool();
}
RuntimeFactory factory;
std::shared_ptr<Runtime> runtime;
Runtime& rt;
};
} // namespace jsi
} // namespace facebook

View File

@@ -0,0 +1,79 @@
/*
* 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 <mutex>
#include <jsi/decorator.h>
#include <jsi/jsi.h>
namespace facebook {
namespace jsi {
class ThreadSafeRuntime : public Runtime {
public:
virtual void lock() const = 0;
virtual void unlock() const = 0;
virtual Runtime& getUnsafeRuntime() = 0;
};
namespace detail {
template <typename R, typename L>
struct WithLock {
L lock;
WithLock(R& r) : lock(r) {}
void before() {
lock.lock();
}
void after() {
lock.unlock();
}
};
// The actual implementation of a given ThreadSafeRuntime. It's parameterized
// by:
//
// - R: The actual Runtime type that this wraps
// - L: A lock type that has three members:
// - L(R& r) // ctor
// - void lock()
// - void unlock()
template <typename R, typename L>
class ThreadSafeRuntimeImpl final
: public WithRuntimeDecorator<WithLock<R, L>, R, ThreadSafeRuntime> {
public:
template <typename... Args>
ThreadSafeRuntimeImpl(Args&&... args)
: WithRuntimeDecorator<WithLock<R, L>, R, ThreadSafeRuntime>(
unsafe_,
lock_),
unsafe_(std::forward<Args>(args)...),
lock_(unsafe_) {}
R& getUnsafeRuntime() override {
return WithRuntimeDecorator<WithLock<R, L>, R, ThreadSafeRuntime>::plain();
}
void lock() const override {
lock_.before();
}
void unlock() const override {
lock_.after();
}
private:
R unsafe_;
mutable WithLock<R, L> lock_;
};
} // namespace detail
} // namespace jsi
} // namespace facebook