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
5 changes: 3 additions & 2 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3724,8 +3724,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
25 changes: 25 additions & 0 deletions test/js-native-api/test_typedarray/test-detach-immutable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Flags: --js-immutable-arraybuffer
'use strict';

const common = require('../../common');
const assert = require('assert');
const binding = require(`./build/${common.buildType}/test_typedarray`);

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

assert.throws(() => binding.Detach(view), {
name: 'Error',
message: 'A detachable arraybuffer was expected',
});
assert.strictEqual(binding.IsDetached(buffer), false);
assert.strictEqual(buffer.immutable, true);
assert.strictEqual(view.byteLength, bytes.length);
assert.deepStrictEqual([...view], bytes);

const mutable = Uint8Array.from(bytes);
binding.Detach(mutable);
assert.strictEqual(binding.IsDetached(mutable.buffer), true);
assert.strictEqual(mutable.byteLength, 0);
}
Loading