Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 12 additions & 10 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2762,21 +2762,22 @@ napi_status napi_create_typedarray(napi_env env,
* `[in] env`: The environment that the API is invoked under.
* `[in] type`: Scalar datatype of the elements within the `TypedArray`.
* `[in] length`: Number of elements in the `TypedArray`.
* `[in] arraybuffer`: `ArrayBuffer` underlying the typed array.
* `[in] byte_offset`: The byte offset within the `ArrayBuffer` from which to
start projecting the `TypedArray`.
* `[in] arraybuffer`: `ArrayBuffer` or `SharedArrayBuffer` underlying the
typed array.
* `[in] byte_offset`: The byte offset within the `ArrayBuffer` or
`SharedArrayBuffer` from which to start projecting the `TypedArray`.
* `[out] result`: A `napi_value` representing a JavaScript `TypedArray`.

Returns `napi_ok` if the API succeeded.

This API creates a JavaScript `TypedArray` object over an existing
`ArrayBuffer`. `TypedArray` objects provide an array-like view over an
underlying data buffer where each element has the same underlying binary scalar
datatype.
`ArrayBuffer` or `SharedArrayBuffer`. `TypedArray` objects provide an
array-like view over an underlying data buffer where each element has the same
underlying binary scalar datatype.

It's required that `(length * size_of_element) + byte_offset` should
be <= the size in bytes of the array passed in. If not, a `RangeError` exception
is raised.
It is required that `(length * size_of_element) + byte_offset` is less than or
equal to the size in bytes of the `ArrayBuffer` or `SharedArrayBuffer` passed
in. If not, a `RangeError` exception is raised.

JavaScript `TypedArray` objects are described in
[Section TypedArray objects][] of the ECMAScript Language Specification.
Expand Down Expand Up @@ -3469,7 +3470,8 @@ napi_status napi_get_typedarray_info(napi_env env,
the `byte_offset` value so that it points to the first element in the
`TypedArray`. If the length of the array is `0`, this may be `NULL` or
any other pointer value.
* `[out] arraybuffer`: The `ArrayBuffer` underlying the `TypedArray`.
* `[out] arraybuffer`: The `ArrayBuffer` or `SharedArrayBuffer` underlying the
`TypedArray`.
* `[out] byte_offset`: The byte offset within the underlying native array
at which the first element of the arrays is located. The value for the data
parameter has already been adjusted so that data points to the first element
Expand Down
121 changes: 64 additions & 57 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3228,66 +3228,73 @@ napi_status NAPI_CDECL napi_create_typedarray(napi_env env,
CHECK_ARG(env, result);

v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer);
RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(), napi_invalid_arg);
auto create_typedarray = [&](auto buffer) -> napi_status {
v8::Local<v8::TypedArray> typedArray;

switch (type) {
case napi_int8_array:
CREATE_TYPED_ARRAY(
env, Int8Array, 1, buffer, byte_offset, length, typedArray);
break;
case napi_uint8_array:
CREATE_TYPED_ARRAY(
env, Uint8Array, 1, buffer, byte_offset, length, typedArray);
break;
case napi_uint8_clamped_array:
CREATE_TYPED_ARRAY(
env, Uint8ClampedArray, 1, buffer, byte_offset, length, typedArray);
break;
case napi_int16_array:
CREATE_TYPED_ARRAY(
env, Int16Array, 2, buffer, byte_offset, length, typedArray);
break;
case napi_uint16_array:
CREATE_TYPED_ARRAY(
env, Uint16Array, 2, buffer, byte_offset, length, typedArray);
break;
case napi_int32_array:
CREATE_TYPED_ARRAY(
env, Int32Array, 4, buffer, byte_offset, length, typedArray);
break;
case napi_uint32_array:
CREATE_TYPED_ARRAY(
env, Uint32Array, 4, buffer, byte_offset, length, typedArray);
break;
case napi_float32_array:
CREATE_TYPED_ARRAY(
env, Float32Array, 4, buffer, byte_offset, length, typedArray);
break;
case napi_float64_array:
CREATE_TYPED_ARRAY(
env, Float64Array, 8, buffer, byte_offset, length, typedArray);
break;
case napi_bigint64_array:
CREATE_TYPED_ARRAY(
env, BigInt64Array, 8, buffer, byte_offset, length, typedArray);
break;
case napi_biguint64_array:
CREATE_TYPED_ARRAY(
env, BigUint64Array, 8, buffer, byte_offset, length, typedArray);
break;
case napi_float16_array:
CREATE_TYPED_ARRAY(
env, Float16Array, 2, buffer, byte_offset, length, typedArray);
break;
default:
return napi_set_last_error(env, napi_invalid_arg);
}

v8::Local<v8::ArrayBuffer> buffer = value.As<v8::ArrayBuffer>();
v8::Local<v8::TypedArray> typedArray;
*result = v8impl::JsValueFromV8LocalValue(typedArray);
return GET_RETURN_STATUS(env);
};

switch (type) {
case napi_int8_array:
CREATE_TYPED_ARRAY(
env, Int8Array, 1, buffer, byte_offset, length, typedArray);
break;
case napi_uint8_array:
CREATE_TYPED_ARRAY(
env, Uint8Array, 1, buffer, byte_offset, length, typedArray);
break;
case napi_uint8_clamped_array:
CREATE_TYPED_ARRAY(
env, Uint8ClampedArray, 1, buffer, byte_offset, length, typedArray);
break;
case napi_int16_array:
CREATE_TYPED_ARRAY(
env, Int16Array, 2, buffer, byte_offset, length, typedArray);
break;
case napi_uint16_array:
CREATE_TYPED_ARRAY(
env, Uint16Array, 2, buffer, byte_offset, length, typedArray);
break;
case napi_int32_array:
CREATE_TYPED_ARRAY(
env, Int32Array, 4, buffer, byte_offset, length, typedArray);
break;
case napi_uint32_array:
CREATE_TYPED_ARRAY(
env, Uint32Array, 4, buffer, byte_offset, length, typedArray);
break;
case napi_float32_array:
CREATE_TYPED_ARRAY(
env, Float32Array, 4, buffer, byte_offset, length, typedArray);
break;
case napi_float64_array:
CREATE_TYPED_ARRAY(
env, Float64Array, 8, buffer, byte_offset, length, typedArray);
break;
case napi_bigint64_array:
CREATE_TYPED_ARRAY(
env, BigInt64Array, 8, buffer, byte_offset, length, typedArray);
break;
case napi_biguint64_array:
CREATE_TYPED_ARRAY(
env, BigUint64Array, 8, buffer, byte_offset, length, typedArray);
break;
case napi_float16_array:
CREATE_TYPED_ARRAY(
env, Float16Array, 2, buffer, byte_offset, length, typedArray);
break;
default:
return napi_set_last_error(env, napi_invalid_arg);
if (value->IsArrayBuffer()) {
return create_typedarray(value.As<v8::ArrayBuffer>());
} else if (value->IsSharedArrayBuffer()) {
return create_typedarray(value.As<v8::SharedArrayBuffer>());
} else {
return napi_set_last_error(env, napi_invalid_arg);
}

*result = v8impl::JsValueFromV8LocalValue(typedArray);
return GET_RETURN_STATUS(env);
}

napi_status NAPI_CDECL napi_get_typedarray_info(napi_env env,
Expand Down
10 changes: 10 additions & 0 deletions test/js-native-api/test_typedarray_sharedarraybuffer/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"targets": [
{
"target_name": "test_typedarray_sharedarraybuffer",
"sources": [
"test_typedarray_sharedarraybuffer.c"
]
}
]
}
110 changes: 110 additions & 0 deletions test/js-native-api/test_typedarray_sharedarraybuffer/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
'use strict';

// Verify SharedArrayBuffer-backed typed arrays can be created through
// napi_create_typedarray() while preserving existing ArrayBuffer behavior.

const common = require('../../common');
const assert = require('assert');

const test_typedarray_sharedarraybuffer =
require(`./build/${common.buildType}/test_typedarray_sharedarraybuffer`);

// Test for creating Uint8Array with ArrayBuffer
{
const buffer = new ArrayBuffer(16);
const theArray =
test_typedarray_sharedarraybuffer.CreateUint8Array(buffer, 4, 6);
const theArrayBuffer =
test_typedarray_sharedarraybuffer.GetArrayBuffer(theArray);

assert.ok(theArray instanceof Uint8Array);
assert.strictEqual(theArray.buffer, buffer);
assert.ok(theArrayBuffer instanceof ArrayBuffer);
assert.strictEqual(theArrayBuffer, buffer);
assert.strictEqual(theArray.byteOffset, 4);
assert.strictEqual(theArray.length, 6);

theArray.set([1, 2, 3, 4, 5, 6]);
assert.deepStrictEqual(Array.from(new Uint8Array(buffer, 4, 6)),
[1, 2, 3, 4, 5, 6]);
}

// Test for creating Uint8Array with SharedArrayBuffer
{
const buffer = new SharedArrayBuffer(16);
const theArray =
test_typedarray_sharedarraybuffer.CreateUint8Array(buffer, 4, 6);
const theArrayBuffer =
test_typedarray_sharedarraybuffer.GetArrayBuffer(theArray);

assert.ok(theArray instanceof Uint8Array);
assert.strictEqual(theArray.buffer, buffer);
assert.ok(theArrayBuffer instanceof SharedArrayBuffer);
assert.strictEqual(theArrayBuffer, buffer);
assert.strictEqual(theArray.byteOffset, 4);
assert.strictEqual(theArray.length, 6);

theArray.set([6, 5, 4, 3, 2, 1]);
assert.deepStrictEqual(Array.from(new Uint8Array(buffer, 4, 6)),
[6, 5, 4, 3, 2, 1]);
}

// Test for creating Uint16Array with SharedArrayBuffer
{
const buffer = new SharedArrayBuffer(24);
const theArray =
test_typedarray_sharedarraybuffer.CreateUint16Array(buffer, 4, 4);

assert.ok(theArray instanceof Uint16Array);
assert.strictEqual(theArray.buffer, buffer);
assert.strictEqual(theArray.byteOffset, 4);
assert.strictEqual(theArray.length, 4);

theArray.set([1, 2, 3, 65535]);
assert.deepStrictEqual(Array.from(new Uint16Array(buffer, 4, 4)),
[1, 2, 3, 65535]);
}

// Test for creating Int32Array with SharedArrayBuffer
{
const buffer = new SharedArrayBuffer(32);
const theArray =
test_typedarray_sharedarraybuffer.CreateInt32Array(buffer, 8, 3);

assert.ok(theArray instanceof Int32Array);
assert.strictEqual(theArray.buffer, buffer);
assert.strictEqual(theArray.byteOffset, 8);
assert.strictEqual(theArray.length, 3);

theArray.set([-1, 0, 123456789]);
assert.deepStrictEqual(Array.from(new Int32Array(buffer, 8, 3)),
[-1, 0, 123456789]);
}

// Test for creating TypedArrays with SharedArrayBuffer and invalid range
{
const buffer = new SharedArrayBuffer(8);

assert.throws(() => {
test_typedarray_sharedarraybuffer.CreateUint8Array(buffer, 9, 0);
}, RangeError);

assert.throws(() => {
test_typedarray_sharedarraybuffer.CreateUint16Array(buffer, 0, 5);
}, RangeError);

assert.throws(() => {
test_typedarray_sharedarraybuffer.CreateUint16Array(buffer, 1, 1);
}, RangeError);
}

// Test invalid arguments
{
assert.throws(() => {
test_typedarray_sharedarraybuffer.CreateUint8Array({}, 0, 1);
}, { name: 'Error', message: 'Invalid argument' });

assert.throws(() => {
test_typedarray_sharedarraybuffer.CreateUint8Array(1, 0, 1);
}, { name: 'Error', message: 'Invalid argument' });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Verify napi_create_typedarray() accepts SharedArrayBuffer-backed views
// without changing its existing error handling.

#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"

static napi_value CreateTypedArray(napi_env env,
napi_callback_info info,
napi_typedarray_type type) {
size_t argc = 3;
napi_value args[3];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NODE_API_ASSERT(env, argc == 3, "Wrong number of arguments");

uint32_t byte_offset;
NODE_API_CALL(env, napi_get_value_uint32(env, args[1], &byte_offset));

uint32_t length;
NODE_API_CALL(env, napi_get_value_uint32(env, args[2], &length));

napi_value typedarray;
NODE_API_CALL(env,
napi_create_typedarray(
env, type, length, args[0], byte_offset, &typedarray));

return typedarray;
}

static napi_value CreateUint8Array(napi_env env, napi_callback_info info) {
return CreateTypedArray(env, info, napi_uint8_array);
}

static napi_value CreateUint16Array(napi_env env, napi_callback_info info) {
return CreateTypedArray(env, info, napi_uint16_array);
}

static napi_value CreateInt32Array(napi_env env, napi_callback_info info) {
return CreateTypedArray(env, info, napi_int32_array);
}

static napi_value GetArrayBuffer(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NODE_API_ASSERT(env, argc == 1, "Wrong number of arguments");

napi_value arraybuffer;
NODE_API_CALL(env,
napi_get_typedarray_info(
env, args[0], NULL, NULL, NULL, &arraybuffer, NULL));

return arraybuffer;
}

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor descriptors[] = {
DECLARE_NODE_API_PROPERTY("CreateUint8Array", CreateUint8Array),
DECLARE_NODE_API_PROPERTY("CreateUint16Array", CreateUint16Array),
DECLARE_NODE_API_PROPERTY("CreateInt32Array", CreateInt32Array),
DECLARE_NODE_API_PROPERTY("GetArrayBuffer", GetArrayBuffer),
};

NODE_API_CALL(
env,
napi_define_properties(env,
exports,
sizeof(descriptors) / sizeof(*descriptors),
descriptors));

return exports;
}
EXTERN_C_END
Loading