Skip to content

Commit 2703fe4

Browse files
Gabriel Schulhofcodebytere
Gabriel Schulhof
authored andcommitted
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 <[email protected]> PR-URL: #34554 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 9c442f9 commit 2703fe4

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/js_native_api_v8.cc

+4-7
Original file line numberDiff line numberDiff line change
@@ -1602,13 +1602,10 @@ napi_status napi_create_bigint_words(napi_env env,
16021602
v8::MaybeLocal<v8::BigInt> b = v8::BigInt::NewFromWords(
16031603
context, sign_bit, word_count, words);
16041604

1605-
if (try_catch.HasCaught()) {
1606-
return napi_set_last_error(env, napi_pending_exception);
1607-
} else {
1608-
CHECK_MAYBE_EMPTY(env, b, napi_generic_failure);
1609-
*result = v8impl::JsValueFromV8LocalValue(b.ToLocalChecked());
1610-
return napi_clear_last_error(env);
1611-
}
1605+
CHECK_MAYBE_EMPTY_WITH_PREAMBLE(env, b, napi_generic_failure);
1606+
1607+
*result = v8impl::JsValueFromV8LocalValue(b.ToLocalChecked());
1608+
return GET_RETURN_STATUS(env);
16121609
}
16131610

16141611
napi_status napi_get_boolean(napi_env env, bool value, napi_value* result) {

test/js-native-api/test_bigint/test.js

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
TestUint64,
88
TestWords,
99
CreateTooBigBigInt,
10+
MakeBigIntWordsThrow,
1011
} = require(`./build/${common.buildType}/test_bigint`);
1112

1213
[
@@ -43,3 +44,9 @@ assert.throws(CreateTooBigBigInt, {
4344
name: 'Error',
4445
message: 'Invalid argument',
4546
});
47+
48+
// Test that we correctly forward exceptions from the engine.
49+
assert.throws(MakeBigIntWordsThrow, {
50+
name: 'RangeError',
51+
message: 'Maximum BigInt size exceeded'
52+
});

test/js-native-api/test_bigint/test_bigint.c

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <limits.h>
12
#include <inttypes.h>
23
#include <stdio.h>
34
#include <js_native_api.h>
@@ -122,6 +123,22 @@ static napi_value CreateTooBigBigInt(napi_env env, napi_callback_info info) {
122123
return output;
123124
}
124125

126+
// Test that we correctly forward exceptions from the engine.
127+
static napi_value MakeBigIntWordsThrow(napi_env env, napi_callback_info info) {
128+
uint64_t words[10];
129+
napi_value output;
130+
131+
napi_status status = napi_create_bigint_words(env,
132+
0,
133+
INT_MAX,
134+
words,
135+
&output);
136+
if (status != napi_pending_exception)
137+
napi_throw_error(env, NULL, "Expected status `napi_pending_exception`");
138+
139+
return NULL;
140+
}
141+
125142
EXTERN_C_START
126143
napi_value Init(napi_env env, napi_value exports) {
127144
napi_property_descriptor descriptors[] = {
@@ -130,6 +147,7 @@ napi_value Init(napi_env env, napi_value exports) {
130147
DECLARE_NAPI_PROPERTY("TestUint64", TestUint64),
131148
DECLARE_NAPI_PROPERTY("TestWords", TestWords),
132149
DECLARE_NAPI_PROPERTY("CreateTooBigBigInt", CreateTooBigBigInt),
150+
DECLARE_NAPI_PROPERTY("MakeBigIntWordsThrow", MakeBigIntWordsThrow),
133151
};
134152

135153
NAPI_CALL(env, napi_define_properties(

0 commit comments

Comments
 (0)