Skip to content

Commit 012af85

Browse files
authored
Merge branch 'main' into feat/spawn-test-global
2 parents 63a6db3 + 1fd96ff commit 012af85

8 files changed

Lines changed: 190 additions & 16 deletions

File tree

PORTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Tests covering the engine-specific part of Node-API, defined in `js_native_api.h
5252
| `test_cannot_run_js` | Not ported | Medium |
5353
| `test_constructor` | Ported ✅ | Medium |
5454
| `test_conversions` | Ported ✅ | Medium |
55-
| `test_dataview` | Not ported | Medium |
55+
| `test_dataview` | Ported ✅ | Medium |
5656
| `test_date` | Ported ✅ | Easy |
5757
| `test_error` | Ported ✅ | Medium |
5858
| `test_exception` | Not ported | Medium |

implementors/node/assert.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
21
import {
32
ok,
43
strictEqual,
54
notStrictEqual,
65
deepStrictEqual,
76
throws,
7+
match,
88
} from "node:assert/strict";
99

10-
const assert = Object.assign(
11-
(value, message) => ok(value, message),
12-
{
13-
ok: (value, message) => ok(value, message),
14-
strictEqual: (actual, expected, message) =>
15-
strictEqual(actual, expected, message),
16-
notStrictEqual: (actual, expected, message) =>
17-
notStrictEqual(actual, expected, message),
18-
deepStrictEqual: (actual, expected, message) =>
19-
deepStrictEqual(actual, expected, message),
20-
throws: (fn, error, message) => throws(fn, error, message),
21-
},
22-
);
10+
const assert = Object.assign((value, message) => ok(value, message), {
11+
ok: (value, message) => ok(value, message),
12+
strictEqual: (actual, expected, message) =>
13+
strictEqual(actual, expected, message),
14+
notStrictEqual: (actual, expected, message) =>
15+
notStrictEqual(actual, expected, message),
16+
deepStrictEqual: (actual, expected, message) =>
17+
deepStrictEqual(actual, expected, message),
18+
throws: (fn, error, message) => throws(fn, error, message),
19+
match: (string, regex, message) => match(string, regex, message),
20+
});
2321

2422
Object.assign(globalThis, { assert });

implementors/node/features.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Each key corresponds to a NODE_API_EXPERIMENTAL_HAS_* compile-time macro.
33
// Other implementors should set unsupported features to false or omit them.
44

5-
const [major, minor] = process.version.slice(1).split('.').map(Number);
5+
const [major, minor, patch] = process.version.slice(1).split(".").map(Number);
66

77
globalThis.experimentalFeatures = {
88
// node_api_is_sharedarraybuffer and node_api_create_sharedarraybuffer were
@@ -12,4 +12,11 @@ globalThis.experimentalFeatures = {
1212
createObjectWithProperties: true,
1313
setPrototype: true,
1414
postFinalizer: true,
15+
// napi_create_dataview accepts a SharedArrayBuffer-backed buffer only since
16+
// Node.js v24.13.1 and v25.4.0 (nodejs/node#60473). It was not backported to
17+
// v20.x or v22.x, where such calls fail with "invalid argument".
18+
dataviewSharedArrayBuffer:
19+
major > 25 ||
20+
(major === 25 && minor >= 4) ||
21+
(major === 24 && (minor > 13 || (minor === 13 && patch >= 1))),
1522
};

tests/harness/assert.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,19 @@ assert.throws(
8989
threw = false;
9090
try { assert.throws(() => { /* does not throw */ }); } catch { threw = true; }
9191
if (!threw) throw new Error('assert.throws must throw when fn does not throw');
92+
93+
// assert.match
94+
if (typeof assert.match !== 'function') {
95+
throw new Error('Expected assert.match to be a function');
96+
}
97+
assert.match('hello world', /hello/);
98+
assert.match('abc123', /^[a-z]+\d+$/);
99+
threw = false;
100+
try { assert.match('hello', /world/); } catch { threw = true; }
101+
if (!threw) throw new Error('assert.match("hello", /world/) must throw');
102+
threw = false;
103+
try { assert.match(123, /\d+/); } catch { threw = true; }
104+
if (!threw) throw new Error('assert.match must throw when input is not a string');
105+
threw = false;
106+
try { assert.match('hello', 'hello'); } catch { threw = true; }
107+
if (!threw) throw new Error('assert.match must throw when pattern is not a RegExp');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_node_api_cts_addon(test_dataview test_dataview.c)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use strict";
2+
3+
// Testing api calls for dataview
4+
const test_dataview = loadAddon("test_dataview");
5+
6+
// Test for creating dataview with ArrayBuffer
7+
{
8+
const buffer = new ArrayBuffer(128);
9+
const template = Reflect.construct(DataView, [buffer]);
10+
11+
const theDataview = test_dataview.CreateDataViewFromJSDataView(template);
12+
assert.ok(
13+
theDataview instanceof DataView,
14+
`Expect ${theDataview} to be a DataView`,
15+
);
16+
}
17+
18+
// Test for creating dataview with ArrayBuffer and invalid range
19+
{
20+
const buffer = new ArrayBuffer(128);
21+
assert.throws(() => {
22+
test_dataview.CreateDataView(buffer, 10, 200);
23+
}, RangeError);
24+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <js_native_api.h>
2+
#include <string.h>
3+
#include "../common.h"
4+
#include "../entry_point.h"
5+
6+
static napi_value CreateDataView(napi_env env, napi_callback_info info) {
7+
size_t argc = 3;
8+
napi_value args [3];
9+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
10+
11+
NODE_API_ASSERT(env, argc == 3, "Wrong number of arguments");
12+
13+
napi_valuetype valuetype0;
14+
napi_value arraybuffer = args[0];
15+
16+
NODE_API_CALL(env, napi_typeof(env, arraybuffer, &valuetype0));
17+
NODE_API_ASSERT(env, valuetype0 == napi_object,
18+
"Wrong type of arguments. Expects a ArrayBuffer as the first "
19+
"argument.");
20+
21+
napi_valuetype valuetype1;
22+
NODE_API_CALL(env, napi_typeof(env, args[1], &valuetype1));
23+
24+
NODE_API_ASSERT(env, valuetype1 == napi_number,
25+
"Wrong type of arguments. Expects a number as second argument.");
26+
27+
size_t byte_offset = 0;
28+
NODE_API_CALL(env, napi_get_value_uint32(env, args[1], (uint32_t*)(&byte_offset)));
29+
30+
napi_valuetype valuetype2;
31+
NODE_API_CALL(env, napi_typeof(env, args[2], &valuetype2));
32+
33+
NODE_API_ASSERT(env, valuetype2 == napi_number,
34+
"Wrong type of arguments. Expects a number as third argument.");
35+
36+
size_t length = 0;
37+
NODE_API_CALL(env, napi_get_value_uint32(env, args[2], (uint32_t*)(&length)));
38+
39+
napi_value output_dataview;
40+
NODE_API_CALL(env,
41+
napi_create_dataview(env, length, arraybuffer,
42+
byte_offset, &output_dataview));
43+
44+
return output_dataview;
45+
}
46+
47+
static napi_value CreateDataViewFromJSDataView(napi_env env, napi_callback_info info) {
48+
size_t argc = 1;
49+
napi_value args [1];
50+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
51+
52+
NODE_API_ASSERT(env, argc == 1, "Wrong number of arguments");
53+
54+
napi_valuetype valuetype;
55+
napi_value input_dataview = args[0];
56+
57+
NODE_API_CALL(env, napi_typeof(env, input_dataview, &valuetype));
58+
NODE_API_ASSERT(env, valuetype == napi_object,
59+
"Wrong type of arguments. Expects a DataView as the first "
60+
"argument.");
61+
62+
bool is_dataview;
63+
NODE_API_CALL(env, napi_is_dataview(env, input_dataview, &is_dataview));
64+
NODE_API_ASSERT(env, is_dataview,
65+
"Wrong type of arguments. Expects a DataView as the first "
66+
"argument.");
67+
size_t byte_offset = 0;
68+
size_t length = 0;
69+
napi_value buffer;
70+
NODE_API_CALL(env,
71+
napi_get_dataview_info(env, input_dataview, &length, NULL,
72+
&buffer, &byte_offset));
73+
74+
napi_value output_dataview;
75+
NODE_API_CALL(env,
76+
napi_create_dataview(env, length, buffer,
77+
byte_offset, &output_dataview));
78+
79+
80+
return output_dataview;
81+
}
82+
83+
EXTERN_C_START
84+
napi_value Init(napi_env env, napi_value exports) {
85+
napi_property_descriptor descriptors[] = {
86+
DECLARE_NODE_API_PROPERTY("CreateDataView", CreateDataView),
87+
DECLARE_NODE_API_PROPERTY("CreateDataViewFromJSDataView",
88+
CreateDataViewFromJSDataView)
89+
};
90+
91+
NODE_API_CALL(env, napi_define_properties(
92+
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
93+
94+
return exports;
95+
}
96+
EXTERN_C_END
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
3+
// napi_create_dataview accepts a SharedArrayBuffer-backed buffer only on newer
4+
// Node.js releases (see implementors/node/features.js).
5+
if (!experimentalFeatures.dataviewSharedArrayBuffer) {
6+
skipTest();
7+
}
8+
9+
// Testing api calls for dataview backed by a SharedArrayBuffer
10+
const test_dataview = loadAddon("test_dataview");
11+
12+
// Test for creating dataview with SharedArrayBuffer
13+
{
14+
const buffer = new SharedArrayBuffer(128);
15+
const template = new DataView(buffer);
16+
17+
const theDataview = test_dataview.CreateDataViewFromJSDataView(template);
18+
assert.ok(
19+
theDataview instanceof DataView,
20+
`Expect ${theDataview} to be a DataView`,
21+
);
22+
23+
assert.strictEqual(template.buffer, theDataview.buffer);
24+
}
25+
26+
// Test for creating dataview with SharedArrayBuffer and invalid range
27+
{
28+
const buffer = new SharedArrayBuffer(128);
29+
assert.throws(() => {
30+
test_dataview.CreateDataView(buffer, 10, 200);
31+
}, RangeError);
32+
}

0 commit comments

Comments
 (0)