Skip to content

Commit

Permalink
n-api: simplify bigint-from-word creation
Browse files Browse the repository at this point in the history
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 <gabriel.schulhof@intel.com>
PR-URL: #34554
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
Gabriel Schulhof authored and codebytere committed Aug 5, 2020
1 parent 9c442f9 commit 2703fe4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1602,13 +1602,10 @@ napi_status napi_create_bigint_words(napi_env env,
v8::MaybeLocal<v8::BigInt> 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) {
Expand Down
7 changes: 7 additions & 0 deletions test/js-native-api/test_bigint/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
TestUint64,
TestWords,
CreateTooBigBigInt,
MakeBigIntWordsThrow,
} = require(`./build/${common.buildType}/test_bigint`);

[
Expand Down Expand Up @@ -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'
});
18 changes: 18 additions & 0 deletions test/js-native-api/test_bigint/test_bigint.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <limits.h>
#include <inttypes.h>
#include <stdio.h>
#include <js_native_api.h>
Expand Down Expand Up @@ -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[] = {
Expand All @@ -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(
Expand Down

0 comments on commit 2703fe4

Please sign in to comment.