Skip to content

Commit 4a5161c

Browse files
author
Octavian Soldea
committed
test: propagate napi_status to JS
Re: nodejs#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.
1 parent ce56853 commit 4a5161c

File tree

3 files changed

+88
-135
lines changed

3 files changed

+88
-135
lines changed

test/js-native-api/common.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,3 @@
5858

5959
#define DECLARE_NAPI_GETTER(name, func) \
6060
{ (name), NULL, NULL, (func), NULL, NULL, napi_default, NULL }
61-
62-
#define NAPI_STATUS_TO_STRING(status) \
63-
(status) == napi_ok ? "napi_ok" : \
64-
(status) == napi_invalid_arg ? "napi_invalid_arg" : \
65-
(status) == napi_object_expected ? "napi_object_expected" : \
66-
(status) == napi_string_expected ? "napi_string_expected" : \
67-
(status) == napi_name_expected ? "napi_name_expected" : \
68-
(status) == napi_function_expected ? "napi_function_expected" : \
69-
(status) == napi_number_expected ? "napi_number_expected" : \
70-
(status) == napi_boolean_expected ? "napi_boolean_expected" : \
71-
(status) == napi_array_expected ? "napi_array_expected" : \
72-
(status) == napi_generic_failure ? "napi_generic_failure" : \
73-
(status) == napi_pending_exception ? "napi_pending_exception" : \
74-
(status) == napi_cancelled ? "napi_cancelled" : \
75-
(status) == napi_escape_called_twice ? "napi_escape_called_twice" : \
76-
(status) == napi_handle_scope_mismatch ? "napi_handle_scope_mismatch" : \
77-
(status) == napi_callback_scope_mismatch ? "napi_callback_scope_mismatch" : \
78-
(status) == napi_queue_full ? "napi_queue_full" : \
79-
(status) == napi_closing ? "napi_closing" : \
80-
(status) == napi_bigint_expected ? "napi_bigint_expected" : \
81-
(status) == napi_date_expected ? "napi_date_expected" : "UNKNOWN_ERROR"

test/js-native-api/test_constructor/test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ assert.strictEqual(test_object.staticReadonlyAccessor1, undefined);
5353
// Verify that passing NULL to napi_define_class() results in the correct
5454
// error.
5555
assert.deepStrictEqual(TestConstructor.TestDefineClass(), {
56-
envIsNull: 'napi_invalid_arg',
57-
nameIsNull: 'napi_invalid_arg',
58-
cbIsNull: 'napi_invalid_arg',
56+
envIsNull: 'napi_ok',
57+
nameIsNull: 'Invalid argument',
58+
cbIsNull: 'Invalid argument',
5959
cbDataIsNull: 'napi_ok',
60-
propertiesIsNull: 'napi_invalid_arg',
61-
resultIsNull: 'napi_invalid_arg'
60+
propertiesIsNull: 'Invalid argument',
61+
resultIsNull: 'Invalid argument'
6262
});

test/js-native-api/test_constructor/test_constructor.c

Lines changed: 83 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,25 @@
44
static double value_ = 1;
55
static double static_value_ = 10;
66

7+
static void add_named_property(napi_env env, const char * key, napi_value return_value) {
8+
napi_value prop_value;
9+
const napi_extended_error_info * p_last_error;
10+
napi_get_last_error_info(env, &p_last_error);
11+
12+
NAPI_CALL(env, napi_create_string_utf8(env,
13+
(p_last_error->error_message == NULL ? "napi_ok" : p_last_error->error_message),
14+
NAPI_AUTO_LENGTH,
15+
&prop_value));
16+
NAPI_CALL(env, napi_set_named_property(env,
17+
return_value,
18+
key,
19+
prop_value));
20+
}
21+
722
static napi_value TestDefineClass(napi_env env,
823
napi_callback_info info) {
9-
napi_status ret[7];
10-
napi_value result, return_value, prop_value;
24+
25+
napi_value result, return_value;
1126

1227
napi_property_descriptor property_descriptor = {
1328
"TestDefineClass",
@@ -19,115 +34,74 @@ static napi_value TestDefineClass(napi_env env,
1934
napi_enumerable | napi_static,
2035
NULL};
2136

22-
ret[0] = napi_define_class(NULL,
23-
"TrackedFunction",
24-
NAPI_AUTO_LENGTH,
25-
TestDefineClass,
26-
NULL,
27-
1,
28-
&property_descriptor,
29-
&result);
30-
31-
ret[1] = napi_define_class(env,
32-
NULL,
33-
NAPI_AUTO_LENGTH,
34-
TestDefineClass,
35-
NULL,
36-
1,
37-
&property_descriptor,
38-
&result);
39-
40-
ret[2] = napi_define_class(env,
41-
"TrackedFunction",
42-
NAPI_AUTO_LENGTH,
43-
NULL,
44-
NULL,
45-
1,
46-
&property_descriptor,
47-
&result);
48-
49-
ret[3] = napi_define_class(env,
50-
"TrackedFunction",
51-
NAPI_AUTO_LENGTH,
52-
TestDefineClass,
53-
NULL,
54-
1,
55-
&property_descriptor,
56-
&result);
57-
58-
ret[4] = napi_define_class(env,
59-
"TrackedFunction",
60-
NAPI_AUTO_LENGTH,
61-
TestDefineClass,
62-
NULL,
63-
1,
64-
NULL,
65-
&result);
66-
67-
ret[5] = napi_define_class(env,
68-
"TrackedFunction",
69-
NAPI_AUTO_LENGTH,
70-
TestDefineClass,
71-
NULL,
72-
1,
73-
&property_descriptor,
74-
NULL);
75-
7637
NAPI_CALL(env, napi_create_object(env, &return_value));
7738

78-
NAPI_CALL(env, napi_create_string_utf8(env,
79-
NAPI_STATUS_TO_STRING(ret[0]),
80-
NAPI_AUTO_LENGTH,
81-
&prop_value));
82-
NAPI_CALL(env, napi_set_named_property(env,
83-
return_value,
84-
"envIsNull",
85-
prop_value));
86-
87-
NAPI_CALL(env, napi_create_string_utf8(env,
88-
NAPI_STATUS_TO_STRING(ret[1]),
89-
NAPI_AUTO_LENGTH,
90-
&prop_value));
91-
NAPI_CALL(env, napi_set_named_property(env,
92-
return_value,
93-
"nameIsNull",
94-
prop_value));
95-
96-
NAPI_CALL(env, napi_create_string_utf8(env,
97-
NAPI_STATUS_TO_STRING(ret[2]),
98-
NAPI_AUTO_LENGTH,
99-
&prop_value));
100-
NAPI_CALL(env, napi_set_named_property(env,
101-
return_value,
102-
"cbIsNull",
103-
prop_value));
104-
105-
NAPI_CALL(env, napi_create_string_utf8(env,
106-
NAPI_STATUS_TO_STRING(ret[3]),
107-
NAPI_AUTO_LENGTH,
108-
&prop_value));
109-
NAPI_CALL(env, napi_set_named_property(env,
110-
return_value,
111-
"cbDataIsNull",
112-
prop_value));
113-
114-
NAPI_CALL(env, napi_create_string_utf8(env,
115-
NAPI_STATUS_TO_STRING(ret[4]),
116-
NAPI_AUTO_LENGTH,
117-
&prop_value));
118-
NAPI_CALL(env, napi_set_named_property(env,
119-
return_value,
120-
"propertiesIsNull",
121-
prop_value));
122-
123-
NAPI_CALL(env, napi_create_string_utf8(env,
124-
NAPI_STATUS_TO_STRING(ret[5]),
125-
NAPI_AUTO_LENGTH,
126-
&prop_value));
127-
NAPI_CALL(env, napi_set_named_property(env,
128-
return_value,
129-
"resultIsNull",
130-
prop_value));
39+
napi_define_class(NULL,
40+
"TrackedFunction",
41+
NAPI_AUTO_LENGTH,
42+
TestDefineClass,
43+
NULL,
44+
1,
45+
&property_descriptor,
46+
&result);
47+
48+
add_named_property(env, "envIsNull", return_value);
49+
50+
napi_define_class(env,
51+
NULL,
52+
NAPI_AUTO_LENGTH,
53+
TestDefineClass,
54+
NULL,
55+
1,
56+
&property_descriptor,
57+
&result);
58+
59+
add_named_property(env, "nameIsNull", return_value);
60+
61+
napi_define_class(env,
62+
"TrackedFunction",
63+
NAPI_AUTO_LENGTH,
64+
NULL,
65+
NULL,
66+
1,
67+
&property_descriptor,
68+
&result);
69+
70+
add_named_property(env, "cbIsNull", return_value);
71+
72+
napi_define_class(env,
73+
"TrackedFunction",
74+
NAPI_AUTO_LENGTH,
75+
TestDefineClass,
76+
NULL,
77+
1,
78+
&property_descriptor,
79+
&result);
80+
81+
add_named_property(env, "cbDataIsNull", return_value);
82+
83+
napi_define_class(env,
84+
"TrackedFunction",
85+
NAPI_AUTO_LENGTH,
86+
TestDefineClass,
87+
NULL,
88+
1,
89+
NULL,
90+
&result);
91+
92+
add_named_property(env, "propertiesIsNull", return_value);
93+
94+
95+
napi_define_class(env,
96+
"TrackedFunction",
97+
NAPI_AUTO_LENGTH,
98+
TestDefineClass,
99+
NULL,
100+
1,
101+
&property_descriptor,
102+
NULL);
103+
104+
add_named_property(env, "resultIsNull", return_value);
131105

132106
return return_value;
133107
}

0 commit comments

Comments
 (0)