summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/basic/megamorphic-setelem-plain.js
blob: 0a60a116e8ad509f33f25d167c35a9da06cf430b (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
setJitCompilerOption("ic.force-megamorphic", 1);

// The megamorphic SetElem.
function doSet(obj, prop, v) {
    "use strict";
    obj[prop] = v;
}
function test() {
    with ({}) {} // No inlining.
    for (var i = 0; i < 10; i++) {
        var obj = {};

        // Test simple add/set cases.
        for (var j = 0; j < 10; j++) {
            doSet(obj, "x" + (j % 8), j);
        }

        // Ensure __proto__ is handled correctly.
        var setterCalls = 0;
        var proto = {set someSetter(v) { setterCalls++; }};
        doSet(obj, "__proto__", proto);
        assertEq(Object.getPrototypeOf(obj), proto);

        // Can't shadow a non-writable data property.
        Object.defineProperty(proto, "readonly",
                              {value: 1, writable: false, configurable: true});
        var ex = null;
        try {
            doSet(obj, "readonly", 2);
        } catch (e) {
            ex = e;
        }
        assertEq(ex instanceof TypeError, true);
        assertEq(obj.readonly, 1);

        // Setter on the proto chain must be called.
        doSet(obj, "someSetter", 1);
        assertEq(setterCalls, 1);

        // Can't add properties if non-extensible.
        Object.preventExtensions(obj);
        ex = null;
        try {
            doSet(obj, "foo", 1);
        } catch (e) {
            ex = e;
        }
        assertEq(ex instanceof TypeError, true);

        assertEq(JSON.stringify(obj), '{"x0":8,"x1":9,"x2":2,"x3":3,"x4":4,"x5":5,"x6":6,"x7":7}');
    }
}
test();