Skip to content

Commit

Permalink
RAB: Integrate staging tests for the .set method (#4173)
Browse files Browse the repository at this point in the history
* Import relevant files from #3888

* Removing parts in resizableArrayBufferUtils.js and adding it in includes,
and renaming helper function.
  • Loading branch information
ioannad authored Aug 5, 2024
1 parent ea37a19 commit c0942e0
Show file tree
Hide file tree
Showing 6 changed files with 1,177 additions and 0 deletions.
113 changes: 113 additions & 0 deletions test/built-ins/TypedArray/prototype/set/target-grow-mid-iteration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-%typedarray%.prototype.set
description: >
TypedArray.p.set behaves correctly on TypedArrays backed by resizable buffers
that are grown mid-iteration due to a Proxy source.
includes: [compareArray.js, resizableArrayBufferUtils.js]
features: [resizable-arraybuffer]
---*/

// Resizing will happen when we're calling Get for the `resizeAt`:th data
// element, but we haven't yet written it to the target.
function CreateSourceProxy(length, rab, resizeAt, resizeTo) {
let requestedIndices = [];
return new Proxy({}, {
get(target, prop, receiver) {
if (prop == 'length') {
return length;
}
requestedIndices.push(prop);
if (requestedIndices.length == resizeAt) {
rab.resize(resizeTo);
}
return true; // Can be converted to both BigInt and Number.
}
});
}
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
const fixedLength = new ctor(rab, 0, 4);
const resizeAt = 2;
const resizeTo = 6 * ctor.BYTES_PER_ELEMENT;
fixedLength.set(CreateSourceProxy(4, rab, resizeAt, resizeTo));
assert.compareArray(ToNumbers(fixedLength), [
1,
1,
1,
1
]);
assert.compareArray(ToNumbers(new ctor(rab)), [
1,
1,
1,
1,
0,
0
]);
}
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
const resizeAt = 1;
const resizeTo = 6 * ctor.BYTES_PER_ELEMENT;
fixedLengthWithOffset.set(CreateSourceProxy(2, rab, resizeAt, resizeTo));
assert.compareArray(ToNumbers(fixedLengthWithOffset), [
1,
1
]);
assert.compareArray(ToNumbers(new ctor(rab)), [
0,
2,
1,
1,
0,
0
]);
}
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
const lengthTracking = new ctor(rab, 0);
const resizeAt = 2;
const resizeTo = 6 * ctor.BYTES_PER_ELEMENT;
lengthTracking.set(CreateSourceProxy(2, rab, resizeAt, resizeTo));
assert.compareArray(ToNumbers(lengthTracking), [
1,
1,
4,
6,
0,
0
]);
assert.compareArray(ToNumbers(new ctor(rab)), [
1,
1,
4,
6,
0,
0
]);
}
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
const resizeAt = 1;
const resizeTo = 6 * ctor.BYTES_PER_ELEMENT;
lengthTrackingWithOffset.set(CreateSourceProxy(2, rab, resizeAt, resizeTo));
assert.compareArray(ToNumbers(lengthTrackingWithOffset), [
1,
1,
0,
0
]);
assert.compareArray(ToNumbers(new ctor(rab)), [
0,
2,
1,
1,
0,
0
]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-%typedarray%.prototype.set
description: >
TypedArray.p.set behaves correctly on TypedArrays backed by a
resizable buffer is grown due to the source's length getter
includes: [compareArray.js, resizableArrayBufferUtils.js]
features: [resizable-arraybuffer]
---*/

// Orig. array: [0, 2, 4, 6]
// [0, 2, 4, 6] << fixedLength
// [4, 6] << fixedLengthWithOffset
// [0, 2, 4, 6, ...] << lengthTracking
// [4, 6, ...] << lengthTrackingWithOffset
function CreateSourceProxy(length, rab, resizeTo) {
return new Proxy({}, {
get(target, prop, receiver) {
if (prop == 'length') {
rab.resize(resizeTo);
return length;
}
return true; // Can be converted to both BigInt and Number.
}
});
}

// Test that we still throw for lengthTracking TAs if the source length is
// too large, even though we resized in the length getter (we check against
// the original length).
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
const lengthTracking = new ctor(rab, 0);
const resizeTo = 6 * ctor.BYTES_PER_ELEMENT;
assert.throws(RangeError, () => {
lengthTracking.set(CreateSourceProxy(6, rab, resizeTo));
});
assert.compareArray(ToNumbers(new ctor(rab)), [
0,
2,
4,
6,
0,
0
]);
}
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
const resizeTo = 6 * ctor.BYTES_PER_ELEMENT;
assert.throws(RangeError, () => {
lengthTrackingWithOffset.set(CreateSourceProxy(4, rab, resizeTo));
});
assert.compareArray(ToNumbers(new ctor(rab)), [
0,
2,
4,
6,
0,
0
]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-%typedarray%.prototype.set
description: >
TypedArray.p.set behaves correctly on TypedArrays backed by resizable buffers
that are shrunk mid-iteration due to a Proxy source.
includes: [compareArray.js, resizableArrayBufferUtils.js]
features: [resizable-arraybuffer]
---*/

// Orig. array: [0, 2, 4, 6]
// [0, 2, 4, 6] << fixedLength
// [4, 6] << fixedLengthWithOffset
// [0, 2, 4, 6, ...] << lengthTracking
// [4, 6, ...] << lengthTrackingWithOffset

// Resizing will happen when we're calling Get for the `resizeAt`:th data
// element, but we haven't yet written it to the target.
function CreateSourceProxy(length, rab, resizeAt, resizeTo) {
let requestedIndices = [];
return new Proxy({}, {
get(target, prop, receiver) {
if (prop == 'length') {
return length;
}
requestedIndices.push(prop);
if (requestedIndices.length == resizeAt) {
rab.resize(resizeTo);
}
return true; // Can be converted to both BigInt and Number.
}
});
}
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
const fixedLength = new ctor(rab, 0, 4);
const resizeAt = 2;
const resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
fixedLength.set(CreateSourceProxy(4, rab, resizeAt, resizeTo));
assert.compareArray(ToNumbers(new ctor(rab)), [
1,
2,
4
]);
}
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
const resizeAt = 2;
const resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
fixedLengthWithOffset.set(CreateSourceProxy(2, rab, resizeAt, resizeTo));
assert.compareArray(ToNumbers(new ctor(rab)), [
0,
2,
1
]);
}
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
const lengthTracking = new ctor(rab, 0);
const resizeAt = 2;
const resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
lengthTracking.set(CreateSourceProxy(2, rab, resizeAt, resizeTo));
assert.compareArray(ToNumbers(lengthTracking), [
1,
1,
4
]);
assert.compareArray(ToNumbers(new ctor(rab)), [
1,
1,
4
]);
}
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
const resizeAt = 2;
const resizeTo = 3 * ctor.BYTES_PER_ELEMENT;
lengthTrackingWithOffset.set(CreateSourceProxy(2, rab, resizeAt, resizeTo));
assert.compareArray(ToNumbers(lengthTrackingWithOffset), [1]);
assert.compareArray(ToNumbers(new ctor(rab)), [
0,
2,
1
]);
}

// Length-tracking TA goes OOB because of the offset.
for (let ctor of ctors) {
const rab = CreateRabForTest(ctor);
const lengthTrackingWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT);
const resizeAt = 1;
const resizeTo = 1 * ctor.BYTES_PER_ELEMENT;
lengthTrackingWithOffset.set(CreateSourceProxy(2, rab, resizeAt, resizeTo));
assert.compareArray(ToNumbers(new ctor(rab)), [0]);
}
Loading

0 comments on commit c0942e0

Please sign in to comment.