summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/set-toobject.js
blob: 360c5dde7ebeb542681216d12c7cc5281f45fefd (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
// Test ToObject in %TypedArray%.prototype.set(array|typedArray, offset).

let ta = new Int32Array(4);

for (let nullOrUndefined of [null, undefined]) {
    // ToObject(array) throws a TypeError when |array| is null or undefined.
    assertThrowsInstanceOf(() => ta.set(nullOrUndefined), TypeError);

    // ToInteger(offset) is called before ToObject(array).
    class ExpectedError extends Error {}
    assertThrowsInstanceOf(() => ta.set(nullOrUndefined, {
        valueOf() {
            throw new ExpectedError();
        }
    }), ExpectedError);
}

// Ensure ta is still initialized with zeros.
assertEqArray(ta, [0, 0, 0, 0]);

// %TypedArray%.prototype.set can be called with a string primitive values.
ta.set("");
assertEqArray(ta, [0, 0, 0, 0]);

ta.set("123");
assertEqArray(ta, [1, 2, 3, 0]);

// Throws a RangeError if the length is too large.
assertThrowsInstanceOf(() => ta.set("456789"), RangeError);
assertEqArray(ta, [1, 2, 3, 0]);

// When called with other primitive values the typed array contents don't
// change since ToObject(<primitive>).length is zero, i.e. the source object is
// treated the same as an empty array.
for (let value of [true, false, 0, NaN, 123, Infinity, Symbol()]) {
    ta.set(value);
    assertEqArray(ta, [1, 2, 3, 0]);
}

// Repeat test from above when the primitive wrapper prototype has been changed
// to include "length" and an indexed property.
Number.prototype.length = 4;
Number.prototype[3] = -1;
try {
    ta.set(456);
    assertEqArray(ta, [0, 0, 0, -1]);
} finally {
    delete Number.prototype.length;
    delete Number.prototype[3];
}

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