Skip to content

Commit

Permalink
test: propagate napi_status to JS
Browse files Browse the repository at this point in the history
Re: #27945 (comment)

This commit regards reporting to the JS level an actual event
that happens when using suspected improper null arguments. It is better
to report the exact reason from N-API to the JS level.

PR-URL: #28505
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
Octavian Soldea authored and targos committed Jul 20, 2019
1 parent 61db987 commit 88809a4
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 106 deletions.
12 changes: 6 additions & 6 deletions test/js-native-api/test_constructor/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ assert.strictEqual(test_object.staticReadonlyAccessor1, undefined);
// Verify that passing NULL to napi_define_class() results in the correct
// error.
assert.deepStrictEqual(TestConstructor.TestDefineClass(), {
envIsNull: 'pass',
nameIsNull: 'pass',
cbIsNull: 'pass',
cbDataIsNull: 'pass',
propertiesIsNull: 'pass',
resultIsNull: 'pass'
envIsNull: 'Invalid argument',
nameIsNull: 'Invalid argument',
cbIsNull: 'Invalid argument',
cbDataIsNull: 'napi_ok',
propertiesIsNull: 'Invalid argument',
resultIsNull: 'Invalid argument'
});
188 changes: 88 additions & 100 deletions test/js-native-api/test_constructor/test_constructor.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
#include <js_native_api.h>
#include "../common.h"

#include <stdio.h>

static double value_ = 1;
static double static_value_ = 10;

static void
add_named_status(napi_env env, const char* key, napi_value return_value) {
napi_value prop_value;
const napi_extended_error_info* p_last_error;
NAPI_CALL_RETURN_VOID(env, napi_get_last_error_info(env, &p_last_error));

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_status ret[7];
napi_status status;
napi_value result, return_value, prop_value;
char p_napi_message[100] = "";

napi_property_descriptor property_descriptor = {
"TestDefineClass",
Expand All @@ -19,34 +41,9 @@ static napi_value TestDefineClass(napi_env env,
napi_enumerable | napi_static,
NULL};

ret[0] = napi_define_class(NULL,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
&result);

ret[1] = napi_define_class(env,
NULL,
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
&result);

ret[2] = napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
NULL,
NULL,
1,
&property_descriptor,
&result);
NAPI_CALL(env, napi_create_object(env, &return_value));

ret[3] = napi_define_class(env,
status = napi_define_class(NULL,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
Expand All @@ -55,85 +52,76 @@ static napi_value TestDefineClass(napi_env env,
&property_descriptor,
&result);

ret[4] = napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
NULL,
&result);

ret[5] = napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
NULL);

NAPI_CALL(env, napi_create_object(env, &return_value));
if (status == napi_invalid_arg) {
snprintf(p_napi_message, 99, "Invalid argument");
} else {
snprintf(p_napi_message, 99, "Invalid status [%d]", status);
}

NAPI_CALL(env, napi_create_string_utf8(env,
(ret[0] == napi_invalid_arg ?
"pass" : "fail"),
p_napi_message,
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL(env, napi_set_named_property(env,
return_value,
"envIsNull",
prop_value));

NAPI_CALL(env, napi_create_string_utf8(env,
(ret[1] == napi_invalid_arg ?
"pass" : "fail"),
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL(env, napi_set_named_property(env,
return_value,
"nameIsNull",
prop_value));

NAPI_CALL(env, napi_create_string_utf8(env,
(ret[2] == napi_invalid_arg ?
"pass" : "fail"),
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL(env, napi_set_named_property(env,
return_value,
"cbIsNull",
prop_value));

NAPI_CALL(env, napi_create_string_utf8(env,
(ret[3] == napi_ok ?
"pass" : "fail"),
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL(env, napi_set_named_property(env,
return_value,
"cbDataIsNull",
prop_value));

NAPI_CALL(env, napi_create_string_utf8(env,
(ret[4] == napi_invalid_arg ?
"pass" : "fail"),
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL(env, napi_set_named_property(env,
return_value,
"propertiesIsNull",
prop_value));

NAPI_CALL(env, napi_create_string_utf8(env,
(ret[5] == napi_invalid_arg ?
"pass" : "fail"),
NAPI_AUTO_LENGTH,
&prop_value));
NAPI_CALL(env, napi_set_named_property(env,
return_value,
"resultIsNull",
prop_value));
prop_value));

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

add_named_status(env, "nameIsNull", return_value);

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

add_named_status(env, "cbIsNull", return_value);

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

add_named_status(env, "cbDataIsNull", return_value);

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

add_named_status(env, "propertiesIsNull", return_value);


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

add_named_status(env, "resultIsNull", return_value);

return return_value;
}
Expand Down

0 comments on commit 88809a4

Please sign in to comment.