From e95059ae31f020c877b90533dc28d0a657d61f27 Mon Sep 17 00:00:00 2001 From: tudorms <48035227+tudorms@users.noreply.github.com> Date: Fri, 13 Oct 2023 17:58:53 -0700 Subject: [PATCH] Bring in updated headers from microsoft/node-api-jsi (#185) --- ReactNative.V8Jsi.Windows.nuspec | 2 +- config.json | 2 +- src/jsi/CMakeLists.txt | 4 ++ src/jsi/jsi.h | 6 +-- src/node-api-jsi/ApiLoaders/JSRuntimeApi.cpp | 2 +- src/node-api-jsi/NodeApiJsiRuntime.cpp | 45 +++++++++++++++----- src/node-api-jsi/NodeApiJsiRuntime.h | 4 +- src/node-api/js_runtime_api.h | 2 +- 8 files changed, 48 insertions(+), 19 deletions(-) diff --git a/ReactNative.V8Jsi.Windows.nuspec b/ReactNative.V8Jsi.Windows.nuspec index 04d33c1..25b559d 100644 --- a/ReactNative.V8Jsi.Windows.nuspec +++ b/ReactNative.V8Jsi.Windows.nuspec @@ -13,7 +13,7 @@ - + diff --git a/config.json b/config.json index 3bd76de..7f123f8 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,5 @@ { - "version": "0.71.10", + "version": "0.71.11", "v8ref": "refs/branch-heads/11.8", "buildNumber": "172" } diff --git a/src/jsi/CMakeLists.txt b/src/jsi/CMakeLists.txt index 3d2959e..ed9bae8 100644 --- a/src/jsi/CMakeLists.txt +++ b/src/jsi/CMakeLists.txt @@ -20,10 +20,14 @@ 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") + list(APPEND jsi_compile_flags "/Zi") + list(APPEND jsi_compile_flags "/Qspectre") + list(APPEND jsi_compile_flags "/sdl") endif() if (HERMES_ENABLE_BITCODE) list(APPEND jsi_compile_flags "-fembed-bitcode") endif () + target_compile_options(jsi PRIVATE ${jsi_compile_flags}) install(DIRECTORY "${PROJECT_SOURCE_DIR}/API/jsi/" DESTINATION include diff --git a/src/jsi/jsi.h b/src/jsi/jsi.h index 9c65788..36c90e0 100644 --- a/src/jsi/jsi.h +++ b/src/jsi/jsi.h @@ -1556,9 +1556,9 @@ class JSI_EXPORT JSError : public JSIException { return *value_; } - /// In V8's case, creating an Error object in JS doesn't record the callstack. - /// To preserve it, we need a way to manually add the stack here and on the JS - /// side. + // In V8's case, creating an Error object in JS doesn't record the callstack. + // To preserve it, we need a way to manually add the stack here and on the JS + // side. void setStack(std::string stack) { stack_ = std::move(stack); what_ = message_ + "\n\n" + stack_; diff --git a/src/node-api-jsi/ApiLoaders/JSRuntimeApi.cpp b/src/node-api-jsi/ApiLoaders/JSRuntimeApi.cpp index cad9c29..e11865a 100644 --- a/src/node-api-jsi/ApiLoaders/JSRuntimeApi.cpp +++ b/src/node-api-jsi/ApiLoaders/JSRuntimeApi.cpp @@ -37,7 +37,7 @@ struct JSRuntimeApiNames { #include "JSRuntimeApi.inc" }; -// Prepared script function either should be all loaded or we use all default functions. +// Prepared script functions either should be all loaded or we use all default functions. void loadPreparedScriptFuncs() { JSRuntimeApi *current = JSRuntimeApi::current(); bool useDefault = false; diff --git a/src/node-api-jsi/NodeApiJsiRuntime.cpp b/src/node-api-jsi/NodeApiJsiRuntime.cpp index 6169764..e520fa7 100644 --- a/src/node-api-jsi/NodeApiJsiRuntime.cpp +++ b/src/node-api-jsi/NodeApiJsiRuntime.cpp @@ -7,13 +7,36 @@ #include #include -#include #include #include #include #include #include +// JSI version defines set of features available in the API. +// Each significant API change must be under a new version. +// These macros must be defined in jsi.h, but define them here too +// in case if this code is used with unmodified jsi.h. +#ifndef JSI_VERSION +#define JSI_VERSION 10 +#endif + +#ifndef JSI_NO_CONST_3 +#if JSI_VERSION >= 3 +#define JSI_NO_CONST_3 +#else +#define JSI_NO_CONST_3 const +#endif +#endif + +#ifndef JSI_CONST_10 +#if JSI_VERSION >= 10 +#define JSI_CONST_10 const +#else +#define JSI_CONST_10 +#endif +#endif + using namespace facebook; using namespace std::string_view_literals; @@ -64,7 +87,7 @@ using namespace std::string_view_literals; } \ } while (false) -#if 0 +#ifdef __cpp_lib_span #include #endif // __cpp_lib_span @@ -72,19 +95,21 @@ namespace Microsoft::NodeApiJsi { namespace { -#if 0 // TODO: additional changes required to switch to std::span +#ifdef __cpp_lib_span using std::span; #else /** * @brief A span of values that can be used to pass arguments to a function. * - * This should be replaced with std::span once C++ 2020 is supported. + * This should be replaced with std::span once C++20 is supported. */ template class span { public: - constexpr span(std::initializer_list il) noexcept : data_{const_cast(il.begin())}, size_{il.size()} {} + constexpr span() noexcept : data_{nullptr}, size_{0} {} constexpr span(T *data, size_t size) noexcept : data_{data}, size_{size} {} + template + constexpr span(T (&arr)[N]) noexcept : data_{arr}, size_{N} {} [[nodiscard]] constexpr T *data() const noexcept { return data_; @@ -1249,8 +1274,8 @@ jsi::Object NodeApiJsiRuntime::createObject(std::shared_ptr hos getProperty(getNodeApiValue(cachedValue_.Global), getNodeApiValue(propertyId_.Proxy)), NodeApiPointerValueKind::Object); } - napi_value proxy = - constructObject(getNodeApiValue(cachedValue_.ProxyConstructor), {obj, getHostObjectProxyHandler()}); + napi_value args[] = {obj, getHostObjectProxyHandler()}; + napi_value proxy = constructObject(getNodeApiValue(cachedValue_.ProxyConstructor), args); return makeJsiPointer(proxy); } @@ -1859,7 +1884,7 @@ size_t NodeApiJsiRuntime::JsiValueViewArgs::size() const noexcept { // TODO: account for symbol NodeApiJsiRuntime::PropNameIDView::PropNameIDView(NodeApiJsiRuntime * /*runtime*/, napi_value propertyId) noexcept - : propertyId_{make(new (std::addressof( + : propertyId_{make(new(std::addressof( pointerStore_)) NodeApiStackOnlyPointerValue(propertyId, NodeApiPointerValueKind::StringPropNameID))} {} NodeApiJsiRuntime::PropNameIDView::operator jsi::PropNameID const &() const noexcept { @@ -2166,14 +2191,14 @@ std::string NodeApiJsiRuntime::symbolToStdString(napi_value symbolValue) { // Calls a JavaScript function. napi_value NodeApiJsiRuntime::callFunction(napi_value thisArg, napi_value function, span args) const { napi_value result{}; - CHECK_NAPI(jsrApi_->napi_call_function(env_, thisArg, function, args.size(), args.begin(), &result)); + CHECK_NAPI(jsrApi_->napi_call_function(env_, thisArg, function, args.size(), args.data(), &result)); return result; } // Constructs a new JavaScript Object using a constructor function. napi_value NodeApiJsiRuntime::constructObject(napi_value constructor, span args) const { napi_value result{}; - CHECK_NAPI(jsrApi_->napi_new_instance(env_, constructor, args.size(), args.begin(), &result)); + CHECK_NAPI(jsrApi_->napi_new_instance(env_, constructor, args.size(), args.data(), &result)); return result; } diff --git a/src/node-api-jsi/NodeApiJsiRuntime.h b/src/node-api-jsi/NodeApiJsiRuntime.h index 6895066..7fd2b6d 100644 --- a/src/node-api-jsi/NodeApiJsiRuntime.h +++ b/src/node-api-jsi/NodeApiJsiRuntime.h @@ -17,14 +17,14 @@ makeNodeApiJsiRuntime(napi_env env, JSRuntimeApi *jsrApi, std::function struct NodeApiEnvScope { NodeApiEnvScope(napi_env env) : env_(env) { - jsr_open_napi_env_scope(env, &scope_); + JSRuntimeApi::current()->jsr_open_napi_env_scope(env, &scope_); } NodeApiEnvScope(const NodeApiEnvScope &) = delete; NodeApiEnvScope &operator=(const NodeApiEnvScope &) = delete; ~NodeApiEnvScope() { - jsr_close_napi_env_scope(env_, scope_); + JSRuntimeApi::current()->jsr_close_napi_env_scope(env_, scope_); } private: diff --git a/src/node-api/js_runtime_api.h b/src/node-api/js_runtime_api.h index 88ce352..d16e33e 100644 --- a/src/node-api/js_runtime_api.h +++ b/src/node-api/js_runtime_api.h @@ -149,7 +149,7 @@ JSR_API jsr_run_script(napi_env env, // Prepare the script for running. JSR_API jsr_create_prepared_script(napi_env env, - const uint8_t* script_utf8, + const uint8_t* script_data, size_t script_length, jsr_data_delete_cb script_delete_cb, void* deleter_data,