summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/class/methodInstallation.js
blob: bbb6c2f72746bc55c81c7db8174c5eba9e85f8ad (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
93
94
95
96
97
98
99
100
101
// Do the things we write in classes actually appear as they are supposed to?

var methodCalled;
var getterCalled;
var setterCalled;
var constructorCalled;
var staticMethodCalled;
var staticGetterCalled;
var staticSetterCalled;
class testClass {
    constructor() { constructorCalled = true; }
    __proto__() { methodCalled = true }
    get getter() { getterCalled = true; }
    set setter(x) { setterCalled = true; }
    static staticMethod() { staticMethodCalled = true; }
    static get staticGetter() { staticGetterCalled = true; }
    static set staticSetter(x) { staticSetterCalled = true; }
    *[Symbol.iterator]() { yield "cow"; yield "pig"; }
}

for (let a of [testClass,
                class {
                    constructor() { constructorCalled = true; }
                    __proto__() { methodCalled = true }
                    get getter() { getterCalled = true; }
                    set setter(x) { setterCalled = true; }
                    static staticMethod() { staticMethodCalled = true; }
                    static get staticGetter() { staticGetterCalled = true; }
                    static set staticSetter(x) { staticSetterCalled = true; }
                    *[Symbol.iterator]() { yield "cow"; yield "pig"; }
                }]) {

    methodCalled = false;
    getterCalled = false;
    setterCalled = false;
    constructorCalled = false;
    staticMethodCalled = false;
    staticGetterCalled = false;
    staticSetterCalled = false;

    var aConstDesc = Object.getOwnPropertyDescriptor(a.prototype, "constructor");
    assertEq(aConstDesc.writable, true);
    assertEq(aConstDesc.configurable, true);
    assertEq(aConstDesc.enumerable, false);
    new aConstDesc.value();
    assertEq(constructorCalled, true);

    // __proto__ is just an identifier for classes. No prototype changes are made.
    assertEq(Object.getPrototypeOf(a.prototype), Object.prototype);
    var aMethDesc = Object.getOwnPropertyDescriptor(a.prototype, "__proto__");
    assertEq(aMethDesc.writable, true);
    assertEq(aMethDesc.configurable, true);
    assertEq(aMethDesc.enumerable, false);
    aMethDesc.value();
    assertEq(methodCalled, true);

    var aGetDesc = Object.getOwnPropertyDescriptor(a.prototype, "getter");
    assertEq(aGetDesc.configurable, true);
    assertEq(aGetDesc.enumerable, false);
    aGetDesc.get();
    assertThrowsInstanceOf(() => new aGetDesc.get, TypeError);
    assertEq(getterCalled, true);

    var aSetDesc = Object.getOwnPropertyDescriptor(a.prototype, "setter");
    assertEq(aSetDesc.configurable, true);
    assertEq(aSetDesc.enumerable, false);
    aSetDesc.set();
    assertThrowsInstanceOf(() => new aSetDesc.set, TypeError);
    assertEq(setterCalled, true);
    assertDeepEq(aSetDesc, Object.getOwnPropertyDescriptor(a.prototype, "setter"));

    assertEq(Object.getOwnPropertyDescriptor(new a(), "staticMethod"), undefined);
    var aStaticMethDesc = Object.getOwnPropertyDescriptor(a, "staticMethod");
    assertEq(aStaticMethDesc.configurable, true);
    assertEq(aStaticMethDesc.enumerable, false);
    assertEq(aStaticMethDesc.writable, true);
    aStaticMethDesc.value();
    assertThrowsInstanceOf(() => new aStaticMethDesc.value, TypeError);
    assertEq(staticMethodCalled, true);

    assertEq(Object.getOwnPropertyDescriptor(new a(), "staticGetter"), undefined);
    var aStaticGetDesc = Object.getOwnPropertyDescriptor(a, "staticGetter");
    assertEq(aStaticGetDesc.configurable, true);
    assertEq(aStaticGetDesc.enumerable, false);
    aStaticGetDesc.get();
    assertThrowsInstanceOf(() => new aStaticGetDesc.get, TypeError);
    assertEq(staticGetterCalled, true);

    assertEq(Object.getOwnPropertyDescriptor(new a(), "staticSetter"), undefined);
    var aStaticSetDesc = Object.getOwnPropertyDescriptor(a, "staticSetter");
    assertEq(aStaticSetDesc.configurable, true);
    assertEq(aStaticSetDesc.enumerable, false);
    aStaticSetDesc.set();
    assertThrowsInstanceOf(() => new aStaticSetDesc.set, TypeError);
    assertEq(staticSetterCalled, true);

    assertEq([...new a()].join(), "cow,pig");
}

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