summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Array/change-array-by-copy-errors-from-correct-realm.js
blob: 4e99cbe0ffffa75882f5e96c9da8828cb0e9b6eb (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
// |reftest| shell-option(--enable-change-array-by-copy) skip-if(!Array.prototype.with)

function test(otherGlobal) {
    assertEq(TypeError !== otherGlobal.TypeError, true);
    assertEq(Object.getPrototypeOf(TypeError) !== Object.getPrototypeOf(otherGlobal.TypeError), true);
    assertEq(RangeError !== otherGlobal.RangeError, true);
    assertEq(Object.getPrototypeOf(RangeError) !== Object.getPrototypeOf(otherGlobal.RangeError), true);

    
    var arrayLike = {
        get "0"() {
            throw new Error("Get 0");
        },
        get "4294967295" () { // 2 ** 32 - 1
            throw new Error("Get 2147483648");
        },
        get "4294967296" () { // 2 ** 32
            throw new Error("Get 2147483648");
        },
        length: 2 ** 32
    };

    let gToSorted = otherGlobal.Array.prototype.toSorted;
    let gToSpliced = otherGlobal.Array.prototype.toSpliced;
    let gToReversed = otherGlobal.Array.prototype.toReversed;
    let gWith = otherGlobal.Array.prototype.with;

    let typeErrorCalls = [
        ["toSorted - bad comparator", () => gToSorted.call([], 5)],
        ["toSorted - this is null", () => gToSorted.call(null)],
        ["toSpliced - array too long", () => {
            var oldLen = arrayLike.length;
            arrayLike.length = 2**53 - 1;
            gToSpliced.call(arrayLike, 0, 0, 1);
            arrayLike.length = oldLen;
        }]
    ]

    let rangeErrorCalls = [
        ["toSorted - array too long", () => gToSorted.call(arrayLike)],
        ["toReversed - array too long", () => gToReversed.call(arrayLike)],
        ["toSpliced - adding elements would exceed max array length", () => gToSpliced.call(arrayLike, 0, 0)],
        ["with - index out of range", () => gWith.call([0, 1, 2], 3, 7)],
        ["with - negative index", () => gWith.call([0, 1, 2], -4, 7)],
        ["with - array too long", () => gWith.call(arrayLike, 0, 0)]
    ]

    // For each erroneous case, make sure the error comes from
    // the other realm (not this realm)
    for (const [message, f] of typeErrorCalls) {
        try {
            f();
        } catch (exc) {
            assertEq(exc instanceof TypeError, false, message + " threw TypeError from wrong realm");
            assertEq(exc instanceof otherGlobal.TypeError, true, message + " didn't throw TypeError from other realm");
            assertEq(Object.getPrototypeOf(exc) !== Object.getPrototypeOf(TypeError), true,
                     message + " TypeError has wrong prototype");
        }
    }

    for (const [message, f] of rangeErrorCalls) {
        try {
            f();
        } catch (exc) {
            assertEq(exc instanceof RangeError, false, message + " threw RangeError from wrong realm");
            assertEq(exc instanceof otherGlobal.RangeError, true, message + " didn't throw RangeError from other realm");
            assertEq(Object.getPrototypeOf(exc) !== Object.getPrototypeOf(RangeError), true,
                     message + " TypeError has wrong prototype");
        }
    }
}

test(newGlobal());
test(newGlobal({newCompartment: true}));

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