| 1 |
load(libdir + "asserts.js"); |
| 2 |
|
| 3 |
const TypedArrayLength = getSelfHostedValue("TypedArrayLength"); |
| 4 |
|
| 5 |
function testTypedArrayLength() { |
| 6 |
let ab = new ArrayBuffer(100, {maxByteLength: 100}); |
| 7 |
let typedArrays = [ |
| 8 |
new Int8Array(ab), |
| 9 |
new Int8Array(ab, 1), |
| 10 |
new Int8Array(ab, 2), |
| 11 |
new Int8Array(ab, 3), |
| 12 |
]; |
| 13 |
|
| 14 |
for (let i = 0; i < 200; ++i) { |
| 15 |
let ta = typedArrays[i & 3]; |
| 16 |
assertEq(TypedArrayLength(ta), 100 - (i & 3)); |
| 17 |
} |
| 18 |
} |
| 19 |
testTypedArrayLength(); |
| 20 |
|
| 21 |
function testTypedArrayLengthOutOfBounds() { |
| 22 |
let ab = new ArrayBuffer(100, {maxByteLength: 100}); |
| 23 |
let typedArrays = [ |
| 24 |
new Int8Array(ab, 0, 10), |
| 25 |
new Int8Array(ab, 1, 10), |
| 26 |
new Int8Array(ab, 2, 10), |
| 27 |
new Int8Array(ab, 3, 10), |
| 28 |
]; |
| 29 |
|
| 30 |
// Resize to zero to make all views out-of-bounds. |
| 31 |
ab.resize(0); |
| 32 |
|
| 33 |
for (let i = 0; i < 200; ++i) { |
| 34 |
let ta = typedArrays[i & 3]; |
| 35 |
assertThrowsInstanceOf(() => TypedArrayLength(ta), TypeError); |
| 36 |
} |
| 37 |
} |
| 38 |
testTypedArrayLengthOutOfBounds(); |
| 39 |
|
| 40 |
function testTypedArrayLengthDetached() { |
| 41 |
let ab = new ArrayBuffer(100, {maxByteLength: 100}); |
| 42 |
let typedArrays = [ |
| 43 |
new Int8Array(ab, 0, 10), |
| 44 |
new Int8Array(ab, 1, 10), |
| 45 |
new Int8Array(ab, 2, 10), |
| 46 |
new Int8Array(ab, 3, 10), |
| 47 |
]; |
| 48 |
|
| 49 |
// Detach the buffer. |
| 50 |
ab.transfer(); |
| 51 |
|
| 52 |
for (let i = 0; i < 200; ++i) { |
| 53 |
let ta = typedArrays[i & 3]; |
| 54 |
assertEq(TypedArrayLength(ta), 0); |
| 55 |
} |
| 56 |
} |
| 57 |
testTypedArrayLengthDetached(); |
| 58 |
|
| 59 |
function testTypedArrayLengthWithShared() { |
| 60 |
let ab = new SharedArrayBuffer(100, {maxByteLength: 100}); |
| 61 |
let typedArrays = [ |
| 62 |
new Int8Array(ab), |
| 63 |
new Int8Array(ab, 1), |
| 64 |
new Int8Array(ab, 2), |
| 65 |
new Int8Array(ab, 3), |
| 66 |
]; |
| 67 |
|
| 68 |
for (let i = 0; i < 200; ++i) { |
| 69 |
let ta = typedArrays[i & 3]; |
| 70 |
assertEq(TypedArrayLength(ta), 100 - (i & 3)); |
| 71 |
} |
| 72 |
} |
| 73 |
testTypedArrayLengthWithShared(); |