Skip to content

Commit

Permalink
src,n-api: throwing error with napi_pending_exception status
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
legendecas committed Oct 10, 2019
1 parent 8b9a578 commit 731ead9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
33 changes: 14 additions & 19 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1489,10 +1489,8 @@ napi_status napi_create_bigint_words(napi_env env,

v8::Local<v8::Context> 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<v8::BigInt> b = v8::BigInt::NewFromWords(
context, sign_bit, word_count, words);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -2769,14 +2764,14 @@ napi_status napi_create_dataview(napi_env env,
RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(), napi_invalid_arg);

v8::Local<v8::ArrayBuffer> buffer = value.As<v8::ArrayBuffer>();
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<v8::DataView> DataView = v8::DataView::New(buffer, byte_offset,
byte_length);

Expand Down
20 changes: 14 additions & 6 deletions src/js_native_api_v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ========================
Expand Down

0 comments on commit 731ead9

Please sign in to comment.