| 1 |
// GENERATED, DO NOT EDIT |
| 2 |
// file: resizableArrayBufferUtils.js |
| 3 |
// Copyright 2023 the V8 project authors. All rights reserved. |
| 4 |
// This code is governed by the BSD license found in the LICENSE file. |
| 5 |
|
| 6 |
/*--- |
| 7 |
description: | |
| 8 |
Collection of helper constants and functions for testing resizable array buffers. |
| 9 |
defines: |
| 10 |
- floatCtors |
| 11 |
- ctors |
| 12 |
- MyBigInt64Array |
| 13 |
- CreateResizableArrayBuffer |
| 14 |
- MayNeedBigInt |
| 15 |
- Convert |
| 16 |
- ToNumbers |
| 17 |
- CreateRabForTest |
| 18 |
- CollectValuesAndResize |
| 19 |
- TestIterationAndResize |
| 20 |
features: [BigInt] |
| 21 |
---*/ |
| 22 |
// Helper to create subclasses without bombing out when `class` isn't supported |
| 23 |
function subClass(type) { |
| 24 |
try { |
| 25 |
return new Function('return class My' + type + ' extends ' + type + ' {}')(); |
| 26 |
} catch (e) {} |
| 27 |
} |
| 28 |
|
| 29 |
const MyUint8Array = subClass('Uint8Array'); |
| 30 |
const MyFloat32Array = subClass('Float32Array'); |
| 31 |
const MyBigInt64Array = subClass('BigInt64Array'); |
| 32 |
|
| 33 |
const builtinCtors = [ |
| 34 |
Uint8Array, |
| 35 |
Int8Array, |
| 36 |
Uint16Array, |
| 37 |
Int16Array, |
| 38 |
Uint32Array, |
| 39 |
Int32Array, |
| 40 |
Float32Array, |
| 41 |
Float64Array, |
| 42 |
Uint8ClampedArray, |
| 43 |
]; |
| 44 |
|
| 45 |
// Big(U)int64Array and Float16Array are newer features adding them above unconditionally |
| 46 |
// would cause implementations lacking it to fail every test which uses it. |
| 47 |
if (typeof Float16Array !== 'undefined') { |
| 48 |
builtinCtors.push(Float16Array); |
| 49 |
} |
| 50 |
|
| 51 |
if (typeof BigUint64Array !== 'undefined') { |
| 52 |
builtinCtors.push(BigUint64Array); |
| 53 |
} |
| 54 |
|
| 55 |
if (typeof BigInt64Array !== 'undefined') { |
| 56 |
builtinCtors.push(BigInt64Array); |
| 57 |
} |
| 58 |
|
| 59 |
const floatCtors = [ |
| 60 |
Float32Array, |
| 61 |
Float64Array, |
| 62 |
MyFloat32Array |
| 63 |
]; |
| 64 |
|
| 65 |
if (typeof Float16Array !== 'undefined') { |
| 66 |
floatCtors.push(Float16Array); |
| 67 |
} |
| 68 |
|
| 69 |
const ctors = builtinCtors.concat(MyUint8Array, MyFloat32Array); |
| 70 |
|
| 71 |
if (typeof MyBigInt64Array !== 'undefined') { |
| 72 |
ctors.push(MyBigInt64Array); |
| 73 |
} |
| 74 |
|
| 75 |
function CreateResizableArrayBuffer(byteLength, maxByteLength) { |
| 76 |
return new ArrayBuffer(byteLength, { maxByteLength: maxByteLength }); |
| 77 |
} |
| 78 |
|
| 79 |
function Convert(item) { |
| 80 |
if (typeof item == 'bigint') { |
| 81 |
return Number(item); |
| 82 |
} |
| 83 |
return item; |
| 84 |
} |
| 85 |
|
| 86 |
function ToNumbers(array) { |
| 87 |
let result = []; |
| 88 |
for (let i = 0; i < array.length; i++) { |
| 89 |
let item = array[i]; |
| 90 |
result.push(Convert(item)); |
| 91 |
} |
| 92 |
return result; |
| 93 |
} |
| 94 |
|
| 95 |
function MayNeedBigInt(ta, n) { |
| 96 |
assert.sameValue(typeof n, 'number'); |
| 97 |
if ((BigInt64Array !== 'undefined' && ta instanceof BigInt64Array) |
| 98 |
|| (BigUint64Array !== 'undefined' && ta instanceof BigUint64Array)) { |
| 99 |
return BigInt(n); |
| 100 |
} |
| 101 |
return n; |
| 102 |
} |
| 103 |
|
| 104 |
function CreateRabForTest(ctor) { |
| 105 |
const rab = CreateResizableArrayBuffer(4 * ctor.BYTES_PER_ELEMENT, 8 * ctor.BYTES_PER_ELEMENT); |
| 106 |
// Write some data into the array. |
| 107 |
const taWrite = new ctor(rab); |
| 108 |
for (let i = 0; i < 4; ++i) { |
| 109 |
taWrite[i] = MayNeedBigInt(taWrite, 2 * i); |
| 110 |
} |
| 111 |
return rab; |
| 112 |
} |
| 113 |
|
| 114 |
function CollectValuesAndResize(n, values, rab, resizeAfter, resizeTo) { |
| 115 |
if (typeof n == 'bigint') { |
| 116 |
values.push(Number(n)); |
| 117 |
} else { |
| 118 |
values.push(n); |
| 119 |
} |
| 120 |
if (values.length == resizeAfter) { |
| 121 |
rab.resize(resizeTo); |
| 122 |
} |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
function TestIterationAndResize(iterable, expected, rab, resizeAfter, newByteLength) { |
| 127 |
let values = []; |
| 128 |
let resized = false; |
| 129 |
var arrayValues = false; |
| 130 |
|
| 131 |
for (let value of iterable) { |
| 132 |
if (Array.isArray(value)) { |
| 133 |
arrayValues = true; |
| 134 |
values.push([ |
| 135 |
value[0], |
| 136 |
Number(value[1]) |
| 137 |
]); |
| 138 |
} else { |
| 139 |
values.push(Number(value)); |
| 140 |
} |
| 141 |
if (!resized && values.length == resizeAfter) { |
| 142 |
rab.resize(newByteLength); |
| 143 |
resized = true; |
| 144 |
} |
| 145 |
} |
| 146 |
if (!arrayValues) { |
| 147 |
assert.compareArray([].concat(values), expected, "TestIterationAndResize: list of iterated values"); |
| 148 |
} else { |
| 149 |
for (let i = 0; i < expected.length; i++) { |
| 150 |
assert.compareArray(values[i], expected[i], "TestIterationAndResize: list of iterated lists of values"); |
| 151 |
} |
| 152 |
} |
| 153 |
assert(resized, "TestIterationAndResize: resize condition should have been hit"); |
| 154 |
} |