Skip to content

Commit 7cc6fd8

Browse files
mhdawsonjasnell
authored andcommitted
test: improve n-api coverage for typed arrays
Add testing for all types of typed arrays. Add testing for napi_is_arraybuffer. PR-URL: #13244 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jason Ginchereau <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Kunal Pathak <[email protected]> Reviewed-By: Hitesh Kanwathirtha <[email protected]>
1 parent bee1421 commit 7cc6fd8

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

test/addons-napi/test_typedarray/test.js

+20
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,23 @@ assert.strictEqual(externalResult.length, 3);
3737
assert.strictEqual(externalResult[0], 0);
3838
assert.strictEqual(externalResult[1], 1);
3939
assert.strictEqual(externalResult[2], 2);
40+
41+
// validate creation of all kinds of TypedArrays
42+
const buffer = new ArrayBuffer(128);
43+
const arrayTypes = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array,
44+
Uint16Array, Int32Array, Uint32Array, Float32Array,
45+
Float64Array ];
46+
47+
arrayTypes.forEach((currentType, key) => {
48+
const template = Reflect.construct(currentType, buffer);
49+
const theArray = test_typedarray.CreateTypedArray(template, buffer);
50+
51+
assert.ok(theArray instanceof currentType,
52+
'Type of new array should match that of the template');
53+
assert.notStrictEqual(theArray,
54+
template,
55+
'the new array should not be a copy of the template');
56+
assert.strictEqual(theArray.buffer,
57+
buffer,
58+
'Buffer for array should match the one passed in');
59+
});

test/addons-napi/test_typedarray/test_typedarray.c

+48
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,58 @@ napi_value External(napi_env env, napi_callback_info info) {
9595
return output_array;
9696
}
9797

98+
napi_value CreateTypedArray(napi_env env, napi_callback_info info) {
99+
size_t argc = 2;
100+
napi_value args[2];
101+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
102+
103+
NAPI_ASSERT(env, argc == 2, "Wrong number of arguments");
104+
105+
napi_value input_array = args[0];
106+
napi_valuetype valuetype0;
107+
NAPI_CALL(env, napi_typeof(env, input_array, &valuetype0));
108+
109+
NAPI_ASSERT(env, valuetype0 == napi_object,
110+
"Wrong type of argments. Expects a typed array as first argument.");
111+
112+
bool is_typedarray;
113+
NAPI_CALL(env, napi_is_typedarray(env, input_array, &is_typedarray));
114+
115+
NAPI_ASSERT(env, is_typedarray,
116+
"Wrong type of argments. Expects a typed array as first argument.");
117+
118+
napi_valuetype valuetype1;
119+
napi_value input_buffer = args[1];
120+
NAPI_CALL(env, napi_typeof(env, input_buffer, &valuetype1));
121+
122+
NAPI_ASSERT(env, valuetype1 == napi_object,
123+
"Wrong type of argments. Expects an array buffer as second argument.");
124+
125+
bool is_arraybuffer;
126+
NAPI_CALL(env, napi_is_arraybuffer(env, input_buffer, &is_arraybuffer));
127+
128+
NAPI_ASSERT(env, is_arraybuffer,
129+
"Wrong type of argments. Expects an array buffer as second argument.");
130+
131+
napi_typedarray_type type;
132+
napi_value in_array_buffer;
133+
size_t byte_offset;
134+
size_t length;
135+
NAPI_CALL(env, napi_get_typedarray_info(
136+
env, input_array, &type, &length, NULL, &in_array_buffer, &byte_offset));
137+
138+
napi_value output_array;
139+
NAPI_CALL(env, napi_create_typedarray(
140+
env, type, length, input_buffer, byte_offset, &output_array));
141+
142+
return output_array;
143+
}
144+
98145
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
99146
napi_property_descriptor descriptors[] = {
100147
DECLARE_NAPI_PROPERTY("Multiply", Multiply),
101148
DECLARE_NAPI_PROPERTY("External", External),
149+
DECLARE_NAPI_PROPERTY("CreateTypedArray", CreateTypedArray),
102150
};
103151

104152
NAPI_CALL_RETURN_VOID(env, napi_define_properties(

0 commit comments

Comments
 (0)