Skip to content

Commit 2d48606

Browse files
authored
Merge branch 'main' into feat/port-test-exception
2 parents 8af7dee + 48f1a69 commit 2d48606

12 files changed

Lines changed: 755 additions & 2 deletions

File tree

PORTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ 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 |
57-
| `test_error` | Not ported | Medium |
57+
| `test_error` | Ported ✅ | Medium |
5858
| `test_exception` | Ported ✅ | Medium |
5959
| `test_finalizer` | Not ported | Medium |
6060
| `test_function` | Not ported | Medium |

tests/harness/assert.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ if (typeof assert.throws !== 'function') {
7878
}
7979
assert.throws(() => { throw new Error('oops'); }, /oops/);
8080
assert.throws(() => { throw new TypeError('bad'); }, TypeError);
81+
assert.throws(
82+
() => { const err = new Error('match me'); err.code = 'ERR_TEST'; throw err; },
83+
{ code: 'ERR_TEST', message: 'match me' },
84+
);
85+
assert.throws(
86+
() => { throw new RangeError('validate me'); },
87+
(err) => err instanceof RangeError && err.message === 'validate me',
88+
);
8189
threw = false;
8290
try { assert.throws(() => { /* does not throw */ }); } catch { threw = true; }
8391
if (!threw) throw new Error('assert.throws must throw when fn does not throw');
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

0 commit comments

Comments
 (0)