-
Notifications
You must be signed in to change notification settings - Fork 499
[immutable-arraybuffer] ArrayBuffer.prototype.immutable #4541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gibson042
wants to merge
2
commits into
tc39:main
Choose a base branch
from
gibson042:2025-07-immutable-arraybuffer-arraybuffer-prototype-immutable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
test/built-ins/ArrayBuffer/prototype/immutable/prop-desc.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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-get-arraybuffer.prototype.immutable | ||
description: Checks the "immutable" property of ArrayBuffer.prototype. | ||
info: | | ||
ArrayBuffer.prototype.immutable is an accessor property whose set accessor | ||
function is undefined. | ||
|
||
ECMAScript Standard Built-in Objects | ||
... | ||
For functions that are specified as properties of objects, the name value is | ||
the property name string used to access the function. Functions that are | ||
specified as get or set accessor functions of built-in properties have "get" | ||
or "set" (respectively) passed to the prefix parameter when calling | ||
CreateBuiltinFunction. | ||
... | ||
Every accessor property described in clauses 19 through 28 and in Annex B.2 | ||
has the attributes { [[Enumerable]]: false, [[Configurable]]: true } unless | ||
otherwise specified. If only a get accessor function is described, the set | ||
accessor function is the default value, undefined. | ||
features: [immutable-arraybuffer] | ||
includes: [propertyHelper.js] | ||
---*/ | ||
|
||
var desc = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "immutable"); | ||
var isHardened = Object.isFrozen(Object); | ||
if (isHardened && desc.get) { | ||
Object.defineProperty(desc, "get", { configurable: false }); | ||
} | ||
|
||
assert.sameValue(desc.value, undefined); | ||
assert.sameValue(desc.set, undefined); | ||
verifyCallableProperty(desc, "get", "get immutable", 0, { configurable: !isHardened }); | ||
|
||
verifyPrimordialProperty(ArrayBuffer.prototype, "immutable", { | ||
enumerable: false, | ||
configurable: true | ||
}); |
28 changes: 28 additions & 0 deletions
28
test/built-ins/ArrayBuffer/prototype/immutable/return-immutable.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (C) 2025 Richard Gibson. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-get-arraybuffer.prototype.immutable | ||
description: Return value according to the [[ArrayBufferIsImmutable]] internal slot. | ||
info: | | ||
get ArrayBuffer.prototype.immutable | ||
1. Let O be the this value. | ||
2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). | ||
3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. | ||
4. Return IsImmutableBuffer(O). | ||
|
||
IsImmutableBuffer ( arrayBuffer ) | ||
1. If arrayBuffer has an [[ArrayBufferIsImmutable]] internal slot, return true. | ||
2. Return false. | ||
features: [ArrayBuffer, immutable-arraybuffer] | ||
includes: [detachArrayBuffer.js] | ||
---*/ | ||
|
||
var ab = new ArrayBuffer(1); | ||
assert.sameValue(ab.immutable, false); | ||
|
||
var iab = ab.transferToImmutable(); | ||
assert.sameValue(iab.immutable, true); | ||
|
||
$DETACHBUFFER(ab); | ||
assert.sameValue(ab.immutable, false); |
45 changes: 45 additions & 0 deletions
45
test/built-ins/ArrayBuffer/prototype/immutable/this-has-no-arraybufferdata-internal.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (C) 2025 Richard Gibson. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-get-arraybuffer.prototype.immutable | ||
description: > | ||
Throws a TypeError exception when `this` does not have a [[ArrayBufferData]] | ||
internal slot | ||
info: | | ||
get ArrayBuffer.prototype.immutable | ||
1. Let O be the this value. | ||
2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). | ||
features: [DataView, Int8Array, ArrayBuffer, immutable-arraybuffer] | ||
---*/ | ||
|
||
var getter = Object.getOwnPropertyDescriptor( | ||
ArrayBuffer.prototype, "immutable" | ||
).get; | ||
|
||
assert.sameValue(typeof getter, "function", "Getter must exist."); | ||
|
||
var badReceivers = [ | ||
["plain object", {}], | ||
["array", []], | ||
["function", function(){}], | ||
["ArrayBuffer.prototype", ArrayBuffer.prototype], | ||
["TypedArray", new Int8Array(8)], | ||
["DataView", new DataView(new ArrayBuffer(8), 0)] | ||
]; | ||
|
||
for (var i = 0; i < badReceivers.length; i++) { | ||
var label = badReceivers[i][0]; | ||
var value = badReceivers[i][1]; | ||
assert.throws(TypeError, function() { | ||
getter.call(value); | ||
}, label); | ||
} | ||
|
||
assert.throws(TypeError, function() { | ||
ArrayBuffer.prototype.immutable; | ||
}, "invoked as prototype property access"); | ||
|
||
assert.throws(TypeError, function() { | ||
getter(); | ||
}, "invoked as function"); |
39 changes: 39 additions & 0 deletions
39
test/built-ins/ArrayBuffer/prototype/immutable/this-is-not-object.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (C) 2025 Richard Gibson. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-get-arraybuffer.prototype.immutable | ||
description: Throws a TypeError exception when `this` is not an object | ||
info: | | ||
get ArrayBuffer.prototype.immutable | ||
1. Let O be the this value. | ||
2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). | ||
features: [ArrayBuffer, immutable-arraybuffer] | ||
---*/ | ||
|
||
var getter = Object.getOwnPropertyDescriptor( | ||
ArrayBuffer.prototype, "immutable" | ||
).get; | ||
|
||
assert.sameValue(typeof getter, "function", "Getter must exist."); | ||
|
||
var badReceivers = [ | ||
["undefined", undefined], | ||
["null", null], | ||
["number", 42], | ||
["string", "1"], | ||
["true", true], | ||
["false", false], | ||
typeof Symbol === "undefined" ? undefined : ["unique symbol", Symbol("s")], | ||
typeof Symbol === "undefined" || !Symbol.for ? undefined : ["registered symbol", Symbol.for("s")], | ||
typeof BigInt === "undefined" ? undefined : ["bigint", BigInt(1)] | ||
]; | ||
|
||
for (var i = 0; i < badReceivers.length; i++) { | ||
if (!badReceivers[i]) continue; | ||
var label = badReceivers[i][0]; | ||
var value = badReceivers[i][1]; | ||
assert.throws(TypeError, function() { | ||
getter.call(value); | ||
}, label); | ||
} |
24 changes: 24 additions & 0 deletions
24
test/built-ins/ArrayBuffer/prototype/immutable/this-is-sharedarraybuffer.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (C) 2025 Richard Gibson. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-get-arraybuffer.prototype.immutable | ||
description: Throws a TypeError exception when `this` is a SharedArrayBuffer | ||
info: | | ||
get ArrayBuffer.prototype.immutable | ||
1. Let O be the this value. | ||
2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). | ||
3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. | ||
features: [SharedArrayBuffer, ArrayBuffer, immutable-arraybuffer] | ||
---*/ | ||
|
||
var getter = Object.getOwnPropertyDescriptor( | ||
ArrayBuffer.prototype, "immutable" | ||
).get; | ||
|
||
assert.sameValue(typeof getter, "function", "Getter must exist."); | ||
|
||
var sab = new SharedArrayBuffer(4); | ||
assert.throws(TypeError, function() { | ||
getter.call(sab); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should be in individual tests, if we support it at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed on
defineProperty
(and removed), but varying theconfigurable
assumption is pretty much necessary to avoid excessive work in a hardened environment.