test: propagate napi_status to JS

Re: https://github.com/nodejs/node/pull/27945#discussion_r288833979

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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Octavian Soldea 2019-06-27 00:09:24 -07:00 committed by Rich Trott
parent 3b063dc21c
commit 3da44b0b52
2 changed files with 97 additions and 109 deletions

View File

@ -53,10 +53,10 @@ assert.strictEqual(test_object.staticReadonlyAccessor1, undefined);
// Verify that passing NULL to napi_define_class() results in the correct // Verify that passing NULL to napi_define_class() results in the correct
// error. // error.
assert.deepStrictEqual(TestConstructor.TestDefineClass(), { assert.deepStrictEqual(TestConstructor.TestDefineClass(), {
envIsNull: 'pass', envIsNull: 'Invalid argument',
nameIsNull: 'pass', nameIsNull: 'Invalid argument',
cbIsNull: 'pass', cbIsNull: 'Invalid argument',
cbDataIsNull: 'pass', cbDataIsNull: 'napi_ok',
propertiesIsNull: 'pass', propertiesIsNull: 'Invalid argument',
resultIsNull: 'pass' resultIsNull: 'Invalid argument'
}); });

View File

@ -1,13 +1,35 @@
#include <js_native_api.h> #include <js_native_api.h>
#include "../common.h" #include "../common.h"
#include <stdio.h>
static double value_ = 1; static double value_ = 1;
static double static_value_ = 10; 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, static napi_value TestDefineClass(napi_env env,
napi_callback_info info) { napi_callback_info info) {
napi_status ret[7]; napi_status status;
napi_value result, return_value, prop_value; napi_value result, return_value, prop_value;
char p_napi_message[100] = "";
napi_property_descriptor property_descriptor = { napi_property_descriptor property_descriptor = {
"TestDefineClass", "TestDefineClass",
@ -19,121 +41,87 @@ static napi_value TestDefineClass(napi_env env,
napi_enumerable | napi_static, napi_enumerable | napi_static,
NULL}; 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);
ret[3] = napi_define_class(env,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&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)); NAPI_CALL(env, napi_create_object(env, &return_value));
status = napi_define_class(NULL,
"TrackedFunction",
NAPI_AUTO_LENGTH,
TestDefineClass,
NULL,
1,
&property_descriptor,
&result);
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, NAPI_CALL(env, napi_create_string_utf8(env,
(ret[0] == napi_invalid_arg ? p_napi_message,
"pass" : "fail"),
NAPI_AUTO_LENGTH, NAPI_AUTO_LENGTH,
&prop_value)); &prop_value));
NAPI_CALL(env, napi_set_named_property(env, NAPI_CALL(env, napi_set_named_property(env,
return_value, return_value,
"envIsNull", "envIsNull",
prop_value)); prop_value));
NAPI_CALL(env, napi_create_string_utf8(env, napi_define_class(env,
(ret[1] == napi_invalid_arg ? NULL,
"pass" : "fail"), NAPI_AUTO_LENGTH,
NAPI_AUTO_LENGTH, TestDefineClass,
&prop_value)); NULL,
NAPI_CALL(env, napi_set_named_property(env, 1,
return_value, &property_descriptor,
"nameIsNull", &result);
prop_value));
NAPI_CALL(env, napi_create_string_utf8(env, add_named_status(env, "nameIsNull", return_value);
(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, napi_define_class(env,
(ret[3] == napi_ok ? "TrackedFunction",
"pass" : "fail"), NAPI_AUTO_LENGTH,
NAPI_AUTO_LENGTH, NULL,
&prop_value)); NULL,
NAPI_CALL(env, napi_set_named_property(env, 1,
return_value, &property_descriptor,
"cbDataIsNull", &result);
prop_value));
NAPI_CALL(env, napi_create_string_utf8(env, add_named_status(env, "cbIsNull", return_value);
(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, napi_define_class(env,
(ret[5] == napi_invalid_arg ? "TrackedFunction",
"pass" : "fail"), NAPI_AUTO_LENGTH,
NAPI_AUTO_LENGTH, TestDefineClass,
&prop_value)); NULL,
NAPI_CALL(env, napi_set_named_property(env, 1,
return_value, &property_descriptor,
"resultIsNull", &result);
prop_value));
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; return return_value;
} }