From 731ead90bcd8dc4778b80544b911595aa0c05400 Mon Sep 17 00:00:00 2001 From: legendecas Date: Sat, 5 Oct 2019 01:48:40 +0800 Subject: [PATCH] src,n-api: throwing error with napi_pending_exception status It should be napi_pending_exception if an JavaScript error is pending to be handled. Or it might have a great chance to be ignored. --- src/js_native_api_v8.cc | 33 ++++++++++++++------------------- src/js_native_api_v8.h | 20 ++++++++++++++------ 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index a87af79a890832..1c154320b27e66 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1489,10 +1489,8 @@ napi_status napi_create_bigint_words(napi_env env, v8::Local context = env->context(); - if (word_count > INT_MAX) { - napi_throw_range_error(env, nullptr, "Maximum BigInt size exceeded"); - return napi_set_last_error(env, napi_pending_exception); - } + THROW_RANGE_ERROR_IF_TRUE( + env, word_count > INT_MAX, nullptr, "Maximum BigInt size exceeded"); v8::MaybeLocal b = v8::BigInt::NewFromWords( context, sign_bit, word_count, words); @@ -2483,13 +2481,10 @@ napi_status napi_instanceof(napi_env env, CHECK_TO_OBJECT(env, context, ctor, constructor); - if (!ctor->IsFunction()) { - napi_throw_type_error(env, - "ERR_NAPI_CONS_FUNCTION", - "Constructor must be a function"); - - return napi_set_last_error(env, napi_function_expected); - } + THROW_TYPE_ERROR_IF_FALSE(env, + ctor->IsFunction(), + "ERR_NAPI_CONS_FUNCTION", + "Constructor must be a function"); napi_status status = napi_generic_failure; @@ -2769,14 +2764,14 @@ napi_status napi_create_dataview(napi_env env, RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(), napi_invalid_arg); v8::Local buffer = value.As(); - if (byte_length + byte_offset > buffer->ByteLength()) { - napi_throw_range_error( - env, - "ERR_NAPI_INVALID_DATAVIEW_ARGS", - "byte_offset + byte_length should be less than or " - "equal to the size in bytes of the array passed in"); - return napi_set_last_error(env, napi_pending_exception); - } + + THROW_RANGE_ERROR_IF_TRUE( + env, + byte_length + byte_offset > buffer->ByteLength(), + "ERR_NAPI_INVALID_DATAVIEW_ARGS", + "byte_offset + byte_length should be less than or " + "equal to the size in bytes of the array passed in"); + v8::Local DataView = v8::DataView::New(buffer, byte_offset, byte_length); diff --git a/src/js_native_api_v8.h b/src/js_native_api_v8.h index 506e693f821227..31856ce655de5b 100644 --- a/src/js_native_api_v8.h +++ b/src/js_native_api_v8.h @@ -140,14 +140,22 @@ napi_status napi_set_last_error(napi_env env, napi_status error_code, (!try_catch.HasCaught() ? napi_ok \ : napi_set_last_error((env), napi_pending_exception)) -#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message) \ - do { \ - if (!(condition)) { \ - napi_throw_range_error((env), (error), (message)); \ - return napi_set_last_error((env), napi_generic_failure); \ - } \ +#define THROW_XERROR_IF_TRUE(errortype, env, condition, error, message) \ + do { \ + if (condition) { \ + napi_throw_##errortype##_error((env), (error), (message)); \ + return napi_set_last_error((env), napi_pending_exception); \ + } \ } while (0) +#define THROW_RANGE_ERROR_IF_TRUE(env, condition, error, message) \ + THROW_XERROR_IF_TRUE(range, env, condition, error, message) +#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message) \ + THROW_XERROR_IF_TRUE(range, env, !(condition), error, message) + +#define THROW_TYPE_ERROR_IF_FALSE(env, condition, error, message) \ + THROW_XERROR_IF_TRUE(type, env, !(condition), error, message) + namespace v8impl { //=== Conversion between V8 Handles and napi_value ========================