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

n-api: simplify bigint-from-word creation #34554

Closed
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
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);

Choose a reason for hiding this comment

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

No tests - no pull-request

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The new test included with this PR tests the correct behaviour of CHECK_MAYBE_EMPTY_WITH_PREAMBLE().


*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