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

try {
    let p = new Proxy({}, { preventExtensions: null });
    assert(Object.preventExtensions(p) === p);
    p = new Proxy({}, { preventExtensions: undefined });
    assert(Object.preventExtensions(p) === p);
    p = new Proxy({}, {});
    assert(Object.preventExtensions(p) == p);

    let o = {};
    p = new Proxy(o, {
        preventExtensions(target) {
            assert(target === o);
            return true;
        }
    });

    Object.preventExtensions(o);
    Object.preventExtensions(p);

    // Invariants

    p = new Proxy({}, {
        preventExtensions() {
            return false;
        },
    });
    assertThrowsError(() => {
        Object.preventExtensions(p);
    }, {
        error: TypeError,
        message: "Proxy preventExtensions handler returned false",
    });

    o = {};
    p = new Proxy(o, {
        preventExtensions() {
            return true;
        },
    });
    assertThrowsError(() => {
        Object.preventExtensions(p);
    }, {
        error: TypeError,
        message: "Proxy handler's preventExtensions trap violates invariant: cannot return true if the target object is extensible"
    });

    Object.preventExtensions(o);
    assert(Object.preventExtensions(p) === p);

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