Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src,n-api: throwing error with napi_pending_exception status #29847

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe JavaScript error should not be thrown here and returning napi_function_expected instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a semver-major change, I think, so we mustn't.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we cannot change the return status from napi_function_expected to napi_pending_exception because that's semver-major as well.

}
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) \
Copy link
Member Author

@legendecas legendecas Oct 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only place that used this macro is napi_create_typedarray by CREATE_TYPED_ARRAY.

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