summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/expressions/ToPropertyKey-symbols.js
blob: 32b3994b3c766a66d32611064f99cf5ec1aff5dc (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
var symbols = [
    Symbol(), Symbol("iterator"), Symbol.for("iterator"), Symbol.iterator
];

for (var sym of symbols) {
    var key = {
        toString() { return sym; }
    };

    // Test that ToPropertyKey can return a symbol in each of the following
    // contexts.

    // Computed property names.
    var obj = {[key]: 13};
    var found = Reflect.ownKeys(obj);
    assertEq(found.length, 1);
    assertEq(found[0], sym);

    // Computed accessor property names.
    var obj2 = {
        get [key]() { return "got"; },
        set [key](v) { this.v = v; }
    };
    assertEq(obj2[sym], "got");
    obj2[sym] = 33;
    assertEq(obj2.v, 33);

    // Getting and setting properties.
    assertEq(obj[key], 13);
    obj[key] = 19;
    assertEq(obj[sym], 19);
    (function () { "use strict"; obj[key] = 20; })();
    assertEq(obj[sym], 20);
    obj[key]++;
    assertEq(obj[sym], 21);

    // Getting properties of primitive values.
    Number.prototype[sym] = "success";
    assertEq(Math.PI[key], "success");
    delete Number.prototype[sym];

    // Getting a super property.
    class X {
        [sym]() { return "X"; }
    }
    class Y extends X {
        [sym]() { return super[key]() + "Y"; }
    }
    var y = new Y();
    assertEq(y[sym](), "XY");

    // Setting a super property.
    class Z {
        set [sym](v) {
            this.self = this;
            this.value = v;
        }
    }
    class W extends Z {
        set [sym](v) {
            this.isW = true;
            super[key] = v;
        }
    }
    var w = new W();
    w[key] = "ok";
    assertEq(w.self, w);
    assertEq(w.value, "ok");
    assertEq(w.isW, true);

    // Deleting properties.
    obj = {[sym]: 1};
    assertEq(delete obj[key], true);
    assertEq(sym in obj, false);

    // LHS of `in` expressions.
    assertEq(key in {iterator: 0}, false);
    assertEq(key in {[sym]: 0}, true);

    // Methods of Object and Object.prototype
    obj = {};
    Object.defineProperty(obj, key, {value: "ok", enumerable: true});
    assertEq(obj[sym], "ok");
    assertEq(obj.hasOwnProperty(key), true);
    assertEq(obj.propertyIsEnumerable(key), true);
    var desc = Object.getOwnPropertyDescriptor(obj, key);
    assertEq(desc.value, "ok");
}

reportCompare(0, 0);