From 63f04a0049527d86888570365bbc26c6a880a135 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Fri, 10 Nov 2023 14:17:24 -0800 Subject: [PATCH] node-api: factor out common code into macros * Create macro for checking new string arguments. * Create macro for combining env check and inside-gc check. --- src/js_native_api_v8.cc | 197 +++++++++++++++------------------------- src/js_native_api_v8.h | 9 +- src/node_api.cc | 21 ++--- 3 files changed, 86 insertions(+), 141 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 7f96a7edd82ef7..54d0c2a70b215b 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -38,6 +38,17 @@ #define CHECK_NEW_FROM_UTF8(env, result, str) \ CHECK_NEW_FROM_UTF8_LEN((env), (result), (str), NAPI_AUTO_LENGTH) +#define CHECK_NEW_STRING_ARGS(env, str, length, result) \ + do { \ + CHECK_ENV_NOT_IN_GC((env)); \ + if ((length) > 0) CHECK_ARG((env), (str)); \ + CHECK_ARG((env), (result)); \ + RETURN_STATUS_IF_FALSE( \ + (env), \ + ((length) == NAPI_AUTO_LENGTH) || (length) <= INT_MAX, \ + napi_invalid_arg); \ + } while (0) + #define CREATE_TYPED_ARRAY( \ env, type, size_of_element, buffer, byte_offset, length, out) \ do { \ @@ -81,12 +92,7 @@ napi_status NewString(napi_env env, size_t length, napi_value* result, StringMaker string_maker) { - CHECK_ENV(env); - env->CheckGCAccess(); - if (length > 0) CHECK_ARG(env, str); - CHECK_ARG(env, result); - RETURN_STATUS_IF_FALSE( - env, (length == NAPI_AUTO_LENGTH) || length <= INT_MAX, napi_invalid_arg); + CHECK_NEW_STRING_ARGS(env, str, length, result); auto isolate = env->isolate; auto str_maybe = string_maker(isolate); @@ -105,9 +111,10 @@ napi_status NewExternalString(napi_env env, bool* copied, CreateAPI create_api, StringMaker string_maker) { + CHECK_NEW_STRING_ARGS(env, str, length, result); + napi_status status; #if defined(V8_ENABLE_SANDBOX) - env->CheckGCAccess(); status = create_api(env, str, length, result); if (status == napi_ok) { if (copied != nullptr) { @@ -1467,8 +1474,7 @@ napi_status NAPI_CDECL napi_object_seal(napi_env env, napi_value object) { napi_status NAPI_CDECL napi_is_array(napi_env env, napi_value value, bool* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -1528,8 +1534,7 @@ napi_status NAPI_CDECL napi_get_prototype(napi_env env, } napi_status NAPI_CDECL napi_create_object(napi_env env, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue(v8::Object::New(env->isolate)); @@ -1538,8 +1543,7 @@ napi_status NAPI_CDECL napi_create_object(napi_env env, napi_value* result) { } napi_status NAPI_CDECL napi_create_array(napi_env env, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue(v8::Array::New(env->isolate)); @@ -1550,8 +1554,7 @@ napi_status NAPI_CDECL napi_create_array(napi_env env, napi_value* result) { napi_status NAPI_CDECL napi_create_array_with_length(napi_env env, size_t length, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = @@ -1651,8 +1654,7 @@ node_api_create_external_string_utf16(napi_env env, napi_status NAPI_CDECL napi_create_double(napi_env env, double value, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = @@ -1664,8 +1666,7 @@ napi_status NAPI_CDECL napi_create_double(napi_env env, napi_status NAPI_CDECL napi_create_int32(napi_env env, int32_t value, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = @@ -1677,8 +1678,7 @@ napi_status NAPI_CDECL napi_create_int32(napi_env env, napi_status NAPI_CDECL napi_create_uint32(napi_env env, uint32_t value, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue( @@ -1690,8 +1690,7 @@ napi_status NAPI_CDECL napi_create_uint32(napi_env env, napi_status NAPI_CDECL napi_create_int64(napi_env env, int64_t value, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue( @@ -1703,8 +1702,7 @@ napi_status NAPI_CDECL napi_create_int64(napi_env env, napi_status NAPI_CDECL napi_create_bigint_int64(napi_env env, int64_t value, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = @@ -1716,8 +1714,7 @@ napi_status NAPI_CDECL napi_create_bigint_int64(napi_env env, napi_status NAPI_CDECL napi_create_bigint_uint64(napi_env env, uint64_t value, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue( @@ -1732,7 +1729,6 @@ napi_status NAPI_CDECL napi_create_bigint_words(napi_env env, const uint64_t* words, napi_value* result) { NAPI_PREAMBLE(env); - env->CheckGCAccess(); CHECK_ARG(env, words); CHECK_ARG(env, result); @@ -1752,8 +1748,7 @@ napi_status NAPI_CDECL napi_create_bigint_words(napi_env env, napi_status NAPI_CDECL napi_get_boolean(napi_env env, bool value, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); v8::Isolate* isolate = env->isolate; @@ -1770,8 +1765,7 @@ napi_status NAPI_CDECL napi_get_boolean(napi_env env, napi_status NAPI_CDECL napi_create_symbol(napi_env env, napi_value description, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); v8::Isolate* isolate = env->isolate; @@ -1793,8 +1787,7 @@ napi_status NAPI_CDECL node_api_symbol_for(napi_env env, const char* utf8description, size_t length, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); napi_value js_description_string; @@ -1839,8 +1832,7 @@ napi_status NAPI_CDECL napi_create_error(napi_env env, napi_value code, napi_value msg, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, msg); CHECK_ARG(env, result); @@ -1860,8 +1852,7 @@ napi_status NAPI_CDECL napi_create_type_error(napi_env env, napi_value code, napi_value msg, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, msg); CHECK_ARG(env, result); @@ -1881,8 +1872,7 @@ napi_status NAPI_CDECL napi_create_range_error(napi_env env, napi_value code, napi_value msg, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, msg); CHECK_ARG(env, result); @@ -1902,8 +1892,7 @@ napi_status NAPI_CDECL node_api_create_syntax_error(napi_env env, napi_value code, napi_value msg, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, msg); CHECK_ARG(env, result); @@ -1924,8 +1913,7 @@ napi_status NAPI_CDECL napi_typeof(napi_env env, napi_valuetype* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -1964,8 +1952,7 @@ napi_status NAPI_CDECL napi_typeof(napi_env env, } napi_status NAPI_CDECL napi_get_undefined(napi_env env, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue(v8::Undefined(env->isolate)); @@ -1974,8 +1961,7 @@ napi_status NAPI_CDECL napi_get_undefined(napi_env env, napi_value* result) { } napi_status NAPI_CDECL napi_get_null(napi_env env, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue(v8::Null(env->isolate)); @@ -2018,10 +2004,9 @@ napi_status NAPI_CDECL napi_get_cb_info( napi_status NAPI_CDECL napi_get_new_target(napi_env env, napi_callback_info cbinfo, napi_value* result) { - CHECK_ENV(env); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, cbinfo); CHECK_ARG(env, result); - env->CheckGCAccess(); v8impl::CallbackWrapper* info = reinterpret_cast(cbinfo); @@ -2063,8 +2048,7 @@ napi_status NAPI_CDECL napi_call_function(napi_env env, } napi_status NAPI_CDECL napi_get_global(napi_env env, napi_value* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue(env->context()->Global()); @@ -2161,8 +2145,7 @@ napi_status NAPI_CDECL napi_is_error(napi_env env, bool* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot // throw JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -2177,8 +2160,7 @@ napi_status NAPI_CDECL napi_get_value_double(napi_env env, double* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -2195,8 +2177,7 @@ napi_status NAPI_CDECL napi_get_value_int32(napi_env env, int32_t* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -2220,8 +2201,7 @@ napi_status NAPI_CDECL napi_get_value_uint32(napi_env env, uint32_t* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -2245,8 +2225,7 @@ napi_status NAPI_CDECL napi_get_value_int64(napi_env env, int64_t* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -2279,8 +2258,7 @@ napi_status NAPI_CDECL napi_get_value_bigint_int64(napi_env env, napi_value value, int64_t* result, bool* lossless) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); CHECK_ARG(env, lossless); @@ -2298,8 +2276,7 @@ napi_status NAPI_CDECL napi_get_value_bigint_uint64(napi_env env, napi_value value, uint64_t* result, bool* lossless) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); CHECK_ARG(env, lossless); @@ -2318,8 +2295,7 @@ napi_status NAPI_CDECL napi_get_value_bigint_words(napi_env env, int* sign_bit, size_t* word_count, uint64_t* words) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, word_count); @@ -2349,8 +2325,7 @@ napi_status NAPI_CDECL napi_get_value_bool(napi_env env, bool* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -2372,8 +2347,7 @@ napi_status NAPI_CDECL napi_get_value_bool(napi_env env, // The result argument is optional unless buf is NULL. napi_status NAPI_CDECL napi_get_value_string_latin1( napi_env env, napi_value value, char* buf, size_t bufsize, size_t* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); v8::Local val = v8impl::V8LocalValueFromJsValue(value); @@ -2411,8 +2385,7 @@ napi_status NAPI_CDECL napi_get_value_string_latin1( // The result argument is optional unless buf is NULL. napi_status NAPI_CDECL napi_get_value_string_utf8( napi_env env, napi_value value, char* buf, size_t bufsize, size_t* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); v8::Local val = v8impl::V8LocalValueFromJsValue(value); @@ -2453,8 +2426,7 @@ napi_status NAPI_CDECL napi_get_value_string_utf16(napi_env env, char16_t* buf, size_t bufsize, size_t* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); v8::Local val = v8impl::V8LocalValueFromJsValue(value); @@ -2640,8 +2612,7 @@ napi_status NAPI_CDECL napi_check_object_type_tag(napi_env env, napi_status NAPI_CDECL napi_get_value_external(napi_env env, napi_value value, void** result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -2661,8 +2632,7 @@ napi_status NAPI_CDECL napi_create_reference(napi_env env, napi_ref* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -2686,8 +2656,7 @@ napi_status NAPI_CDECL napi_create_reference(napi_env env, napi_status NAPI_CDECL napi_delete_reference(napi_env env, napi_ref ref) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, ref); delete reinterpret_cast(ref); @@ -2705,8 +2674,7 @@ napi_status NAPI_CDECL napi_reference_ref(napi_env env, uint32_t* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, ref); v8impl::Reference* reference = reinterpret_cast(ref); @@ -2728,8 +2696,7 @@ napi_status NAPI_CDECL napi_reference_unref(napi_env env, uint32_t* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, ref); v8impl::Reference* reference = reinterpret_cast(ref); @@ -2755,8 +2722,7 @@ napi_status NAPI_CDECL napi_get_reference_value(napi_env env, napi_value* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, ref); CHECK_ARG(env, result); @@ -2770,8 +2736,7 @@ napi_status NAPI_CDECL napi_open_handle_scope(napi_env env, napi_handle_scope* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = v8impl::JsHandleScopeFromV8HandleScope( @@ -2784,8 +2749,7 @@ napi_status NAPI_CDECL napi_close_handle_scope(napi_env env, napi_handle_scope scope) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, scope); if (env->open_handle_scopes == 0) { return napi_handle_scope_mismatch; @@ -2800,8 +2764,7 @@ napi_status NAPI_CDECL napi_open_escapable_handle_scope( napi_env env, napi_escapable_handle_scope* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = v8impl::JsEscapableHandleScopeFromV8EscapableHandleScope( @@ -2814,8 +2777,7 @@ napi_status NAPI_CDECL napi_close_escapable_handle_scope( napi_env env, napi_escapable_handle_scope scope) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, scope); if (env->open_handle_scopes == 0) { return napi_handle_scope_mismatch; @@ -2832,8 +2794,7 @@ napi_status NAPI_CDECL napi_escape_handle(napi_env env, napi_value* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, scope); CHECK_ARG(env, escapee); CHECK_ARG(env, result); @@ -2911,8 +2872,7 @@ napi_status NAPI_CDECL napi_instanceof(napi_env env, napi_status NAPI_CDECL napi_is_exception_pending(napi_env env, bool* result) { // NAPI_PREAMBLE is not used here: this function must execute when there is a // pending exception. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); *result = !env->last_exception.IsEmpty(); @@ -2923,8 +2883,7 @@ napi_status NAPI_CDECL napi_get_and_clear_last_exception(napi_env env, napi_value* result) { // NAPI_PREAMBLE is not used here: this function must execute when there is a // pending exception. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, result); if (env->last_exception.IsEmpty()) { @@ -2941,8 +2900,7 @@ napi_status NAPI_CDECL napi_get_and_clear_last_exception(napi_env env, napi_status NAPI_CDECL napi_is_arraybuffer(napi_env env, napi_value value, bool* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -2994,8 +2952,7 @@ napi_status NAPI_CDECL napi_get_arraybuffer_info(napi_env env, napi_value arraybuffer, void** data, size_t* byte_length) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, arraybuffer); v8::Local value = v8impl::V8LocalValueFromJsValue(arraybuffer); @@ -3017,8 +2974,7 @@ napi_status NAPI_CDECL napi_get_arraybuffer_info(napi_env env, napi_status NAPI_CDECL napi_is_typedarray(napi_env env, napi_value value, bool* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -3104,8 +3060,7 @@ napi_status NAPI_CDECL napi_get_typedarray_info(napi_env env, void** data, napi_value* arraybuffer, size_t* byte_offset) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, typedarray); v8::Local value = v8impl::V8LocalValueFromJsValue(typedarray); @@ -3195,8 +3150,7 @@ napi_status NAPI_CDECL napi_create_dataview(napi_env env, napi_status NAPI_CDECL napi_is_dataview(napi_env env, napi_value value, bool* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -3212,8 +3166,7 @@ napi_status NAPI_CDECL napi_get_dataview_info(napi_env env, void** data, napi_value* arraybuffer, size_t* byte_offset) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, dataview); v8::Local value = v8impl::V8LocalValueFromJsValue(dataview); @@ -3288,8 +3241,7 @@ napi_status NAPI_CDECL napi_reject_deferred(napi_env env, napi_status NAPI_CDECL napi_is_promise(napi_env env, napi_value value, bool* is_promise) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, is_promise); @@ -3315,8 +3267,7 @@ napi_status NAPI_CDECL napi_create_date(napi_env env, napi_status NAPI_CDECL napi_is_date(napi_env env, napi_value value, bool* is_date) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, is_date); @@ -3329,7 +3280,6 @@ napi_status NAPI_CDECL napi_get_date_value(napi_env env, napi_value value, double* result) { NAPI_PREAMBLE(env); - env->CheckGCAccess(); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -3375,8 +3325,7 @@ napi_status NAPI_CDECL napi_add_finalizer(napi_env env, napi_ref* result) { // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw // JS exceptions. - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, js_object); CHECK_ARG(env, finalize_cb); @@ -3455,8 +3404,7 @@ napi_status NAPI_CDECL napi_get_instance_data(napi_env env, void** data) { napi_status NAPI_CDECL napi_detach_arraybuffer(napi_env env, napi_value arraybuffer) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, arraybuffer); v8::Local value = v8impl::V8LocalValueFromJsValue(arraybuffer); @@ -3475,8 +3423,7 @@ napi_status NAPI_CDECL napi_detach_arraybuffer(napi_env env, napi_status NAPI_CDECL napi_is_detached_arraybuffer(napi_env env, napi_value arraybuffer, bool* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, arraybuffer); CHECK_ARG(env, result); diff --git a/src/js_native_api_v8.h b/src/js_native_api_v8.h index db39686c91127e..bdb29490dd9c4e 100644 --- a/src/js_native_api_v8.h +++ b/src/js_native_api_v8.h @@ -212,6 +212,12 @@ inline napi_status napi_set_last_error(napi_env env, } \ } while (0) +#define CHECK_ENV_NOT_IN_GC(env) \ + do { \ + CHECK_ENV((env)); \ + (env)->CheckGCAccess(); \ + } while (0) + #define CHECK_ARG(env, arg) \ RETURN_STATUS_IF_FALSE((env), ((arg) != nullptr), napi_invalid_arg) @@ -227,8 +233,7 @@ inline napi_status napi_set_last_error(napi_env env, // NAPI_PREAMBLE is not wrapped in do..while: try_catch must have function scope #define NAPI_PREAMBLE(env) \ - CHECK_ENV((env)); \ - (env)->CheckGCAccess(); \ + CHECK_ENV_NOT_IN_GC((env)); \ RETURN_STATUS_IF_FALSE( \ (env), (env)->last_exception.IsEmpty(), napi_pending_exception); \ RETURN_STATUS_IF_FALSE((env), \ diff --git a/src/node_api.cc b/src/node_api.cc index 23e9fedf8b146e..1a6cdcb8b1de9a 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -917,8 +917,7 @@ napi_status NAPI_CDECL napi_async_init(napi_env env, napi_value async_resource, napi_value async_resource_name, napi_async_context* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, async_resource_name); CHECK_ARG(env, result); @@ -951,8 +950,7 @@ napi_status NAPI_CDECL napi_async_init(napi_env env, napi_status NAPI_CDECL napi_async_destroy(napi_env env, napi_async_context async_context) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, async_context); v8impl::AsyncContext* node_async_context = @@ -1101,8 +1099,7 @@ napi_status NAPI_CDECL napi_create_buffer_copy(napi_env env, napi_status NAPI_CDECL napi_is_buffer(napi_env env, napi_value value, bool* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); CHECK_ARG(env, result); @@ -1114,8 +1111,7 @@ napi_status NAPI_CDECL napi_get_buffer_info(napi_env env, napi_value value, void** data, size_t* length) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, value); v8::Local buffer = v8impl::V8LocalValueFromJsValue(value); @@ -1236,8 +1232,7 @@ napi_create_async_work(napi_env env, napi_async_complete_callback complete, void* data, napi_async_work* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, execute); CHECK_ARG(env, result); @@ -1267,8 +1262,7 @@ napi_create_async_work(napi_env env, napi_status NAPI_CDECL napi_delete_async_work(napi_env env, napi_async_work work) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, work); uvimpl::Work::Delete(reinterpret_cast(work)); @@ -1322,8 +1316,7 @@ napi_create_threadsafe_function(napi_env env, void* context, napi_threadsafe_function_call_js call_js_cb, napi_threadsafe_function* result) { - CHECK_ENV(env); - env->CheckGCAccess(); + CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, async_resource_name); RETURN_STATUS_IF_FALSE(env, initial_thread_count > 0, napi_invalid_arg); CHECK_ARG(env, result);