node-api: support SharedArrayBuffer in napi_create_dataview

PR-URL: https://github.com/nodejs/node/pull/60473
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Kevin Eady
2025-11-13 22:45:17 +01:00
committed by GitHub
parent 49d6026fd1
commit 9cbf4c9e43
5 changed files with 70 additions and 23 deletions

View File

@@ -4,7 +4,10 @@
"target_name": "test_dataview",
"sources": [
"test_dataview.c"
]
],
# For node_api_is_sharedarraybuffer
'defines': [ 'NAPI_EXPERIMENTAL', 'NODE_API_EXPERIMENTAL_NO_WARNING' ]
}
]
}

View File

@@ -5,7 +5,7 @@ const assert = require('assert');
// Testing api calls for arrays
const test_dataview = require(`./build/${common.buildType}/test_dataview`);
// Test for creating dataview
// Test for creating dataview with ArrayBuffer
{
const buffer = new ArrayBuffer(128);
const template = Reflect.construct(DataView, [buffer]);
@@ -15,10 +15,30 @@ const test_dataview = require(`./build/${common.buildType}/test_dataview`);
`Expect ${theDataview} to be a DataView`);
}
// Test for creating dataview with invalid range
// Test for creating dataview with SharedArrayBuffer
{
const buffer = new SharedArrayBuffer(128);
const template = new DataView(buffer);
const theDataview = test_dataview.CreateDataViewFromJSDataView(template);
assert.ok(theDataview instanceof DataView,
`Expect ${theDataview} to be a DataView`);
assert.strictEqual(template.buffer, theDataview.buffer);
}
// Test for creating dataview with ArrayBuffer and invalid range
{
const buffer = new ArrayBuffer(128);
assert.throws(() => {
test_dataview.CreateDataView(buffer, 10, 200);
}, RangeError);
}
// Test for creating dataview with SharedArrayBuffer and invalid range
{
const buffer = new SharedArrayBuffer(128);
assert.throws(() => {
test_dataview.CreateDataView(buffer, 10, 200);
}, RangeError);
}

View File

@@ -20,9 +20,18 @@ static napi_value CreateDataView(napi_env env, napi_callback_info info) {
bool is_arraybuffer;
NODE_API_CALL(env, napi_is_arraybuffer(env, arraybuffer, &is_arraybuffer));
NODE_API_ASSERT(env, is_arraybuffer,
"Wrong type of arguments. Expects a ArrayBuffer as the first "
"argument.");
if (!is_arraybuffer) {
bool is_sharedarraybuffer;
NODE_API_CALL(
env,
node_api_is_sharedarraybuffer(env, arraybuffer, &is_sharedarraybuffer));
NODE_API_ASSERT(env,
is_sharedarraybuffer,
"Wrong type of arguments. Expects a SharedArrayBuffer or "
"ArrayBuffer as the first "
"argument.");
}
napi_valuetype valuetype1;
NODE_API_CALL(env, napi_typeof(env, args[1], &valuetype1));