summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma_6/Class/superPropProxies.js
blob: 3ec43b279cc93dee71b1bfc4f9979375561dd713 (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
class base {
    constructor() { }
}

let midStaticHandler = { };

// We shouldn't use the |this.called| strategy here, since we have proxies
// snooping property accesses.
let getterCalled, setterCalled;

class mid extends new Proxy(base, midStaticHandler) {
    constructor() { super(); }
    testSuperInProxy() {
        super.prop = "looking";
        assertEq(setterCalled, true);
        assertEq(super.prop, "found");
        assertEq(getterCalled, true);
    }
}

class child extends mid {
    constructor() { super(); }
    static testStaticLookups() {
        // This funtion is called more than once.
        this.called = false;
        super.prop;
        assertEq(this.called, true);
    }
}

let midInstance = new mid();

// Make sure proxies are searched properly on the prototype chain
let baseHandler = {
    get(target, p, receiver) {
        assertEq(receiver, midInstance);
        getterCalled = true;
        return "found";
    },

    set(t,p,val,receiver) {
        assertEq(receiver, midInstance);
        assertEq(val, "looking");
        setterCalled = true;
        return true;
    }
}
Object.setPrototypeOf(base.prototype, new Proxy(Object.prototype, baseHandler));

// make sure subclasses are not searched on static or super lookups.
let childHandler = {
    get() { throw "NO!"; },
    set() { throw "NO!"; }
}
Object.setPrototypeOf(child.prototype, new Proxy(mid.prototype, childHandler));

midInstance.testSuperInProxy();

// Don't do this earlier to avoid the lookup of .prototype during class creation
function midGet(target, p, receiver) {
    assertEq(receiver, child);
    receiver.called = true;
}
midStaticHandler.get = midGet;

child.testStaticLookups();

// Hey does super work in a proxy?
assertEq(new Proxy(({ method() { return super.hasOwnProperty("method"); } }), {}).method(), true);

// What about a CCW?
var g = newGlobal();
var wrappedSuper = g.eval("({ method() { return super.hasOwnProperty('method'); } })");
assertEq(wrappedSuper.method(), true);

// With a CCW on the proto chain?
var wrappedBase = g.eval("({ method() { return this.__secretProp__; } })");
var unwrappedDerived = { __secretProp__: 42, method() { return super.method(); } }
Object.setPrototypeOf(unwrappedDerived, wrappedBase);
assertEq(unwrappedDerived.method(), 42);

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