Skip to content

Commit c75f94e

Browse files
committed
feat: port test_dataview to CTS
Ports [https://github.com/nodejs/node/tree/main/test/js-native-api/test_dataview](test_dataview) from Node.js test suite to the CTS. This test contains an experimental feature `SharedArrayBuffer`, resulting in usage of `add_node_api_cts_experimental_addon()`. Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
1 parent 48f1a69 commit c75f94e

5 files changed

Lines changed: 162 additions & 3 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ function(add_node_api_cts_addon ADDON_NAME)
4545
endfunction()
4646

4747
function(add_node_api_cts_experimental_addon ADDON_NAME)
48-
cmake_parse_arguments(PARSE_ARGV 1 ARG "" "" "SOURCES")
49-
add_node_api_cts_addon(${ADDON_NAME} ${ARG_SOURCES})
48+
add_node_api_cts_addon(${ADDON_NAME} ${ARGN})
5049
target_compile_definitions(${ADDON_NAME} PRIVATE NAPI_EXPERIMENTAL)
5150
if(MSVC)
5251
target_link_libraries(${ADDON_NAME} PRIVATE ${NODE_API_EXPERIMENTAL_LIB})

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` | Not 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 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_node_api_cts_experimental_addon(test_dataview test_dataview.c)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"use strict";
2+
3+
if (experimentalFeatures.sharedArrayBuffer) {
4+
// Testing api calls for arrays
5+
const test_dataview = loadAddon("test_dataview");
6+
7+
// Test for creating dataview with ArrayBuffer
8+
{
9+
const buffer = new ArrayBuffer(128);
10+
const template = Reflect.construct(DataView, [buffer]);
11+
12+
const theDataview = test_dataview.CreateDataViewFromJSDataView(template);
13+
assert.ok(
14+
theDataview instanceof DataView,
15+
`Expect ${theDataview} to be a DataView`,
16+
);
17+
}
18+
19+
// Test for creating dataview with SharedArrayBuffer
20+
{
21+
const buffer = new SharedArrayBuffer(128);
22+
const template = new DataView(buffer);
23+
24+
const theDataview = test_dataview.CreateDataViewFromJSDataView(template);
25+
assert.ok(
26+
theDataview instanceof DataView,
27+
`Expect ${theDataview} to be a DataView`,
28+
);
29+
30+
assert.strictEqual(template.buffer, theDataview.buffer);
31+
}
32+
33+
// Test for creating dataview with ArrayBuffer and invalid range
34+
{
35+
const buffer = new ArrayBuffer(128);
36+
assert.throws(() => {
37+
test_dataview.CreateDataView(buffer, 10, 200);
38+
}, RangeError);
39+
}
40+
41+
// Test for creating dataview with SharedArrayBuffer and invalid range
42+
{
43+
const buffer = new SharedArrayBuffer(128);
44+
assert.throws(() => {
45+
test_dataview.CreateDataView(buffer, 10, 200);
46+
}, RangeError);
47+
}
48+
}
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+
#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+
bool is_arraybuffer;
22+
NODE_API_CALL(env, napi_is_arraybuffer(env, arraybuffer, &is_arraybuffer));
23+
24+
if (!is_arraybuffer) {
25+
bool is_sharedarraybuffer;
26+
NODE_API_CALL(
27+
env,
28+
node_api_is_sharedarraybuffer(env, arraybuffer, &is_sharedarraybuffer));
29+
NODE_API_ASSERT(env,
30+
is_sharedarraybuffer,
31+
"Wrong type of arguments. Expects a SharedArrayBuffer or "
32+
"ArrayBuffer as the first "
33+
"argument.");
34+
}
35+
36+
napi_valuetype valuetype1;
37+
NODE_API_CALL(env, napi_typeof(env, args[1], &valuetype1));
38+
39+
NODE_API_ASSERT(env, valuetype1 == napi_number,
40+
"Wrong type of arguments. Expects a number as second argument.");
41+
42+
size_t byte_offset = 0;
43+
NODE_API_CALL(env, napi_get_value_uint32(env, args[1], (uint32_t*)(&byte_offset)));
44+
45+
napi_valuetype valuetype2;
46+
NODE_API_CALL(env, napi_typeof(env, args[2], &valuetype2));
47+
48+
NODE_API_ASSERT(env, valuetype2 == napi_number,
49+
"Wrong type of arguments. Expects a number as third argument.");
50+
51+
size_t length = 0;
52+
NODE_API_CALL(env, napi_get_value_uint32(env, args[2], (uint32_t*)(&length)));
53+
54+
napi_value output_dataview;
55+
NODE_API_CALL(env,
56+
napi_create_dataview(env, length, arraybuffer,
57+
byte_offset, &output_dataview));
58+
59+
return output_dataview;
60+
}
61+
62+
static napi_value CreateDataViewFromJSDataView(napi_env env, napi_callback_info info) {
63+
size_t argc = 1;
64+
napi_value args [1];
65+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
66+
67+
NODE_API_ASSERT(env, argc == 1, "Wrong number of arguments");
68+
69+
napi_valuetype valuetype;
70+
napi_value input_dataview = args[0];
71+
72+
NODE_API_CALL(env, napi_typeof(env, input_dataview, &valuetype));
73+
NODE_API_ASSERT(env, valuetype == napi_object,
74+
"Wrong type of arguments. Expects a DataView as the first "
75+
"argument.");
76+
77+
bool is_dataview;
78+
NODE_API_CALL(env, napi_is_dataview(env, input_dataview, &is_dataview));
79+
NODE_API_ASSERT(env, is_dataview,
80+
"Wrong type of arguments. Expects a DataView as the first "
81+
"argument.");
82+
size_t byte_offset = 0;
83+
size_t length = 0;
84+
napi_value buffer;
85+
NODE_API_CALL(env,
86+
napi_get_dataview_info(env, input_dataview, &length, NULL,
87+
&buffer, &byte_offset));
88+
89+
napi_value output_dataview;
90+
NODE_API_CALL(env,
91+
napi_create_dataview(env, length, buffer,
92+
byte_offset, &output_dataview));
93+
94+
95+
return output_dataview;
96+
}
97+
98+
EXTERN_C_START
99+
napi_value Init(napi_env env, napi_value exports) {
100+
napi_property_descriptor descriptors[] = {
101+
DECLARE_NODE_API_PROPERTY("CreateDataView", CreateDataView),
102+
DECLARE_NODE_API_PROPERTY("CreateDataViewFromJSDataView",
103+
CreateDataViewFromJSDataView)
104+
};
105+
106+
NODE_API_CALL(env, napi_define_properties(
107+
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
108+
109+
return exports;
110+
}
111+
EXTERN_C_END

0 commit comments

Comments
 (0)