Skip to content

Commit

Permalink
[rab/gsab] Return false in [[PreventExtensions]] for variable-length TAs
Browse files Browse the repository at this point in the history
This is a spec normative change: tc39/ecma262#3453

Fixed: 374310073
Change-Id: I085260fea077bc27cf1e4f06d4389519e12c4e14
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6014979
Commit-Queue: Shu-yu Guo <[email protected]>
Auto-Submit: Shu-yu Guo <[email protected]>
Commit-Queue: Rezvan Mahdavi Hezaveh <[email protected]>
Reviewed-by: Rezvan Mahdavi Hezaveh <[email protected]>
Cr-Commit-Position: refs/heads/main@{#97166}
  • Loading branch information
syg authored and [email protected] committed Nov 13, 2024
1 parent 8eee455 commit d1d82e0
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) 2024 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-object.preventExtensions
description: Can't preventExtensions variable length TypedArrays
features: [SharedArrayBuffer, ArrayBuffer, resizable-arraybuffer]
includes: [resizableArrayBufferUtils.js]
---*/

for (let ctor of ctors) {
const rab = new ArrayBuffer(4 * ctor.BYTES_PER_ELEMENT, { maxByteLength: 8 * ctor.BYTES_PER_ELEMENT });
const fixedLength = new ctor(rab, 0, 4);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
const lengthTracking = new ctor(rab);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);

// "Fixed length" TAs backed by RABs can shrink then re-grow.
assert.throws(TypeError, function() {
Object.preventExtensions(fixedLength);
});
assert.throws(TypeError, function() {
Object.preventExtensions(fixedLengthWithOffset);
});
assert.throws(TypeError, function() {
Object.preventExtensions(lengthTracking);
});
assert.throws(TypeError, function() {
Object.preventExtensions(lengthTrackingWithOffset);
});
}

for (let ctor of ctors) {
const gsab = new SharedArrayBuffer(4 * ctor.BYTES_PER_ELEMENT, { maxByteLength: 8 * ctor.BYTES_PER_ELEMENT });
const fixedLength = new ctor(gsab, 0, 4);
const fixedLengthWithOffset = new ctor(gsab, 2 * ctor.BYTES_PER_ELEMENT, 2);
const lengthTracking = new ctor(gsab);
const lengthTrackingWithOffset = new ctor(gsab, 2 * ctor.BYTES_PER_ELEMENT);

// Fixed length TAs backed by GSABs can't shrink, and so are allowed.
Object.preventExtensions(fixedLength);
Object.preventExtensions(fixedLengthWithOffset);
assert.throws(TypeError, function() {
Object.preventExtensions(lengthTracking);
});
assert.throws(TypeError, function() {
Object.preventExtensions(lengthTrackingWithOffset);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) 2024 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-object.seal
description: Can't seal variable length TypedArrays
features: [SharedArrayBuffer, ArrayBuffer, resizable-arraybuffer]
includes: [resizableArrayBufferUtils.js]
---*/

for (let ctor of ctors) {
const rab = new ArrayBuffer(4 * ctor.BYTES_PER_ELEMENT, { maxByteLength: 8 * ctor.BYTES_PER_ELEMENT });
const fixedLength = new ctor(rab, 0, 4);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
const lengthTracking = new ctor(rab);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);

// "Fixed length" TAs backed by RABs can shrink then re-grow.
assert.throws(TypeError, function() {
Object.seal(fixedLength);
});
assert.throws(TypeError, function() {
Object.seal(fixedLengthWithOffset);
});
assert.throws(TypeError, function() {
Object.seal(lengthTracking);
});
assert.throws(TypeError, function() {
Object.seal(lengthTrackingWithOffset);
});
}

for (let ctor of ctors) {
const gsab = new SharedArrayBuffer(4 * ctor.BYTES_PER_ELEMENT, { maxByteLength: 8 * ctor.BYTES_PER_ELEMENT });
const fixedLength = new ctor(gsab, 0, 4);
const fixedLengthWithOffset = new ctor(gsab, 2 * ctor.BYTES_PER_ELEMENT, 2);
const lengthTracking = new ctor(gsab);
const lengthTrackingWithOffset = new ctor(gsab, 2 * ctor.BYTES_PER_ELEMENT);

// Fixed length TAs backed by GSABs can't shrink, and so are allowed.
Object.seal(fixedLength);
Object.seal(fixedLengthWithOffset);
assert.throws(TypeError, function() {
Object.seal(lengthTracking);
});
assert.throws(TypeError, function() {
Object.seal(lengthTrackingWithOffset);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (C) 2024 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-Reflect.preventExtensions
description: Can't preventExtensions variable length TypedArrays
features: [SharedArrayBuffer, ArrayBuffer, resizable-arraybuffer]
includes: [resizableArrayBufferUtils.js]
---*/

for (let ctor of ctors) {
const rab = new ArrayBuffer(4 * ctor.BYTES_PER_ELEMENT, { maxByteLength: 8 * ctor.BYTES_PER_ELEMENT });
const fixedLength = new ctor(rab, 0, 4);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
const lengthTracking = new ctor(rab);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);

// "Fixed length" TAs backed by RABs can shrink then re-grow.
assert.sameValue(Reflect.preventExtensions(fixedLength), false);
assert.sameValue(Reflect.preventExtensions(fixedLengthWithOffset), false);
assert.sameValue(Reflect.preventExtensions(lengthTracking), false);
assert.sameValue(Reflect.preventExtensions(lengthTrackingWithOffset), false);
}

for (let ctor of ctors) {
const gsab = new SharedArrayBuffer(4 * ctor.BYTES_PER_ELEMENT, { maxByteLength: 8 * ctor.BYTES_PER_ELEMENT });
const fixedLength = new ctor(gsab, 0, 4);
const fixedLengthWithOffset = new ctor(gsab, 2 * ctor.BYTES_PER_ELEMENT, 2);
const lengthTracking = new ctor(gsab);
const lengthTrackingWithOffset = new ctor(gsab, 2 * ctor.BYTES_PER_ELEMENT);

// Fixed length TAs backed by GSABs can't shrink, and so are allowed.
assert.sameValue(Reflect.preventExtensions(fixedLength), true);
assert.sameValue(Reflect.preventExtensions(fixedLengthWithOffset), true);
assert.sameValue(Reflect.preventExtensions(lengthTracking), false);
assert.sameValue(Reflect.preventExtensions(lengthTrackingWithOffset), false);
}

0 comments on commit d1d82e0

Please sign in to comment.