Skip to content

Commit 4d02901

Browse files
gabrielschulhoftargos
authored andcommitted
node-api: test passing NULL to napi_define_class
PR-URL: #47567 Reviewed-By: Michael Dawson <midawson@redhat.com>
1 parent c5c40a8 commit 4d02901

File tree

5 files changed

+142
-1
lines changed

5 files changed

+142
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"sources": [
66
"../common.c",
77
"../entry_point.c",
8-
"test_constructor.c"
8+
"test_constructor.c",
9+
"test_null.c",
910
]
1011
}
1112
]

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

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <js_native_api.h>
22
#include "../common.h"
3+
#include "test_null.h"
34

45
static double value_ = 1;
56
static double static_value_ = 10;
@@ -191,6 +192,8 @@ napi_value Init(napi_env env, napi_value exports) {
191192
NODE_API_CALL(env, napi_define_class(env, "MyObject", NAPI_AUTO_LENGTH, New,
192193
NULL, sizeof(properties)/sizeof(*properties), properties, &cons));
193194

195+
init_test_null(env, cons);
196+
194197
return cons;
195198
}
196199
EXTERN_C_END
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include <js_native_api.h>
2+
3+
#include "../common.h"
4+
#include "test_null.h"
5+
6+
static int some_data = 0;
7+
8+
static napi_value TestConstructor(napi_env env, napi_callback_info info) {
9+
return NULL;
10+
}
11+
12+
static napi_value TestDefineClass(napi_env env, napi_callback_info info) {
13+
napi_value return_value, cons;
14+
15+
const napi_property_descriptor prop =
16+
DECLARE_NODE_API_PROPERTY("testConstructor", TestConstructor);
17+
18+
NODE_API_CALL(env, napi_create_object(env, &return_value));
19+
add_returned_status(env,
20+
"envIsNull",
21+
return_value,
22+
"Invalid argument",
23+
napi_invalid_arg,
24+
napi_define_class(NULL,
25+
"TestClass",
26+
NAPI_AUTO_LENGTH,
27+
TestConstructor,
28+
&some_data,
29+
1,
30+
&prop,
31+
&cons));
32+
33+
napi_define_class(env,
34+
NULL,
35+
NAPI_AUTO_LENGTH,
36+
TestConstructor,
37+
&some_data,
38+
1,
39+
&prop,
40+
&cons);
41+
add_last_status(env, "nameIsNull", return_value);
42+
43+
napi_define_class(
44+
env, "TestClass", 0, TestConstructor, &some_data, 1, &prop, &cons);
45+
add_last_status(env, "lengthIsZero", return_value);
46+
47+
napi_define_class(
48+
env, "TestClass", NAPI_AUTO_LENGTH, NULL, &some_data, 1, &prop, &cons);
49+
add_last_status(env, "nativeSideIsNull", return_value);
50+
51+
napi_define_class(env,
52+
"TestClass",
53+
NAPI_AUTO_LENGTH,
54+
TestConstructor,
55+
NULL,
56+
1,
57+
&prop,
58+
&cons);
59+
add_last_status(env, "dataIsNull", return_value);
60+
61+
napi_define_class(env,
62+
"TestClass",
63+
NAPI_AUTO_LENGTH,
64+
TestConstructor,
65+
&some_data,
66+
0,
67+
&prop,
68+
&cons);
69+
add_last_status(env, "propsLengthIsZero", return_value);
70+
71+
napi_define_class(env,
72+
"TestClass",
73+
NAPI_AUTO_LENGTH,
74+
TestConstructor,
75+
&some_data,
76+
1,
77+
NULL,
78+
&cons);
79+
add_last_status(env, "propsIsNull", return_value);
80+
81+
napi_define_class(env,
82+
"TestClass",
83+
NAPI_AUTO_LENGTH,
84+
TestConstructor,
85+
&some_data,
86+
1,
87+
&prop,
88+
NULL);
89+
add_last_status(env, "resultIsNull", return_value);
90+
91+
return return_value;
92+
}
93+
94+
void init_test_null(napi_env env, napi_value exports) {
95+
napi_value test_null;
96+
97+
const napi_property_descriptor test_null_props[] = {
98+
DECLARE_NODE_API_PROPERTY("testDefineClass", TestDefineClass),
99+
};
100+
101+
NODE_API_CALL_RETURN_VOID(env, napi_create_object(env, &test_null));
102+
NODE_API_CALL_RETURN_VOID(
103+
env,
104+
napi_define_properties(env,
105+
test_null,
106+
sizeof(test_null_props) / sizeof(*test_null_props),
107+
test_null_props));
108+
109+
NODE_API_CALL_RETURN_VOID(
110+
env, napi_set_named_property(env, exports, "testNull", test_null));
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef TEST_JS_NATIVE_API_TEST_OBJECT_TEST_NULL_H_
2+
#define TEST_JS_NATIVE_API_TEST_OBJECT_TEST_NULL_H_
3+
4+
#include <js_native_api.h>
5+
6+
void init_test_null(napi_env env, napi_value exports);
7+
8+
#endif // TEST_JS_NATIVE_API_TEST_OBJECT_TEST_NULL_H_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
const common = require('../../common');
3+
const assert = require('assert');
4+
5+
// Test passing NULL to object-related N-APIs.
6+
const { testNull } = require(`./build/${common.buildType}/test_constructor`);
7+
const expectedResult = {
8+
envIsNull: 'Invalid argument',
9+
nameIsNull: 'Invalid argument',
10+
lengthIsZero: 'napi_ok',
11+
nativeSideIsNull: 'Invalid argument',
12+
dataIsNull: 'napi_ok',
13+
propsLengthIsZero: 'napi_ok',
14+
propsIsNull: 'Invalid argument',
15+
resultIsNull: 'Invalid argument',
16+
};
17+
18+
assert.deepStrictEqual(testNull.testDefineClass(), expectedResult);

0 commit comments

Comments
 (0)