summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/set-detached.js
blob: 4f9226c608e1e707a32ac9f402dd8f7de9b9e656 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Tests for detached ArrayBuffer checks in %TypedArray%.prototype.set(array|typedArray, offset).

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

        yield {typedArray, buffer};
    }
}

if (typeof detachArrayBuffer === "function") {
    class ExpectedError extends Error {}

    // No detached check on function entry.
    for (let {typedArray, buffer} of createTypedArrays()) {
        detachArrayBuffer(buffer);

        assertThrowsInstanceOf(() => typedArray.set(null, {
            valueOf() {
                throw new ExpectedError();
            }
        }), ExpectedError);
    }

    // Check for detached buffer after calling ToInteger(offset). Test with:
    // - valid offset,
    // - too large offset,
    // - and negative offset.
    for (let [offset, error] of [[0, TypeError], [1000000, TypeError], [-1, RangeError]]) {
        for (let source of [[], [0], new Int32Array(0), new Int32Array(1)]) {
            for (let {typedArray, buffer} of createTypedArrays()) {
                assertThrowsInstanceOf(() => typedArray.set(source, {
                    valueOf() {
                        detachArrayBuffer(buffer);
                        return offset;
                    }
                }), error);
            }
        }
    }

    // Tests when called with detached typed array as source.
    for (let {typedArray} of createTypedArrays()) {
        for (let {typedArray: source, buffer: sourceBuffer} of createTypedArrays()) {
            detachArrayBuffer(sourceBuffer);

            assertThrowsInstanceOf(() => typedArray.set(source, {
                valueOf() {
                    throw new ExpectedError();
                }
            }), ExpectedError);
        }
    }

    // Check when detaching source buffer in ToInteger(offset). Test with:
    // - valid offset,
    // - too large offset,
    // - and negative offset.
    for (let [offset, error] of [[0, TypeError], [1000000, TypeError], [-1, RangeError]]) {
        for (let {typedArray} of createTypedArrays()) {
            for (let {typedArray: source, buffer: sourceBuffer} of createTypedArrays()) {
                assertThrowsInstanceOf(() => typedArray.set(source, {
                    valueOf() {
                        detachArrayBuffer(sourceBuffer);
                        return offset;
                    }
                }), error);
            }
        }
    }

    // Test when target and source use the same underlying buffer and
    // ToInteger(offset) detaches the buffer. Test with:
    // - same typed array,
    // - different typed array, but same element type,
    // - and different element type.
    for (let src of [ta => ta, ta => new Int32Array(ta.buffer), ta => new Float32Array(ta.buffer)]) {
        for (let {typedArray, buffer} of createTypedArrays()) {
            let source = src(typedArray);
            assertThrowsInstanceOf(() => typedArray.set(source, {
                valueOf() {
                    detachArrayBuffer(buffer);
                    return 0;
                }
            }), TypeError);
        }
    }

    // Test when Get(src, "length") detaches the buffer, but srcLength is 0.
    // Also use different offsets to ensure bounds checks use the typed array's
    // length value from before detaching the buffer.
    for (let offset of [() => 0, ta => Math.min(1, ta.length), ta => Math.max(0, ta.length - 1)]) {
        for (let {typedArray, buffer} of createTypedArrays()) {
            let source = {
                get length() {
                    detachArrayBuffer(buffer);
                    return 0;
                }
            };
            typedArray.set(source, offset(typedArray));
        }
    }

    // Test when ToLength(Get(src, "length")) detaches the buffer, but
    // srcLength is 0. Also use different offsets to ensure bounds checks use
    // the typed array's length value from before detaching the buffer.
    for (let offset of [() => 0, ta => Math.min(1, ta.length), ta => Math.max(0, ta.length - 1)]) {
        for (let {typedArray, buffer} of createTypedArrays()) {
            let source = {
                length: {
                    valueOf() {
                        detachArrayBuffer(buffer);
                        return 0;
                    }
                }
            };
            typedArray.set(source, offset(typedArray));
        }
    }

    // Test no TypeError is thrown when the typed array is detached and
    // srcLength > 0.
    for (let {typedArray, buffer} of createTypedArrays()) {
        let source = {
            length: {
                valueOf() {
                    detachArrayBuffer(buffer);
                    return 1;
                }
            }
        };
        if (typedArray.length === 0) {
            assertThrowsInstanceOf(() => typedArray.set(source), RangeError);
        } else {
            typedArray.set(source);
        }
    }

    // Same as above, but with side-effect when executing Get(src, "0").
    for (let {typedArray, buffer} of createTypedArrays()) {
        let source = {
            get 0() {
                throw new ExpectedError();
            },
            length: {
                valueOf() {
                    detachArrayBuffer(buffer);
                    return 1;
                }
            }
        };
        let err = typedArray.length === 0 ? RangeError : ExpectedError;
        assertThrowsInstanceOf(() => typedArray.set(source), err);
    }

    // Same as above, but with side-effect when executing ToNumber(Get(src, "0")).
    for (let {typedArray, buffer} of createTypedArrays()) {
        let source = {
            get 0() {
                return {
                    valueOf() {
                        throw new ExpectedError();
                    }
                };
            },
            length: {
                valueOf() {
                    detachArrayBuffer(buffer);
                    return 1;
                }
            }
        };
        let err = typedArray.length === 0 ? RangeError : ExpectedError;
        assertThrowsInstanceOf(() => typedArray.set(source), err);
    }

    // Side-effects when getting the source elements detach the buffer.
    for (let {typedArray, buffer} of createTypedArrays()) {
        let source = Object.defineProperties([], {
            0: {
                get() {
                    detachArrayBuffer(buffer);
                    return 1;
                }
            }
        });
        if (typedArray.length === 0) {
            assertThrowsInstanceOf(() => typedArray.set(source), RangeError);
        } else {
            typedArray.set(source);
        }
    }

    // Side-effects when getting the source elements detach the buffer. Also
    // ensure other elements are accessed.
    for (let {typedArray, buffer} of createTypedArrays()) {
        let accessed = false;
        let source = Object.defineProperties([], {
            0: {
                get() {
                    detachArrayBuffer(buffer);
                    return 1;
                }
            },
            1: {
                get() {
                    assertEq(accessed, false);
                    accessed = true;
                    return 2;
                }
            }
        });
        if (typedArray.length <= 1) {
            assertThrowsInstanceOf(() => typedArray.set(source), RangeError);
        } else {
            assertEq(accessed, false);
            typedArray.set(source);
            assertEq(accessed, true);
        }
    }

    // Side-effects when converting the source elements detach the buffer.
    for (let {typedArray, buffer} of createTypedArrays()) {
        let source = [{
            valueOf() {
                detachArrayBuffer(buffer);
                return 1;
            }
        }];
        if (typedArray.length === 0) {
            assertThrowsInstanceOf(() => typedArray.set(source), RangeError);
        } else {
            typedArray.set(source);
        }
    }

    // Side-effects when converting the source elements detach the buffer. Also
    // ensure other elements are accessed.
    for (let {typedArray, buffer} of createTypedArrays()) {
        let accessed = false;
        let source = [{
            valueOf() {
                detachArrayBuffer(buffer);
                return 1;
            }
        }, {
            valueOf() {
                assertEq(accessed, false);
                accessed = true;
                return 2;
            }
        }];
        if (typedArray.length <= 1) {
            assertThrowsInstanceOf(() => typedArray.set(source), RangeError);
        } else {
            assertEq(accessed, false);
            typedArray.set(source);
            assertEq(accessed, true);
        }
    }
}

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