Skip to content

Conversation

gibson042
Copy link
Member

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)

var view = new Uint8Array(source);
for (var i = 0; i < 8; i++) view[i] = i + 1;

var dest = new ArrayBuffer.sliceToImmutable();
Copy link
Contributor

Choose a reason for hiding this comment

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

new ArrayBuffer.sliceToImmutable() makes no sense. I guess it should be something like source.sliceToImmutable().

return String(value);
}

var make32ByteArrayBuffer() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo? var make32ByteArrayBuffer() -> function make32ByteArrayBuffer()

for (var j = 0; j < goodInputs.length; j++) {
var rawEnd = goodInputs[j][0];
var intEnd = goodInputs[j][1];
var intLength = intEnd - intStart;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not a correct calculation of the length of the slice. It does not work when intStart or intEnd is negative or when intStart > intEnd. Note that there is the same problem on lines 151 and 170.

calls,
["start[Symbol.toPrimitive](number)", "end[Symbol.toPrimitive](number)"],
"sliceToImmutable" + reprArgs("[Symbol.toPrimitive]", "[Symbol.toPrimitive]"));
);
Copy link
Contributor

@iamstolis iamstolis Oct 6, 2025

Choose a reason for hiding this comment

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

SyntaxError, duplicate ); (here and on the previous line). There is the same problem on lines 316 and 324.

[-1, RangeError],
[9007199254740992, RangeError], // Math.pow(2, 53) = 9007199254740992
[Infinity, RangeError],
[-Infinity, RangeError],
Copy link
Contributor

@iamstolis iamstolis Oct 6, 2025

Choose a reason for hiding this comment

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

I don't see why would these out of range numbers caused RangeErrors.

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.

3 participants