Skip to content

Commit ef3fa98

Browse files
committed
[immutable-arraybuffer] Changes to existing ArrayBuffer.prototype properties
1 parent a07e038 commit ef3fa98

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2025 Richard Gibson. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-arraybuffer.prototype.resize
6+
description: >
7+
Throws a TypeError if `this` has an [[ArrayBufferIsImmutable]] internal slot.
8+
info: |
9+
ArrayBuffer.prototype.resize ( newLength )
10+
1. Let O be the this value.
11+
2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]).
12+
...
13+
6. Assert: IsImmutableBuffer(O) is false.
14+
features: [resizable-arraybuffer, immutable-arraybuffer]
15+
includes: [compareArray.js]
16+
---*/
17+
18+
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function',
19+
'Method must exist.');
20+
21+
var calls = [];
22+
23+
var ab = (new ArrayBuffer(4)).transferToImmutable();
24+
assert.throws(TypeError, function() {
25+
ab.resize(0);
26+
});
27+
assert.throws(TypeError, function() {
28+
ab.resize({
29+
valueOf() {
30+
calls.push('newLength.valueOf');
31+
return 0;
32+
}
33+
});
34+
});
35+
assert.compareArray(calls, [], 'Must verify internal slots before reading newLength.');
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (C) 2025 Richard Gibson. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-arraybuffer.prototype.slice
6+
description: >
7+
Throws a TypeError if species constructor returns an immutable ArrayBuffer.
8+
info: |
9+
ArrayBuffer.prototype.slice ( start, end )
10+
1. Let O be the this value.
11+
...
12+
14. Let bounds be ? ResolveBounds(len, start, end).
13+
...
14+
18. Let ctor be ? SpeciesConstructor(O, %ArrayBuffer%).
15+
19. Let new be ? Construct(ctor, « 𝔽(newLen) »).
16+
...
17+
23. If IsImmutableBuffer(new) is true, throw a TypeError exception.
18+
features: [Symbol.species, immutable-arraybuffer]
19+
includes: [compareArray.js]
20+
---*/
21+
22+
var calls = [];
23+
24+
var speciesConstructor = {};
25+
speciesConstructor[Symbol.species] = function(length) {
26+
calls.push("Symbol.species(" + length + ")");
27+
return arrayBuffer.sliceToImmutable();
28+
};
29+
30+
var arrayBuffer = new ArrayBuffer(8);
31+
arrayBuffer.constructor = speciesConstructor;
32+
33+
assert.throws(TypeError, function() {
34+
arrayBuffer.slice();
35+
});
36+
assert.compareArray(calls, ["Symbol.species(8)"]);
37+
38+
calls = [];
39+
assert.throws(TypeError, function() {
40+
var start = {
41+
valueOf() {
42+
calls.push("start.valueOf");
43+
return 1;
44+
}
45+
};
46+
var end = {
47+
valueOf() {
48+
calls.push("end.valueOf");
49+
return 2;
50+
}
51+
};
52+
arrayBuffer.slice();
53+
});
54+
assert.compareArray(calls, ["start.valueOf", "end.valueOf", "Symbol.species(1)"],
55+
"Must read arguments before SpeciesConstructor.");
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (C) 2025 Richard Gibson. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-arraybuffer.prototype.transfer
6+
description: Throws a TypeError if `this` is immutable.
7+
info: |
8+
ArrayBuffer.prototype.transfer ( [ newLength ] )
9+
1. Let O be the this value.
10+
2. Return ? ArrayBufferCopyAndDetach(O, newLength, ~preserve-resizability~).
11+
12+
ArrayBufferCopyAndDetach ( arrayBuffer, newLength, preserveResizability )
13+
1. Perform ? RequireInternalSlot(arrayBuffer, [[ArrayBufferData]]).
14+
2. If IsSharedArrayBuffer(arrayBuffer) is true, throw a TypeError exception.
15+
3. If newLength is undefined, then
16+
a. Let newByteLength be arrayBuffer.[[ArrayBufferByteLength]].
17+
4. Else,
18+
a. Let newByteLength be ? ToIndex(newLength).
19+
5. If IsDetachedBuffer(arrayBuffer) is true, throw a TypeError exception.
20+
6. If IsImmutableBuffer(arrayBuffer) is true, throw a TypeError exception.
21+
features: [arraybuffer-transfer, immutable-arraybuffer]
22+
includes: [compareArray.js]
23+
---*/
24+
25+
var calls = [];
26+
27+
var ab = (new ArrayBuffer(4)).transferToImmutable();
28+
assert.throws(TypeError, function() {
29+
ab.transfer();
30+
});
31+
assert.throws(TypeError, function() {
32+
ab.transfer({
33+
valueOf() {
34+
calls.push("newLength.valueOf");
35+
return 1;
36+
}
37+
});
38+
});
39+
assert.compareArray(calls, ["newLength.valueOf"],
40+
"Must read newLength before verifying mutability.");
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (C) 2025 Richard Gibson. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-arraybuffer.prototype.transferToFixedLength
6+
description: Throws a TypeError if `this` is immutable.
7+
info: |
8+
ArrayBuffer.prototype.transferToFixedLength ( [ newLength ] )
9+
1. Let O be the this value.
10+
2. Return ? ArrayBufferCopyAndDetach(O, newLength, ~fixed-length~).
11+
12+
ArrayBufferCopyAndDetach ( arrayBuffer, newLength, preserveResizability )
13+
1. Perform ? RequireInternalSlot(arrayBuffer, [[ArrayBufferData]]).
14+
2. If IsSharedArrayBuffer(arrayBuffer) is true, throw a TypeError exception.
15+
3. If newLength is undefined, then
16+
a. Let newByteLength be arrayBuffer.[[ArrayBufferByteLength]].
17+
4. Else,
18+
a. Let newByteLength be ? ToIndex(newLength).
19+
5. If IsDetachedBuffer(arrayBuffer) is true, throw a TypeError exception.
20+
6. If IsImmutableBuffer(arrayBuffer) is true, throw a TypeError exception.
21+
features: [arraybuffer-transfer, immutable-arraybuffer]
22+
includes: [compareArray.js]
23+
---*/
24+
25+
var calls = [];
26+
27+
var ab = (new ArrayBuffer(4)).transferToImmutable();
28+
assert.throws(TypeError, function() {
29+
ab.transferToFixedLength();
30+
});
31+
assert.throws(TypeError, function() {
32+
ab.transferToFixedLength({
33+
valueOf() {
34+
calls.push("newLength.valueOf");
35+
return 1;
36+
}
37+
});
38+
});
39+
assert.compareArray(calls, ["newLength.valueOf"],
40+
"Must read newLength before verifying mutability.");

0 commit comments

Comments
 (0)