[immutable-arraybuffer] Changes to existing ArrayBuffer.prototype properties (#4540)

This commit is contained in:
Richard Gibson 2025-08-27 19:23:40 -04:00 committed by GitHub
parent a07e038f37
commit 3fd4ec27f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 167 additions and 0 deletions

View File

@ -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.');

View File

@ -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.");

View File

@ -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.");

View File

@ -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.");