From 540a08b54461b3007267013a5bd776bb6c4b2714 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Wed, 29 Jul 2020 10:48:00 -0700 Subject: [PATCH] n-api: simplify bigint-from-word creation Macro `CHECK_MAYBE_EMPTY_WITH_PREAMBLE()` does the work of checking the `TryCatch` and returning `napi_pending_exception` so this change reuses it for `napi_create_bigint_words()`. Signed-off-by: Gabriel Schulhof --- src/js_native_api_v8.cc | 11 ++++------- test/js-native-api/test_bigint/test.js | 7 +++++++ test/js-native-api/test_bigint/test_bigint.c | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index e99333a6a362d1..37cd1a1ab22109 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -1602,13 +1602,10 @@ napi_status napi_create_bigint_words(napi_env env, v8::MaybeLocal b = v8::BigInt::NewFromWords( context, sign_bit, word_count, words); - if (try_catch.HasCaught()) { - return napi_set_last_error(env, napi_pending_exception); - } else { - CHECK_MAYBE_EMPTY(env, b, napi_generic_failure); - *result = v8impl::JsValueFromV8LocalValue(b.ToLocalChecked()); - return napi_clear_last_error(env); - } + CHECK_MAYBE_EMPTY_WITH_PREAMBLE(env, b, napi_generic_failure); + + *result = v8impl::JsValueFromV8LocalValue(b.ToLocalChecked()); + return GET_RETURN_STATUS(env); } napi_status napi_get_boolean(napi_env env, bool value, napi_value* result) { diff --git a/test/js-native-api/test_bigint/test.js b/test/js-native-api/test_bigint/test.js index 85a183171743c7..bf9ce5066d6d2a 100644 --- a/test/js-native-api/test_bigint/test.js +++ b/test/js-native-api/test_bigint/test.js @@ -7,6 +7,7 @@ const { TestUint64, TestWords, CreateTooBigBigInt, + MakeBigIntWordsThrow, } = require(`./build/${common.buildType}/test_bigint`); [ @@ -43,3 +44,9 @@ assert.throws(CreateTooBigBigInt, { name: 'Error', message: 'Invalid argument', }); + +// Test that we correctly forward exceptions from the engine. +assert.throws(MakeBigIntWordsThrow, { + name: 'RangeError', + message: 'Maximum BigInt size exceeded' +}); diff --git a/test/js-native-api/test_bigint/test_bigint.c b/test/js-native-api/test_bigint/test_bigint.c index c62a0a6a6c2bbc..181f9103fa3399 100644 --- a/test/js-native-api/test_bigint/test_bigint.c +++ b/test/js-native-api/test_bigint/test_bigint.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -122,6 +123,22 @@ static napi_value CreateTooBigBigInt(napi_env env, napi_callback_info info) { return output; } +// Test that we correctly forward exceptions from the engine. +static napi_value MakeBigIntWordsThrow(napi_env env, napi_callback_info info) { + uint64_t words[10]; + napi_value output; + + napi_status status = napi_create_bigint_words(env, + 0, + INT_MAX, + words, + &output); + if (status != napi_pending_exception) + napi_throw_error(env, NULL, "Expected status `napi_pending_exception`"); + + return NULL; +} + EXTERN_C_START napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor descriptors[] = { @@ -130,6 +147,7 @@ napi_value Init(napi_env env, napi_value exports) { DECLARE_NAPI_PROPERTY("TestUint64", TestUint64), DECLARE_NAPI_PROPERTY("TestWords", TestWords), DECLARE_NAPI_PROPERTY("CreateTooBigBigInt", CreateTooBigBigInt), + DECLARE_NAPI_PROPERTY("MakeBigIntWordsThrow", MakeBigIntWordsThrow), }; NAPI_CALL(env, napi_define_properties(