summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/constructor-length-too-large.js
blob: 7a7a785041b14c90342783e4e697c83a7013822b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// |reftest| skip-if(!xulRuntime.shell)

// Test that all TypedArray constructor variants throw a RangeError when
// attempting to create a too large array.

// The maximum typed array length is limited to `(INT32_MAX/BYTES_PER_ELEMENT)`
// on all 32-bit systems; on 64-bit systems the limit is 8GB presently.

const INT32_MAX = 2**31 - 1;
const EIGHTGB = 8 * 1024 * 1024 * 1024;

function tooLarge(elementSize) {
    if (largeArrayBufferSupported()) {
        return (EIGHTGB / elementSize) + 1;
    }
    return (INT32_MAX + 1) / elementSize;
}

// 22.2.4.2 TypedArray ( length )
for (let TA of typedArrayConstructors) {
    assertThrowsInstanceOf(() => new TA(tooLarge(1)), RangeError);
    assertThrowsInstanceOf(() => new TA(tooLarge(TA.BYTES_PER_ELEMENT)), RangeError);
}

// Test disabled because allocating a 2**30 Int8Array easily leads to OOMs.
//
// 22.2.4.3 TypedArray ( typedArray )
// const largeInt8Array = new Int8Array(2**30);
// for (let TA of typedArrayConstructors.filter(c => c.BYTES_PER_ELEMENT > 1)) {
//     assertThrowsInstanceOf(() => new TA(largeInt8Array), RangeError);
// }

// 22.2.4.4 TypedArray ( object )
for (let TA of typedArrayConstructors) {
    assertThrowsInstanceOf(() => new TA({length: tooLarge(1)}), RangeError);
    assertThrowsInstanceOf(() => new TA({length: tooLarge(TA.BYTES_PER_ELEMENT)}), RangeError);
}

if (typeof reportCompare === "function")
    reportCompare(true, true);