Skip to content

Commit c722f14

Browse files
committed
[immutable-arraybuffer] ArrayBuffer.prototype.immutable
1 parent f807ed7 commit c722f14

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed
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-get-arraybuffer.prototype.immutable
6+
description: Checks the "immutable" property of ArrayBuffer.prototype.
7+
info: |
8+
ArrayBuffer.prototype.immutable is an accessor property whose set accessor
9+
function is undefined.
10+
11+
ECMAScript Standard Built-in Objects
12+
...
13+
For functions that are specified as properties of objects, the name value is
14+
the property name string used to access the function. Functions that are
15+
specified as get or set accessor functions of built-in properties have "get"
16+
or "set" (respectively) passed to the prefix parameter when calling
17+
CreateBuiltinFunction.
18+
...
19+
Every accessor property described in clauses 19 through 28 and in Annex B.2
20+
has the attributes { [[Enumerable]]: false, [[Configurable]]: true } unless
21+
otherwise specified. If only a get accessor function is described, the set
22+
accessor function is the default value, undefined.
23+
features: [immutable-arraybuffer]
24+
includes: [propertyHelper.js]
25+
---*/
26+
27+
var desc = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "immutable");
28+
var isHardened = Object.isFrozen(Object);
29+
if (isHardened && desc.get) {
30+
Object.defineProperty(desc, "get", { configurable: false });
31+
}
32+
33+
assert.sameValue(desc.value, undefined);
34+
assert.sameValue(desc.set, undefined);
35+
verifyCallableProperty(desc, "get", "get immutable", 0, { configurable: !isHardened });
36+
37+
verifyPrimordialProperty(ArrayBuffer.prototype, "immutable", {
38+
enumerable: false,
39+
configurable: true
40+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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-get-arraybuffer.prototype.immutable
6+
description: Return value according to the [[ArrayBufferIsImmutable]] internal slot.
7+
info: |
8+
get ArrayBuffer.prototype.immutable
9+
1. Let O be the this value.
10+
2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
11+
3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
12+
4. Return IsImmutableBuffer(O).
13+
14+
IsImmutableBuffer ( arrayBuffer )
15+
1. If arrayBuffer has an [[ArrayBufferIsImmutable]] internal slot, return true.
16+
2. Return false.
17+
features: [ArrayBuffer, immutable-arraybuffer]
18+
includes: [detachArrayBuffer.js]
19+
---*/
20+
21+
var ab = new ArrayBuffer(1);
22+
assert.sameValue(ab.immutable, false);
23+
24+
var iab = ab.transferToImmutable();
25+
assert.sameValue(iab.immutable, true);
26+
27+
$DETACHBUFFER(ab);
28+
assert.sameValue(ab.immutable, false);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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-get-arraybuffer.prototype.immutable
6+
description: >
7+
Throws a TypeError exception when `this` does not have a [[ArrayBufferData]]
8+
internal slot
9+
info: |
10+
get ArrayBuffer.prototype.immutable
11+
1. Let O be the this value.
12+
2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
13+
features: [DataView, Int8Array, ArrayBuffer, immutable-arraybuffer]
14+
---*/
15+
16+
var getter = Object.getOwnPropertyDescriptor(
17+
ArrayBuffer.prototype, "immutable"
18+
).get;
19+
20+
assert.sameValue(typeof getter, "function", "Getter must exist.");
21+
22+
var badReceivers = [
23+
["plain object", {}],
24+
["array", []],
25+
["function", function(){}],
26+
["ArrayBuffer.prototype", ArrayBuffer.prototype],
27+
["TypedArray", new Int8Array(8)],
28+
["DataView", new DataView(new ArrayBuffer(8), 0)]
29+
];
30+
31+
for (var i = 0; i < badReceivers.length; i++) {
32+
var label = badReceivers[i][0];
33+
var value = badReceivers[i][1];
34+
assert.throws(TypeError, function() {
35+
getter.call(value);
36+
}, label);
37+
}
38+
39+
assert.throws(TypeError, function() {
40+
ArrayBuffer.prototype.immutable;
41+
}, "invoked as prototype property access");
42+
43+
assert.throws(TypeError, function() {
44+
getter();
45+
}, "invoked as function");
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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-get-arraybuffer.prototype.immutable
6+
description: Throws a TypeError exception when `this` is not an object
7+
info: |
8+
get ArrayBuffer.prototype.immutable
9+
1. Let O be the this value.
10+
2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
11+
features: [ArrayBuffer, immutable-arraybuffer]
12+
---*/
13+
14+
var getter = Object.getOwnPropertyDescriptor(
15+
ArrayBuffer.prototype, "immutable"
16+
).get;
17+
18+
assert.sameValue(typeof getter, "function", "Getter must exist.");
19+
20+
var badReceivers = [
21+
["undefined", undefined],
22+
["null", null],
23+
["number", 42],
24+
["string", "1"],
25+
["true", true],
26+
["false", false],
27+
typeof Symbol === "undefined" ? undefined : ["unique symbol", Symbol("s")],
28+
typeof Symbol === "undefined" || !Symbol.for ? undefined : ["registered symbol", Symbol.for("s")],
29+
typeof BigInt === "undefined" ? undefined : ["bigint", BigInt(1)]
30+
];
31+
32+
for (var i = 0; i < badReceivers.length; i++) {
33+
if (!badReceivers[i]) continue;
34+
var label = badReceivers[i][0];
35+
var value = badReceivers[i][1];
36+
assert.throws(TypeError, function() {
37+
getter.call(value);
38+
}, label);
39+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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-get-arraybuffer.prototype.immutable
6+
description: Throws a TypeError exception when `this` is a SharedArrayBuffer
7+
info: |
8+
get ArrayBuffer.prototype.immutable
9+
1. Let O be the this value.
10+
2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
11+
3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
12+
features: [SharedArrayBuffer, ArrayBuffer, immutable-arraybuffer]
13+
---*/
14+
15+
var getter = Object.getOwnPropertyDescriptor(
16+
ArrayBuffer.prototype, "immutable"
17+
).get;
18+
19+
assert.sameValue(typeof getter, "function", "Getter must exist.");
20+
21+
var sab = new SharedArrayBuffer(4);
22+
assert.throws(TypeError, function() {
23+
getter.call(sab);
24+
});

0 commit comments

Comments
 (0)