summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/object/defineGetter-defineSetter.js
blob: b2bb21f67c036f2f188be9e8e0810e25766cc417 (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
84
85
86
87
88
89
90
91
92
/*
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/
 */

let count = 0;
let verifyProxy = new Proxy({}, {
    defineProperty(target, property, descriptor) {
        assertEq(property, "x");

        assertEq(descriptor.enumerable, true);
        assertEq(descriptor.configurable, true);

        if ("set" in descriptor)
            assertEq(descriptor.set, Object.prototype.__defineSetter__);
        else
            assertEq(descriptor.get, Object.prototype.__defineGetter__);

        assertEq(Object.keys(descriptor).length, 3);

        count++;
        return true;
    }
});

for (let define of [Object.prototype.__defineGetter__, Object.prototype.__defineSetter__]) {
    // null/undefined |this| value
    for (let thisv of [undefined, null])
        assertThrowsInstanceOf(() => define.call(thisv, "x", define), TypeError);

    // non-callable getter/setter
    let nonCallable = [{}, [], new Proxy({}, {})];
    for (let value of nonCallable)
        assertThrowsInstanceOf(() => define.call(verifyProxy, "x", value), TypeError);

    // ToPropertyKey
    let key = {
        [Symbol.toPrimitive](hint) {
            assertEq(hint, "string");
            // Throws, because non-primitive is returned
            return {};
        },
        valueOf() { throw "wrongly invoked"; },
        toString() { throw "wrongly invoked"; }
    };
    assertThrowsInstanceOf(() => define.call(verifyProxy, key, define), TypeError);

    key = {
        [Symbol.toPrimitive](hint) {
            assertEq(hint, "string");
            return "x";
        },
        valueOf() { throw "wrongly invoked"; },
        toString() { throw "wrongly invoked"; }
    }
    define.call(verifyProxy, key, define);

    key = {
        [Symbol.toPrimitive]: undefined,

        valueOf() { throw "wrongly invoked"; },
        toString() { return "x"; }
    }
    define.call(verifyProxy, key, define);

    // Bog standard call
    define.call(verifyProxy, "x", define);

    let obj = {};
    define.call(obj, "x", define);
    let descriptor = Object.getOwnPropertyDescriptor(obj, "x");

    assertEq(descriptor.enumerable, true);
    assertEq(descriptor.configurable, true);

    if (define == Object.prototype.__defineSetter__) {
        assertEq(descriptor.get, undefined);
        assertEq(descriptor.set, define);
    } else {
        assertEq(descriptor.get, define);
        assertEq(descriptor.set, undefined);
    }

    assertEq(Object.keys(descriptor).length, 4);


}

// Number of calls that should succeed
assertEq(count, 6);

reportCompare(0, 0);