summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/slice-detached.js
blob: be166531549bff2fccbd2eb057ab5392b72f7d1f (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Tests for detached ArrayBuffer checks in %TypedArray%.prototype.slice ( start, end ).

function* createTypedArrays(lengths = [0, 1, 4, 4096]) {
    // Test with eagerly created ArrayBuffer.
    for (let length of lengths) {
        let buffer = new ArrayBuffer(length * Int32Array.BYTES_PER_ELEMENT);
        let typedArray = new Int32Array(buffer);

        yield {typedArray, length, buffer() { return buffer; }};
    }

    // Test with lazily created ArrayBuffer.
    for (let length of lengths) {
        let typedArray = new Int32Array(length);

        yield {typedArray, length, buffer() { return typedArray.buffer; }};
    }
}

if (typeof detachArrayBuffer === "function") {
    // ArrayBuffer is detached when entering slice().
    for (let {typedArray, buffer} of createTypedArrays()) {
        detachArrayBuffer(buffer());
        assertThrowsInstanceOf(() => {
            typedArray.slice(0);
        }, TypeError, "ArrayBuffer is detached on function entry");
    }

    // ArrayBuffer is detached when computing ToInteger(start).
    for (let {typedArray, length, buffer} of createTypedArrays()) {
        let detached = false;
        let start = {
            valueOf() {
                assertEq(detached, false);
                detachArrayBuffer(buffer());
                assertEq(detached, false);
                detached = true;
                return 0;
            }
        };

        // Doesn't throw an error when no bytes are copied.
        if (length === 0) {
            typedArray.slice(start);
        } else {
            assertThrowsInstanceOf(() => {
                typedArray.slice(start);
            }, TypeError, "ArrayBuffer is detached in ToInteger(start)");
        }
        assertEq(detached, true, "detachArrayBuffer was called");
    }

    // ArrayBuffer is detached when computing ToInteger(end).
    for (let {typedArray, length, buffer} of createTypedArrays()) {
        let detached = false;
        let end = {
            valueOf() {
                assertEq(detached, false);
                detachArrayBuffer(buffer());
                assertEq(detached, false);
                detached = true;
                return length;
            }
        };

        // Doesn't throw an error when no bytes are copied.
        if (length === 0) {
            typedArray.slice(0, end);
        } else {
            assertThrowsInstanceOf(() => {
                typedArray.slice(0, end);
            }, TypeError, "ArrayBuffer is detached in ToInteger(end)");
        }
        assertEq(detached, true, "detachArrayBuffer was called");
    }

    // ArrayBuffer is detached in species constructor.
    for (let {typedArray, length, buffer} of createTypedArrays()) {
        let detached = false;
        typedArray.constructor = {
            [Symbol.species]: function(...args) {
                assertEq(detached, false);
                detachArrayBuffer(buffer());
                assertEq(detached, false);
                detached = true;
                return new Int32Array(...args);
            }
        };

        // Doesn't throw an error when no bytes are copied.
        if (length === 0) {
            typedArray.slice(0);
        } else {
            assertThrowsInstanceOf(() => {
                typedArray.slice(0);
            }, TypeError, "ArrayBuffer is detached in TypedArraySpeciesCreate(...)");
        }
        assertEq(detached, true, "detachArrayBuffer was called");
    }
}

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