Skip to content
Closed
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
5 changes: 3 additions & 2 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3726,8 +3726,9 @@ napi_status NAPI_CDECL napi_detach_arraybuffer(napi_env env,
env, value->IsArrayBuffer(), napi_arraybuffer_expected);

v8::Local<v8::ArrayBuffer> it = value.As<v8::ArrayBuffer>();
RETURN_STATUS_IF_FALSE(
env, it->IsDetachable(), napi_detachable_arraybuffer_expected);
RETURN_STATUS_IF_FALSE(env,
it->IsDetachable() && !it->IsImmutable(),
napi_detachable_arraybuffer_expected);

it->Detach(v8::Local<v8::Value>()).Check();

Expand Down
19 changes: 19 additions & 0 deletions test/js-native-api/test_typedarray/test-immutable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Flags: --js-immutable-arraybuffer
'use strict';
const common = require('../../common');
const assert = require('node:assert/strict');

if (typeof ArrayBuffer.prototype.transferToImmutable !== 'function')
common.skip('ArrayBuffer.prototype.transferToImmutable is not available');

const { Detach, IsDetached } = require(`./build/${common.buildType}/test_typedarray`);

const bytes = [1, 2, 3];
const buffer = Uint8Array.from(bytes).buffer.transferToImmutable();
const view = new Uint8Array(buffer);

assert.throws(() => Detach(view), {
message: 'A detachable arraybuffer was expected',
});
assert.equal(IsDetached(buffer), false);
assert.deepEqual([...view], bytes);
Loading