From d1d82e0534eb6c71a7288cd6e80fd64c5527dff5 Mon Sep 17 00:00:00 2001 From: Shu-yu Guo Date: Wed, 13 Nov 2024 12:19:29 -0800 Subject: [PATCH] [rab/gsab] Return false in [[PreventExtensions]] for variable-length TAs This is a spec normative change: https://github.com/tc39/ecma262/pull/3453 Fixed: 374310073 Change-Id: I085260fea077bc27cf1e4f06d4389519e12c4e14 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6014979 Commit-Queue: Shu-yu Guo Auto-Submit: Shu-yu Guo Commit-Queue: Rezvan Mahdavi Hezaveh Reviewed-by: Rezvan Mahdavi Hezaveh Cr-Commit-Position: refs/heads/main@{#97166} --- ...Extensions-variable-length-typed-arrays.js | 49 +++++++++++++++++++ .../seal/seal-variable-length-typed-arrays.js | 49 +++++++++++++++++++ ...Extensions-variable-length-typed-arrays.js | 37 ++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 test/staging/built-ins/Object/preventExtensions/preventExtensions-variable-length-typed-arrays.js create mode 100644 test/staging/built-ins/Object/seal/seal-variable-length-typed-arrays.js create mode 100644 test/staging/built-ins/Reflect/preventExtensions/preventExtensions-variable-length-typed-arrays.js diff --git a/test/staging/built-ins/Object/preventExtensions/preventExtensions-variable-length-typed-arrays.js b/test/staging/built-ins/Object/preventExtensions/preventExtensions-variable-length-typed-arrays.js new file mode 100644 index 00000000000..df75dac91b4 --- /dev/null +++ b/test/staging/built-ins/Object/preventExtensions/preventExtensions-variable-length-typed-arrays.js @@ -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); + }); +} diff --git a/test/staging/built-ins/Object/seal/seal-variable-length-typed-arrays.js b/test/staging/built-ins/Object/seal/seal-variable-length-typed-arrays.js new file mode 100644 index 00000000000..2a2871da4d1 --- /dev/null +++ b/test/staging/built-ins/Object/seal/seal-variable-length-typed-arrays.js @@ -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); + }); +} diff --git a/test/staging/built-ins/Reflect/preventExtensions/preventExtensions-variable-length-typed-arrays.js b/test/staging/built-ins/Reflect/preventExtensions/preventExtensions-variable-length-typed-arrays.js new file mode 100644 index 00000000000..95d11c017fb --- /dev/null +++ b/test/staging/built-ins/Reflect/preventExtensions/preventExtensions-variable-length-typed-arrays.js @@ -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); +}