summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/Proxy.handler-construct.js
blob: c5f7bbf5b5d4e084b3ef36960457cdb53e2d7fde (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
load("test-common.js");

try {
    let p = new Proxy(function() { this.x = 5; }, { construct: null });
    assert((new p).x === 5);
    let p = new Proxy(function() { this.x = 5; }, { construct: undefined });
    assert((new p).x === 5);
    let p = new Proxy(function() { this.x = 5; }, {});
    assert((new p).x === 5);

    function f(value) {
        this.x = value;
    }

    let p;
    const handler = {
        construct(target, arguments, newTarget) {
            assert(target === f);
            assert(newTarget === p);
            if (arguments[1])
                return Reflect.construct(target, [arguments[0] * 2], newTarget);
            return Reflect.construct(target, arguments, newTarget);
        },
    };
    p = new Proxy(f, handler);

    assert(new p(15).x === 15);
    assert(new p(15, true).x === 30);

    let p;
    function theNewTarget() {};
    const handler = {
        construct(target, arguments, newTarget) {
            assert(target === f);
            assert(newTarget === theNewTarget);
            if (arguments[1])
                return Reflect.construct(target, [arguments[0] * 2], newTarget);
            return Reflect.construct(target, arguments, newTarget);
        },
    };
    p = new Proxy(f, handler);

    Reflect.construct(p, [15], theNewTarget);

    // Invariants
    [{}, [], new Proxy({}, {})].forEach(item => {
        assertThrowsError(() => {
            new (new Proxy(item, {}));
        }, {
            error: TypeError,
            message: "[object ProxyObject] is not a constructor",
        });
    });

    console.log("PASS");
} catch (e) {
    console.log("FAIL: " + e);
}