diff --git a/test/built-ins/ArrayBuffer/prototype/resize/this-is-immutable-arraybuffer-object.js b/test/built-ins/ArrayBuffer/prototype/resize/this-is-immutable-arraybuffer-object.js new file mode 100644 index 0000000000..b6b6410652 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/this-is-immutable-arraybuffer-object.js @@ -0,0 +1,32 @@ +// Copyright (C) 2025 Richard Gibson. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-arraybuffer.prototype.resize +description: > + Throws a TypeError if `this` has an [[ArrayBufferIsImmutable]] internal slot. +info: | + ArrayBuffer.prototype.resize ( newLength ) + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + ... + 6. Assert: IsImmutableBuffer(O) is false. +features: [resizable-arraybuffer, immutable-arraybuffer] +includes: [compareArray.js] +---*/ + +var calls = []; + +var ab = (new ArrayBuffer(4)).transferToImmutable(); +assert.throws(TypeError, function() { + ab.resize(0); +}); +assert.throws(TypeError, function() { + ab.resize({ + valueOf() { + calls.push('newLength.valueOf'); + return 0; + } + }); +}); +assert.compareArray(calls, [], 'Must verify internal slots before reading newLength.'); diff --git a/test/built-ins/ArrayBuffer/prototype/slice/species-returns-immutable-arraybuffer.js b/test/built-ins/ArrayBuffer/prototype/slice/species-returns-immutable-arraybuffer.js new file mode 100644 index 0000000000..ca674ffda4 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/slice/species-returns-immutable-arraybuffer.js @@ -0,0 +1,55 @@ +// Copyright (C) 2025 Richard Gibson. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-arraybuffer.prototype.slice +description: > + Throws a TypeError if species constructor returns an immutable ArrayBuffer. +info: | + ArrayBuffer.prototype.slice ( start, end ) + 1. Let O be the this value. + ... + 14. Let bounds be ? ResolveBounds(len, start, end). + ... + 18. Let ctor be ? SpeciesConstructor(O, %ArrayBuffer%). + 19. Let new be ? Construct(ctor, « 𝔽(newLen) »). + ... + 23. If IsImmutableBuffer(new) is true, throw a TypeError exception. +features: [Symbol.species, immutable-arraybuffer] +includes: [compareArray.js] +---*/ + +var calls = []; + +var speciesConstructor = {}; +speciesConstructor[Symbol.species] = function(length) { + calls.push("Symbol.species(" + length + ")"); + return arrayBuffer.sliceToImmutable(); +}; + +var arrayBuffer = new ArrayBuffer(8); +arrayBuffer.constructor = speciesConstructor; + +assert.throws(TypeError, function() { + arrayBuffer.slice(); +}); +assert.compareArray(calls, ["Symbol.species(8)"]); + +calls = []; +assert.throws(TypeError, function() { + var start = { + valueOf() { + calls.push("start.valueOf"); + return 1; + } + }; + var end = { + valueOf() { + calls.push("end.valueOf"); + return 2; + } + }; + arrayBuffer.slice(start, end); +}); +assert.compareArray(calls, ["start.valueOf", "end.valueOf", "Symbol.species(1)"], + "Must read arguments before SpeciesConstructor."); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/this-is-immutable-arraybuffer.js b/test/built-ins/ArrayBuffer/prototype/transfer/this-is-immutable-arraybuffer.js new file mode 100644 index 0000000000..c1fb3092dd --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/this-is-immutable-arraybuffer.js @@ -0,0 +1,40 @@ +// Copyright (C) 2025 Richard Gibson. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-arraybuffer.prototype.transfer +description: Throws a TypeError if `this` is immutable. +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + 1. Let O be the this value. + 2. Return ? ArrayBufferCopyAndDetach(O, newLength, ~preserve-resizability~). + + ArrayBufferCopyAndDetach ( arrayBuffer, newLength, preserveResizability ) + 1. Perform ? RequireInternalSlot(arrayBuffer, [[ArrayBufferData]]). + 2. If IsSharedArrayBuffer(arrayBuffer) is true, throw a TypeError exception. + 3. If newLength is undefined, then + a. Let newByteLength be arrayBuffer.[[ArrayBufferByteLength]]. + 4. Else, + a. Let newByteLength be ? ToIndex(newLength). + 5. If IsDetachedBuffer(arrayBuffer) is true, throw a TypeError exception. + 6. If IsImmutableBuffer(arrayBuffer) is true, throw a TypeError exception. +features: [arraybuffer-transfer, immutable-arraybuffer] +includes: [compareArray.js] +---*/ + +var calls = []; + +var ab = (new ArrayBuffer(4)).transferToImmutable(); +assert.throws(TypeError, function() { + ab.transfer(); +}); +assert.throws(TypeError, function() { + ab.transfer({ + valueOf() { + calls.push("newLength.valueOf"); + return 1; + } + }); +}); +assert.compareArray(calls, ["newLength.valueOf"], + "Must read newLength before verifying mutability."); diff --git a/test/built-ins/ArrayBuffer/prototype/transferToFixedLength/this-is-immutable-arraybuffer.js b/test/built-ins/ArrayBuffer/prototype/transferToFixedLength/this-is-immutable-arraybuffer.js new file mode 100644 index 0000000000..25e3f3968d --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transferToFixedLength/this-is-immutable-arraybuffer.js @@ -0,0 +1,40 @@ +// Copyright (C) 2025 Richard Gibson. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-arraybuffer.prototype.transferToFixedLength +description: Throws a TypeError if `this` is immutable. +info: | + ArrayBuffer.prototype.transferToFixedLength ( [ newLength ] ) + 1. Let O be the this value. + 2. Return ? ArrayBufferCopyAndDetach(O, newLength, ~fixed-length~). + + ArrayBufferCopyAndDetach ( arrayBuffer, newLength, preserveResizability ) + 1. Perform ? RequireInternalSlot(arrayBuffer, [[ArrayBufferData]]). + 2. If IsSharedArrayBuffer(arrayBuffer) is true, throw a TypeError exception. + 3. If newLength is undefined, then + a. Let newByteLength be arrayBuffer.[[ArrayBufferByteLength]]. + 4. Else, + a. Let newByteLength be ? ToIndex(newLength). + 5. If IsDetachedBuffer(arrayBuffer) is true, throw a TypeError exception. + 6. If IsImmutableBuffer(arrayBuffer) is true, throw a TypeError exception. +features: [arraybuffer-transfer, immutable-arraybuffer] +includes: [compareArray.js] +---*/ + +var calls = []; + +var ab = (new ArrayBuffer(4)).transferToImmutable(); +assert.throws(TypeError, function() { + ab.transferToFixedLength(); +}); +assert.throws(TypeError, function() { + ab.transferToFixedLength({ + valueOf() { + calls.push("newLength.valueOf"); + return 1; + } + }); +}); +assert.compareArray(calls, ["newLength.valueOf"], + "Must read newLength before verifying mutability.");