Skip to content

Conversation

gibson042
Copy link
Contributor

Ref #4509

New ArrayBuffer.prototype properties

  • ArrayBuffer.prototype.sliceToImmutable
    • verifyPrimordialCallableProperty(ArrayBuffer.prototype, "sliceToImmutable", "sliceToImmutable", 2);
    • brand-checking (throws TypeError unless receiver is an ArrayBuffer and not a SharedArrayBuffer), before resolving bounds
    • throws TypeError if receiver is detached, before resolving bounds
    • resolves bounds after passing the preceding checks—first start, clamping negative values to [0, len - 1] and positive values to [0, len], then end, defaulting to len and clamping negative values to [0, len - 1] and positive values to [0, len] (and capturing len before coercing either start or end)
    • throws TypeError if receiver was detached by bounds resolution, after bounds resolution
    • throws RangeError if length < specified upper bound, after passing the second detachment check and even if length was ≥ specified upper bound until bounds resolution
    • returns an immutable ArrayBuffer with the correct length and contents (both unaffected by subsequent changes to the original receiver, including detachment)

@gibson042 gibson042 requested a review from a team as a code owner August 1, 2025 01:26
@gibson042 gibson042 force-pushed the 2025-07-immutable-arraybuffer-slicetoimmutable branch from 51629c7 to f9ed2c1 Compare August 1, 2025 01:30
Copy link
Contributor

@ptomato ptomato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments, feel free to merge when resolved

---*/

var fn = ArrayBuffer.prototype.sliceToImmutable;
assert.sameValue(typeof fn, "function", "Method must exist.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.sameValue(typeof fn, "function", "Method must exist.");

I think this is sufficiently implied by the immutable-arraybuffer feature flag.

---*/

var fn = ArrayBuffer.prototype.sliceToImmutable;
assert.sameValue(typeof fn, "function", "Method must exist.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Comment on lines +45 to +46
var label = badReceivers[i][0];
var value = badReceivers[i][1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break on implementations without Symbol or BigInt, I think you probably want if (!badReceivers[i]) continue;

---*/

var fn = ArrayBuffer.prototype.sliceToImmutable;
assert.sameValue(typeof fn, "function", "Method must exist.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already implied by feature flag.

Comment on lines +124 to +128
"sliceToImmutable(" + repr(rawStart) + ", " + repr(rawEnd) + ")");
assert.compareArray(new Uint8Array(dest), expectContents,
"sliceToImmutable(" + repr(rawStart) + ", " + repr(rawEnd) + ")");
assert.sameValue(dest.immutable, true,
"sliceToImmutable(" + repr(rawStart) + ", " + repr(rawEnd) + ")");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion, add something to these assertion messages to distinguish which assertion is failing (byte length, contents, or immutability) (Same for below)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants