Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 02fe750

Browse filesBrowse files
Gabriel Schulhofaddaleax
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 <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>
1 parent 09a6cef commit 02fe750
Copy full SHA for 02fe750

File tree

Expand file treeCollapse file tree

3 files changed

+29
-7
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+29
-7
lines changed
Open diff view settings
Collapse file

‎src/js_native_api_v8.cc‎

Copy file name to clipboardExpand all lines: src/js_native_api_v8.cc
+4-7Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,13 +1634,10 @@ napi_status napi_create_bigint_words(napi_env env,
16341634
v8::MaybeLocal<v8::BigInt> b = v8::BigInt::NewFromWords(
16351635
context, sign_bit, word_count, words);
16361636

1637-
if (try_catch.HasCaught()) {
1638-
return napi_set_last_error(env, napi_pending_exception);
1639-
} else {
1640-
CHECK_MAYBE_EMPTY(env, b, napi_generic_failure);
1641-
*result = v8impl::JsValueFromV8LocalValue(b.ToLocalChecked());
1642-
return napi_clear_last_error(env);
1643-
}
1637+
CHECK_MAYBE_EMPTY_WITH_PREAMBLE(env, b, napi_generic_failure);
1638+
1639+
*result = v8impl::JsValueFromV8LocalValue(b.ToLocalChecked());
1640+
return GET_RETURN_STATUS(env);
16441641
}
16451642

16461643
napi_status napi_get_boolean(napi_env env, bool value, napi_value* result) {
Collapse file

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

Copy file name to clipboardExpand all lines: test/js-native-api/test_bigint/test.js
+7Lines changed: 7 additions & 0 deletions
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+
});
Collapse file

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

Copy file name to clipboardExpand all lines: test/js-native-api/test_bigint/test_bigint.c
+18Lines changed: 18 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.