forked from LeenkxTeam/Kmake
87 lines
3.2 KiB
JavaScript
87 lines
3.2 KiB
JavaScript
|
|
// Copyright 2025 the V8 project authors. All rights reserved.
|
||
|
|
// Use of this source code is governed by a BSD-style license that can be
|
||
|
|
// found in the LICENSE file.
|
||
|
|
|
||
|
|
// Flags: --allow-natives-syntax --js-staging
|
||
|
|
// Flags: --experimental-wasm-rab-integration
|
||
|
|
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
d8.file.execute('test/mjsunit/typedarray-helpers.js');
|
||
|
|
|
||
|
|
const kPageSize = 0x10000;
|
||
|
|
|
||
|
|
function Pad(a, v, start, ctor, pages) {
|
||
|
|
for (let i = start; i < (pages * kPageSize) / ctor.BYTES_PER_ELEMENT; ++i) {
|
||
|
|
a.push(v);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function ZeroPad(a, start, ctor, pages) {
|
||
|
|
Pad(a, 0, start, ctor, pages);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Hand-crafted test to hit a somewhat esoteric code path in Array.p.concat.
|
||
|
|
(function ArrayConcatConcatDictionaryElementsProto() {
|
||
|
|
for (let ctor of ctors) {
|
||
|
|
const rab = CreateResizableArrayBufferViaWasm(1, 2);
|
||
|
|
const fixedLength = new ctor(rab, 0, 4);
|
||
|
|
const fixedLengthWithOffset = new ctor(rab, 2 * ctor.BYTES_PER_ELEMENT, 2);
|
||
|
|
const lengthTracking = new ctor(rab, 0);
|
||
|
|
const lengthTrackingWithOffset = new ctor(rab, kPageSize - 2 * ctor.BYTES_PER_ELEMENT);
|
||
|
|
|
||
|
|
const taWrite = new ctor(rab);
|
||
|
|
for (let i = 0; i < 4; ++i) {
|
||
|
|
WriteToTypedArray(taWrite, i, i + 1);
|
||
|
|
}
|
||
|
|
WriteToTypedArray(taWrite, taWrite.length - 2, 3);
|
||
|
|
WriteToTypedArray(taWrite, taWrite.length - 1, 4);
|
||
|
|
|
||
|
|
// Orig. array: [1, 2, 3, 4]
|
||
|
|
// [1, 2, 3, 4] << fixedLength
|
||
|
|
// [3, 4] << fixedLengthWithOffset
|
||
|
|
// [1, 2, 3, 4, ...] << lengthTracking
|
||
|
|
// [..., 3, 4] << lengthTrackingWithOffset
|
||
|
|
|
||
|
|
const largeIndex = kPageSize * 2;
|
||
|
|
function helper(ta) {
|
||
|
|
const newArray = [];
|
||
|
|
newArray[largeIndex] = 11111; // Force dictionary mode.
|
||
|
|
assertTrue(%HasDictionaryElements(newArray));
|
||
|
|
newArray.__proto__ = ta;
|
||
|
|
return Array.prototype.concat.call([], newArray);
|
||
|
|
}
|
||
|
|
|
||
|
|
function assertArrayContents(expectedStart, array) {
|
||
|
|
for (let i = 0; i < expectedStart.length; ++i) {
|
||
|
|
assertEquals(expectedStart[i], Number(array[i]));
|
||
|
|
}
|
||
|
|
assertEquals(largeIndex + 1, array.length);
|
||
|
|
// Don't check every index to keep the test run time reasonable.
|
||
|
|
for (let i = expectedStart.length; i < largeIndex - 1; i += 153) {
|
||
|
|
assertEquals(undefined, array[i]);
|
||
|
|
}
|
||
|
|
assertEquals(11111, Number(array[largeIndex]));
|
||
|
|
}
|
||
|
|
|
||
|
|
assertArrayContents([1, 2, 3, 4], helper(fixedLength));
|
||
|
|
assertArrayContents([3, 4], helper(fixedLengthWithOffset));
|
||
|
|
let expectedLengthTracking = [1, 2, 3, 4];
|
||
|
|
ZeroPad(expectedLengthTracking, 4, ctor, 1);
|
||
|
|
expectedLengthTracking[expectedLengthTracking.length - 2] = 3;
|
||
|
|
expectedLengthTracking[expectedLengthTracking.length - 1] = 4;
|
||
|
|
assertArrayContents(expectedLengthTracking, helper(lengthTracking));
|
||
|
|
assertArrayContents([3, 4], helper(lengthTrackingWithOffset));
|
||
|
|
|
||
|
|
// Wasm memories can't shrink.
|
||
|
|
assertThrows(() => rab.resize(0), RangeError);
|
||
|
|
|
||
|
|
// After detaching, all TAs behave like zero length.
|
||
|
|
%ArrayBufferDetachForceWasm(rab);
|
||
|
|
assertArrayContents([], helper(fixedLength));
|
||
|
|
assertArrayContents([], helper(fixedLengthWithOffset));
|
||
|
|
assertArrayContents([], helper(lengthTracking));
|
||
|
|
assertArrayContents([], helper(lengthTrackingWithOffset));
|
||
|
|
}
|
||
|
|
})();
|