Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve n-api coverage for typed arrays #13244

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions test/addons-napi/test_typedarray/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,21 @@ assert.strictEqual(externalResult.length, 3);
assert.strictEqual(externalResult[0], 0);
assert.strictEqual(externalResult[1], 1);
assert.strictEqual(externalResult[2], 2);

// validate creation of all kinds of TypedArrays
const buffer = new ArrayBuffer(128);
const arrayTypes = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array,
Uint16Array, Int32Array, Uint32Array, Float32Array,
Float64Array ];

arrayTypes.forEach((currentType, key) => {
const template = Reflect.construct(currentType, buffer);
const theArray = test_typedarray.CreateTypedArray(template, buffer);

assert.ok(theArray instanceof currentType,
'Type of new array should match that of the template');
assert.ok(theArray !== template);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert.strictNotEqual()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, thanks.

assert.strictEqual(theArray.buffer,
buffer,
'Buffer for array should match the one passed in');
});
48 changes: 48 additions & 0 deletions test/addons-napi/test_typedarray/test_typedarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,58 @@ napi_value External(napi_env env, napi_callback_info info) {
return output_array;
}

napi_value CreateTypedArray(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc == 2, "Wrong number of arguments");

napi_valuetype valuetype0;
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));

NAPI_ASSERT(env, valuetype0 == napi_object,
"Wrong type of argments. Expects a typed array as first argument.");

napi_value input_array = args[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can you declare napi_value input_array = args[0]; and napi_value input_buffer = args[1]; around line 104 for better readability and reuse it on line 106 and 119 respectively?

bool is_typedarray;
NAPI_CALL(env, napi_is_typedarray(env, input_array, &is_typedarray));

NAPI_ASSERT(env, is_typedarray,
"Wrong type of argments. Expects a typed array as first argument.");

napi_valuetype valuetype1;
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));

NAPI_ASSERT(env, valuetype1 == napi_object,
"Wrong type of argments. Expects an array buffer as second argument.");

napi_value input_buffer = args[1];
bool is_arraybuffer;
NAPI_CALL(env, napi_is_arraybuffer(env, input_buffer, &is_arraybuffer));

NAPI_ASSERT(env, is_arraybuffer,
"Wrong type of argments. Expects an array buffer as second argument.");

napi_typedarray_type type;
napi_value in_array_buffer;
size_t byte_offset;
size_t length;
NAPI_CALL(env, napi_get_typedarray_info(
env, input_array, &type, &length, NULL, &in_array_buffer, &byte_offset));

napi_value output_array;
NAPI_CALL(env, napi_create_typedarray(
env, type, length, input_buffer, byte_offset, &output_array));

return output_array;
}

void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
napi_property_descriptor descriptors[] = {
DECLARE_NAPI_PROPERTY("Multiply", Multiply),
DECLARE_NAPI_PROPERTY("External", External),
DECLARE_NAPI_PROPERTY("CreateTypedArray", CreateTypedArray),
};

NAPI_CALL_RETURN_VOID(env, napi_define_properties(
Expand Down