Skip to content

Commit 86d68d3

Browse files
kraenhansenclaudelegendecas
authored
feat: port test_constructor to CTS (#29)
Exercises napi_define_class with method, data value, accessor, and static property descriptors. Includes null-argument tests for napi_define_class via test_null.c/test_null.js. Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: Chengzhong Wu <legendecas@gmail.com>
1 parent 57dba59 commit 86d68d3

File tree

8 files changed

+396
-1
lines changed

8 files changed

+396
-1
lines changed

PORTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Tests covering the engine-specific part of Node-API, defined in `js_native_api.h
5050
| `test_array` | Ported ✅ | Easy |
5151
| `test_bigint` | Ported ✅ | Easy |
5252
| `test_cannot_run_js` | Not ported | Medium |
53-
| `test_constructor` | Not ported | Medium |
53+
| `test_constructor` | Ported ✅ | Medium |
5454
| `test_conversions` | Not ported | Medium |
5555
| `test_dataview` | Not ported | Medium |
5656
| `test_date` | Ported ✅ | Easy |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_node_api_cts_addon(test_constructor test_constructor.c test_null.c)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const getterOnlyErrorRE =
2+
/^TypeError: Cannot set property .* of #<.*> which has only a getter$/;
3+
4+
// Testing api calls for a constructor that defines properties
5+
const TestConstructor = loadAddon('test_constructor');
6+
const test_object = new TestConstructor();
7+
8+
assert.strictEqual(test_object.echo('hello'), 'hello');
9+
10+
test_object.readwriteValue = 1;
11+
assert.strictEqual(test_object.readwriteValue, 1);
12+
test_object.readwriteValue = 2;
13+
assert.strictEqual(test_object.readwriteValue, 2);
14+
15+
assert.throws(() => { test_object.readonlyValue = 3; },
16+
/^TypeError: Cannot assign to read only property 'readonlyValue' of object '#<MyObject>'$/);
17+
18+
assert.ok(test_object.hiddenValue);
19+
20+
// Properties with napi_enumerable attribute should be enumerable.
21+
const propertyNames = [];
22+
for (const name in test_object) {
23+
propertyNames.push(name);
24+
}
25+
assert.ok(propertyNames.includes('echo'));
26+
assert.ok(propertyNames.includes('readwriteValue'));
27+
assert.ok(propertyNames.includes('readonlyValue'));
28+
assert.ok(!propertyNames.includes('hiddenValue'));
29+
assert.ok(!propertyNames.includes('readwriteAccessor1'));
30+
assert.ok(!propertyNames.includes('readwriteAccessor2'));
31+
assert.ok(!propertyNames.includes('readonlyAccessor1'));
32+
assert.ok(!propertyNames.includes('readonlyAccessor2'));
33+
34+
// The napi_writable attribute should be ignored for accessors.
35+
test_object.readwriteAccessor1 = 1;
36+
assert.strictEqual(test_object.readwriteAccessor1, 1);
37+
assert.strictEqual(test_object.readonlyAccessor1, 1);
38+
assert.throws(() => { test_object.readonlyAccessor1 = 3; }, getterOnlyErrorRE);
39+
test_object.readwriteAccessor2 = 2;
40+
assert.strictEqual(test_object.readwriteAccessor2, 2);
41+
assert.strictEqual(test_object.readonlyAccessor2, 2);
42+
assert.throws(() => { test_object.readonlyAccessor2 = 3; }, getterOnlyErrorRE);
43+
44+
// Validate that static properties are on the class as opposed
45+
// to the instance
46+
assert.strictEqual(TestConstructor.staticReadonlyAccessor1, 10);
47+
assert.strictEqual(test_object.staticReadonlyAccessor1, undefined);
48+
49+
// Verify that passing NULL to napi_define_class() results in the correct
50+
// error.
51+
assert.deepStrictEqual(TestConstructor.TestDefineClass(), {
52+
envIsNull: 'Invalid argument',
53+
nameIsNull: 'Invalid argument',
54+
cbIsNull: 'Invalid argument',
55+
cbDataIsNull: 'napi_ok',
56+
propertiesIsNull: 'Invalid argument',
57+
resultIsNull: 'Invalid argument',
58+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Testing api calls for a constructor that defines properties
2+
const TestConstructor = loadAddon('test_constructor').constructorName;
3+
assert.strictEqual(TestConstructor.name, 'MyObject');
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
#include <js_native_api.h>
2+
#include "../common.h"
3+
#include "../entry_point.h"
4+
#include "test_null.h"
5+
6+
static double value_ = 1;
7+
static double static_value_ = 10;
8+
9+
static napi_value TestDefineClass(napi_env env,
10+
napi_callback_info info) {
11+
napi_status status;
12+
napi_value result, return_value;
13+
14+
napi_property_descriptor property_descriptor = {
15+
"TestDefineClass",
16+
NULL,
17+
TestDefineClass,
18+
NULL,
19+
NULL,
20+
NULL,
21+
napi_enumerable | napi_static,
22+
NULL};
23+
24+
NODE_API_CALL(env, napi_create_object(env, &return_value));
25+
26+
status = napi_define_class(NULL,
27+
"TrackedFunction",
28+
NAPI_AUTO_LENGTH,
29+
TestDefineClass,
30+
NULL,
31+
1,
32+
&property_descriptor,
33+
&result);
34+
35+
add_returned_status(env,
36+
"envIsNull",
37+
return_value,
38+
"Invalid argument",
39+
napi_invalid_arg,
40+
status);
41+
42+
napi_define_class(env,
43+
NULL,
44+
NAPI_AUTO_LENGTH,
45+
TestDefineClass,
46+
NULL,
47+
1,
48+
&property_descriptor,
49+
&result);
50+
51+
add_last_status(env, "nameIsNull", return_value);
52+
53+
napi_define_class(env,
54+
"TrackedFunction",
55+
NAPI_AUTO_LENGTH,
56+
NULL,
57+
NULL,
58+
1,
59+
&property_descriptor,
60+
&result);
61+
62+
add_last_status(env, "cbIsNull", return_value);
63+
64+
napi_define_class(env,
65+
"TrackedFunction",
66+
NAPI_AUTO_LENGTH,
67+
TestDefineClass,
68+
NULL,
69+
1,
70+
&property_descriptor,
71+
&result);
72+
73+
add_last_status(env, "cbDataIsNull", return_value);
74+
75+
napi_define_class(env,
76+
"TrackedFunction",
77+
NAPI_AUTO_LENGTH,
78+
TestDefineClass,
79+
NULL,
80+
1,
81+
NULL,
82+
&result);
83+
84+
add_last_status(env, "propertiesIsNull", return_value);
85+
86+
87+
napi_define_class(env,
88+
"TrackedFunction",
89+
NAPI_AUTO_LENGTH,
90+
TestDefineClass,
91+
NULL,
92+
1,
93+
&property_descriptor,
94+
NULL);
95+
96+
add_last_status(env, "resultIsNull", return_value);
97+
98+
return return_value;
99+
}
100+
101+
static napi_value GetValue(napi_env env, napi_callback_info info) {
102+
size_t argc = 0;
103+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, NULL, NULL, NULL));
104+
105+
NODE_API_ASSERT(env, argc == 0, "Wrong number of arguments");
106+
107+
napi_value number;
108+
NODE_API_CALL(env, napi_create_double(env, value_, &number));
109+
110+
return number;
111+
}
112+
113+
static napi_value SetValue(napi_env env, napi_callback_info info) {
114+
size_t argc = 1;
115+
napi_value args[1];
116+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
117+
118+
NODE_API_ASSERT(env, argc == 1, "Wrong number of arguments");
119+
120+
NODE_API_CALL(env, napi_get_value_double(env, args[0], &value_));
121+
122+
return NULL;
123+
}
124+
125+
static napi_value Echo(napi_env env, napi_callback_info info) {
126+
size_t argc = 1;
127+
napi_value args[1];
128+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
129+
130+
NODE_API_ASSERT(env, argc == 1, "Wrong number of arguments");
131+
132+
return args[0];
133+
}
134+
135+
static napi_value New(napi_env env, napi_callback_info info) {
136+
napi_value _this;
137+
NODE_API_CALL(env, napi_get_cb_info(env, info, NULL, NULL, &_this, NULL));
138+
139+
return _this;
140+
}
141+
142+
static napi_value GetStaticValue(napi_env env, napi_callback_info info) {
143+
size_t argc = 0;
144+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, NULL, NULL, NULL));
145+
146+
NODE_API_ASSERT(env, argc == 0, "Wrong number of arguments");
147+
148+
napi_value number;
149+
NODE_API_CALL(env, napi_create_double(env, static_value_, &number));
150+
151+
return number;
152+
}
153+
154+
155+
static napi_value NewExtra(napi_env env, napi_callback_info info) {
156+
napi_value _this;
157+
NODE_API_CALL(env, napi_get_cb_info(env, info, NULL, NULL, &_this, NULL));
158+
159+
return _this;
160+
}
161+
162+
EXTERN_C_START
163+
napi_value Init(napi_env env, napi_value exports) {
164+
napi_value number, cons;
165+
NODE_API_CALL(env, napi_create_double(env, value_, &number));
166+
167+
NODE_API_CALL(env, napi_define_class(
168+
env, "MyObject_Extra", 8, NewExtra, NULL, 0, NULL, &cons));
169+
170+
napi_property_descriptor properties[] = {
171+
{ "echo", NULL, Echo, NULL, NULL, NULL, napi_enumerable, NULL },
172+
{ "readwriteValue", NULL, NULL, NULL, NULL, number,
173+
napi_enumerable | napi_writable, NULL },
174+
{ "readonlyValue", NULL, NULL, NULL, NULL, number, napi_enumerable,
175+
NULL },
176+
{ "hiddenValue", NULL, NULL, NULL, NULL, number, napi_default, NULL },
177+
{ "readwriteAccessor1", NULL, NULL, GetValue, SetValue, NULL, napi_default,
178+
NULL },
179+
{ "readwriteAccessor2", NULL, NULL, GetValue, SetValue, NULL,
180+
napi_writable, NULL },
181+
{ "readonlyAccessor1", NULL, NULL, GetValue, NULL, NULL, napi_default,
182+
NULL },
183+
{ "readonlyAccessor2", NULL, NULL, GetValue, NULL, NULL, napi_writable,
184+
NULL },
185+
{ "staticReadonlyAccessor1", NULL, NULL, GetStaticValue, NULL, NULL,
186+
napi_default | napi_static, NULL},
187+
{ "constructorName", NULL, NULL, NULL, NULL, cons,
188+
napi_enumerable | napi_static, NULL },
189+
{ "TestDefineClass", NULL, TestDefineClass, NULL, NULL, NULL,
190+
napi_enumerable | napi_static, NULL },
191+
};
192+
193+
NODE_API_CALL(env, napi_define_class(env, "MyObject", NAPI_AUTO_LENGTH, New,
194+
NULL, sizeof(properties)/sizeof(*properties), properties, &cons));
195+
196+
init_test_null(env, cons);
197+
198+
return cons;
199+
}
200+
EXTERN_C_END
Lines changed: 111 additions & 0 deletions
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+
}
Lines changed: 8 additions & 0 deletions
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_

0 commit comments

Comments
 (0)