| 1 |
const types = [ |
| 2 |
"Int8", |
| 3 |
"Int16", |
| 4 |
"Int32", |
| 5 |
"Uint8", |
| 6 |
"Uint16", |
| 7 |
"Uint32", |
| 8 |
"Uint8Clamped", |
| 9 |
"Float16", |
| 10 |
"Float32", |
| 11 |
"Float64", |
| 12 |
]; |
| 13 |
|
| 14 |
function convert(type, value) { |
| 15 |
let num = Number(value); |
| 16 |
switch (type) { |
| 17 |
case "Int8": |
| 18 |
return ((num | 0) << 24) >> 24; |
| 19 |
case "Int16": |
| 20 |
return ((num | 0) << 16) >> 16; |
| 21 |
case "Int32": |
| 22 |
return (num | 0); |
| 23 |
case "Uint8": |
| 24 |
return (num >>> 0) & 0xff; |
| 25 |
case "Uint16": |
| 26 |
return (num >>> 0) & 0xffff; |
| 27 |
case "Uint32": |
| 28 |
return (num >>> 0); |
| 29 |
case "Uint8Clamped": { |
| 30 |
if (Number.isNaN(num)) { |
| 31 |
return 0; |
| 32 |
} |
| 33 |
let clamped = Math.max(0, Math.min(num, 255)); |
| 34 |
let f = Math.floor(clamped); |
| 35 |
if (clamped < f + 0.5) { |
| 36 |
return f; |
| 37 |
} |
| 38 |
if (clamped > f + 0.5) { |
| 39 |
return f + 1; |
| 40 |
} |
| 41 |
return f + (f & 1); |
| 42 |
} |
| 43 |
case "Float16": |
| 44 |
return Math.f16round(num); |
| 45 |
case "Float32": |
| 46 |
return Math.fround(num); |
| 47 |
case "Float64": |
| 48 |
return num; |
| 49 |
} |
| 50 |
throw new Error(); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
function runTest(type, initial, values) { |
| 55 |
let expected = values.map(v => convert(type, v)); |
| 56 |
assertEq( |
| 57 |
expected.some(e => Object.is(e, initial)), |
| 58 |
false, |
| 59 |
"initial must be different from the expected values" |
| 60 |
); |
| 61 |
|
| 62 |
// Create a fresh function to ensure ICs are specific to a single TypedArray kind. |
| 63 |
let test = Function("initial, values, expected", ` |
| 64 |
let ta = new ${type}Array(1); |
| 65 |
for (let i = 0; i < 200; ++i) { |
| 66 |
ta[0] = initial; |
| 67 |
ta[0] = values[i % values.length]; |
| 68 |
assertEq(ta[0], expected[i % expected.length]); |
| 69 |
} |
| 70 |
`); |
| 71 |
test(initial, values, expected); |
| 72 |
} |
| 73 |
|
| 74 |
const tests = [ |
| 75 |
// |null| is coerced to zero. |
| 76 |
{ |
| 77 |
initial: 1, |
| 78 |
values: [null], |
| 79 |
}, |
| 80 |
|
| 81 |
// |undefined| is coerced to zero or NaN. |
| 82 |
{ |
| 83 |
initial: 1, |
| 84 |
values: [undefined], |
| 85 |
}, |
| 86 |
|
| 87 |
// |false| is coerced to zero and |true| is coerced to one. |
| 88 |
{ |
| 89 |
initial: 2, |
| 90 |
values: [false, true], |
| 91 |
}, |
| 92 |
|
| 93 |
// Strings without a fractional part. |
| 94 |
{ |
| 95 |
initial: 42, |
| 96 |
values: [ |
| 97 |
"0", "1", "10", "111", "128", "256", "0x7fffffff", "0xffffffff", |
| 98 |
], |
| 99 |
}, |
| 100 |
|
| 101 |
// Strings without a fractional part, but a leading minus sign. |
| 102 |
{ |
| 103 |
initial: 42, |
| 104 |
values: [ |
| 105 |
"-0", "-1", "-10", "-111", "-128", "-256", "-2147483647", "-4294967295", |
| 106 |
], |
| 107 |
}, |
| 108 |
|
| 109 |
// Strings with a fractional number part. |
| 110 |
{ |
| 111 |
initial: 42, |
| 112 |
values: [ |
| 113 |
"0.1", "1.2", "10.8", "111.9", |
| 114 |
"-0.1", "-1.2", "-10.8", "-111.9", |
| 115 |
], |
| 116 |
}, |
| 117 |
|
| 118 |
// Special values and strings not parseable as a number. |
| 119 |
{ |
| 120 |
initial: 42, |
| 121 |
values: ["Infinity", "-Infinity", "NaN", "foobar"], |
| 122 |
}, |
| 123 |
]; |
| 124 |
|
| 125 |
for (let type of types) { |
| 126 |
for (let {initial, values} of tests) { |
| 127 |
runTest(type, initial, values); |
| 128 |
} |
| 129 |
} |