Skip to content

Commit

Permalink
test: propagate napi_status to JS - third answer
Browse files Browse the repository at this point in the history
  • Loading branch information
Octavian Soldea committed Jul 10, 2019
1 parent bc94d0b commit 73d6146
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
10 changes: 6 additions & 4 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -684,14 +684,16 @@ napi_status napi_get_last_error_info(napi_env env,
CHECK_ENV(env);
CHECK_ARG(env, result);

// you must update this assert to reference the last message
// in the napi_status enum each time a new error message is added.
// The value of the constant below must be updated to reference the last
// message in the napi_status enum each time a new error message is added.
// We don't have a napi_status_last as this would result in an ABI
// change each time a message was added.
const int last_status = napi_date_expected;

static_assert(
NAPI_ARRAYSIZE(error_messages) == napi_date_expected + 1,
NAPI_ARRAYSIZE(error_messages) == last_status + 1,
"Count of error messages must match count of error values");
CHECK_LE(env->last_error.error_code, napi_callback_scope_mismatch);
CHECK_LE(env->last_error.error_code, last_status);

// Wait until someone requests the last error information to fetch the error
// message string
Expand Down
2 changes: 1 addition & 1 deletion test/js-native-api/test_constructor/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ assert.strictEqual(test_object.staticReadonlyAccessor1, undefined);
// Verify that passing NULL to napi_define_class() results in the correct
// error.
assert.deepStrictEqual(TestConstructor.TestDefineClass(), {
envIsNull: 'napi_ok',
envIsNull: 'napi_env_null_is_ok',
nameIsNull: 'Invalid argument',
cbIsNull: 'Invalid argument',
cbDataIsNull: 'napi_ok',
Expand Down
55 changes: 33 additions & 22 deletions test/js-native-api/test_constructor/test_constructor.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ static void add_named_property(napi_env env, const char* key, napi_value return_
const napi_extended_error_info* p_last_error;
NAPI_CALL_RETURN_VOID(env, napi_get_last_error_info(env, &p_last_error));

NAPI_CALL(env, napi_create_string_utf8(env,
(p_last_error->error_message == NULL ?
"napi_ok" :
p_last_error->error_message),
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL(env, napi_set_named_property(env,
return_value,
key,
prop_value));
NAPI_CALL_RETURN_VOID(env,
napi_create_string_utf8(env,
(p_last_error->error_message == NULL ?
"napi_ok" :
p_last_error->error_message),
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env,
return_value,
key,
prop_value));
}

static napi_value TestDefineClass(napi_env env,
napi_callback_info info) {

napi_value prop_value;
napi_value result, return_value;
napi_status status;

napi_property_descriptor property_descriptor = {
"TestDefineClass",
Expand All @@ -37,18 +40,26 @@ static napi_value TestDefineClass(napi_env env,

NAPI_CALL(env, napi_create_object(env, &return_value));

napi_define_class(NULL,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
&result);

add_named_property(env, "envIsNull", return_value);

napi_define_class(env,
status = napi_define_class(NULL,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
&result);

NAPI_CALL_RETURN_VOID(env,
napi_create_string_utf8(env,
"napi_env_null_is_ok",
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env,
return_value,
"envIsNull",
prop_value));

napi_define_class(env,
NULL,
NAPI_AUTO_LENGTH,
TestDefineClass,
Expand Down

0 comments on commit 73d6146

Please sign in to comment.