-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
node-api: support SharedArrayBuffer in napi_create_typedarray #62710
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
Merged
nodejs-github-bot
merged 4 commits into
nodejs:main
from
umuoy1:feat/napi-typedarray-sharedarraybuffer
May 13, 2026
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
983f644
node-api: support SharedArrayBuffer in napi_create_typedarray
umuoy1 265fa6f
test: format SharedArrayBuffer typedarray addon test
umuoy1 df6d255
doc,test: improve SharedArrayBuffer typedarray test and docs
umuoy1 2bbc868
test: move typedarray sharedarraybuffer test
umuoy1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
test/js-native-api/test_typedarray_sharedarraybuffer/binding.gyp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
110
test/js-native-api/test_typedarray_sharedarraybuffer/test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`); | ||
|
|
||
| const typedArrayCases = [ | ||
| { type: Int8Array, values: [-1, 0, 127] }, | ||
| { type: Uint8Array, values: [1, 2, 255] }, | ||
| { type: Uint8ClampedArray, values: [0, 128, 255] }, | ||
| { type: Int16Array, values: [-1, 0, 32767] }, | ||
| { type: Uint16Array, values: [1, 2, 65535] }, | ||
| { type: Int32Array, values: [-1, 0, 123456789] }, | ||
| { type: Uint32Array, values: [1, 2, 4294967295] }, | ||
| { type: Float16Array, values: [0.5, -1.5, 42.25] }, | ||
| { type: Float32Array, values: [0.5, -1.5, 42.25] }, | ||
| { type: Float64Array, values: [0.5, -1.5, 42.25] }, | ||
| { type: BigInt64Array, values: [1n, -2n, 123456789n] }, | ||
| { type: BigUint64Array, values: [1n, 2n, 123456789n] }, | ||
| ]; | ||
|
|
||
| function createBuffer(Type, BufferType, length) { | ||
| const byteOffset = Type.BYTES_PER_ELEMENT; | ||
| const byteLength = byteOffset + (length * Type.BYTES_PER_ELEMENT); | ||
| return { | ||
| buffer: new BufferType(byteLength), | ||
| byteOffset, | ||
| }; | ||
| } | ||
|
|
||
| function createTypedArray(Type, buffer, byteOffset, length) { | ||
| const template = new Type(buffer, byteOffset, length); | ||
| return test_typedarray_sharedarraybuffer.CreateTypedArray(template, buffer); | ||
| } | ||
|
|
||
| function verifyTypedArray(Type, buffer, byteOffset, values) { | ||
| const theArray = createTypedArray(Type, buffer, byteOffset, values.length); | ||
| const theArrayBuffer = | ||
| test_typedarray_sharedarraybuffer.GetArrayBuffer(theArray); | ||
|
|
||
| assert.ok(theArray instanceof Type); | ||
| assert.strictEqual(theArray.buffer, buffer); | ||
| assert.strictEqual(theArrayBuffer, buffer); | ||
| assert.strictEqual(theArray.byteOffset, byteOffset); | ||
| assert.strictEqual(theArray.length, values.length); | ||
|
|
||
| theArray.set(values); | ||
| assert.deepStrictEqual(Array.from(new Type(buffer, byteOffset, values.length)), | ||
| values); | ||
| } | ||
|
|
||
| // Keep the existing ArrayBuffer behavior covered while focusing this test | ||
| // on SharedArrayBuffer-backed TypedArray creation. | ||
| { | ||
| const { buffer, byteOffset } = createBuffer(Uint8Array, ArrayBuffer, 3); | ||
| verifyTypedArray(Uint8Array, buffer, byteOffset, [1, 2, 3]); | ||
| } | ||
|
|
||
| // Verify all TypedArray variants can be created from SharedArrayBuffer. | ||
| typedArrayCases.forEach(({ type, values }) => { | ||
| const { buffer, byteOffset } = createBuffer(type, SharedArrayBuffer, | ||
| values.length); | ||
| verifyTypedArray(type, buffer, byteOffset, values); | ||
| }); | ||
|
|
||
| // Test for creating TypedArrays with SharedArrayBuffer and invalid range. | ||
| for (const { type, values } of typedArrayCases) { | ||
| const { buffer, byteOffset } = createBuffer(type, SharedArrayBuffer, | ||
| values.length); | ||
| const template = new type(buffer, byteOffset, values.length); | ||
|
|
||
| assert.throws(() => { | ||
| test_typedarray_sharedarraybuffer.CreateTypedArray( | ||
| template, buffer, values.length + 1, byteOffset); | ||
| }, RangeError); | ||
| } | ||
|
|
||
| // Test for creating TypedArrays with SharedArrayBuffer and invalid alignment. | ||
| for (const { type, values } of typedArrayCases) { | ||
| if (type.BYTES_PER_ELEMENT <= 1) { | ||
| continue; | ||
| } | ||
|
|
||
| const { buffer, byteOffset } = createBuffer(type, SharedArrayBuffer, | ||
| values.length); | ||
| const template = new type(buffer, byteOffset, values.length); | ||
|
|
||
| assert.throws(() => { | ||
| test_typedarray_sharedarraybuffer.CreateTypedArray( | ||
| template, buffer, 1, byteOffset + 1); | ||
| }, RangeError); | ||
| } | ||
|
|
||
| // Test invalid arguments. | ||
| { | ||
| const template = new Uint8Array(1); | ||
|
|
||
| assert.throws(() => { | ||
| test_typedarray_sharedarraybuffer.CreateTypedArray(template, {}); | ||
| }, { name: 'Error', message: 'Invalid argument' }); | ||
|
|
||
| assert.throws(() => { | ||
| test_typedarray_sharedarraybuffer.CreateTypedArray(template, 1); | ||
| }, { name: 'Error', message: 'Invalid argument' }); | ||
| } |
79 changes: 79 additions & 0 deletions
79
test/js-native-api/test_typedarray_sharedarraybuffer/test_typedarray_sharedarraybuffer.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // 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) { | ||
| size_t argc = 4; | ||
| napi_value args[4]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| NODE_API_ASSERT(env, argc == 2 || argc == 4, "Wrong number of arguments"); | ||
|
|
||
| bool is_typedarray; | ||
| NODE_API_CALL(env, napi_is_typedarray(env, args[0], &is_typedarray)); | ||
| NODE_API_ASSERT(env, | ||
| is_typedarray, | ||
| "Wrong type of arguments. Expects a typed array as first " | ||
| "argument."); | ||
|
|
||
| napi_typedarray_type type; | ||
| size_t length; | ||
| size_t byte_offset; | ||
| NODE_API_CALL(env, | ||
| napi_get_typedarray_info( | ||
| env, args[0], &type, &length, NULL, NULL, &byte_offset)); | ||
|
|
||
| if (argc == 4) { | ||
| uint32_t uint32_length; | ||
| NODE_API_CALL(env, napi_get_value_uint32(env, args[2], &uint32_length)); | ||
| length = uint32_length; | ||
|
|
||
| uint32_t uint32_byte_offset; | ||
| NODE_API_CALL(env, | ||
| napi_get_value_uint32(env, args[3], &uint32_byte_offset)); | ||
| byte_offset = uint32_byte_offset; | ||
| } | ||
|
|
||
| napi_value typedarray; | ||
| NODE_API_CALL(env, | ||
| napi_create_typedarray( | ||
| env, type, length, args[1], byte_offset, &typedarray)); | ||
|
|
||
| return typedarray; | ||
| } | ||
|
|
||
| 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("CreateTypedArray", CreateTypedArray), | ||
| 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.