summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/class/innerBinding.js
blob: b1afa177e01ddd2b45216d3f109c99fadadb93fc (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
// Named class definitions should create an immutable inner binding.
// Since all code in classes is in strict mode, attempts to mutate it
// should throw.
class Foof { constructor() { }; tryBreak() { Foof = 4; } }
for (let result of [Foof, class Bar { constructor() { }; tryBreak() { Bar = 4; } }])
    assertThrowsInstanceOf(() => new result().tryBreak(), TypeError);

{
    class foo { constructor() { }; tryBreak() { foo = 4; } }
    for (let result of [foo, class Bar { constructor() { }; tryBreak() { Bar = 4 } }])
        assertThrowsInstanceOf(() => new result().tryBreak(), TypeError);
}

// TDZ applies to inner bindings
assertThrowsInstanceOf(()=>eval(`class Bar {
                                    constructor() { };
                                    [Bar] () { };
                                 }`), ReferenceError);

assertThrowsInstanceOf(()=>eval(`(class Bar {
                                    constructor() { };
                                    [Bar] () { };
                                 })`), ReferenceError);

// There's no magic "inner binding" global
{
    class Foo {
        constructor() { };
        test() {
            class Bar {
                constructor() { }
                test() { return Foo === Bar }
            }
            return new Bar().test();
        }
    }
    assertEq(new Foo().test(), false);
    assertEq(new class foo {
        constructor() { };
        test() {
            return new class bar {
                constructor() { }
                test() { return foo === bar }
            }().test();
        }
    }().test(), false);
}

// Inner bindings are shadowable
{
    class Foo {
        constructor() { }
        test(Foo) { return Foo; }
    }
    assertEq(new Foo().test(4), 4);
    assertEq(new class foo {
        constructor() { };
        test(foo) { return foo }
    }().test(4), 4);
}

// Inner bindings in expressions should shadow even existing names.
class Foo { constructor() { } static method() { throw new Error("NO!"); } }
assertEq(new class Foo {
            constructor() { };
            static method() { return 4; };
            test() { return Foo.method(); }
         }().test(), 4);

// The outer binding is distinct from the inner one
{
    let orig_X;

    class X {
        constructor() { }
        f() { assertEq(X, orig_X); }
    }

    orig_X = X;
    X = 13;
    assertEq(X, 13);
    new orig_X().f();
}

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