1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/* Test object literals. */
16
17var local = 0;
18
19function f(x)
20{
21  return x + "et";
22}
23
24var o = {
25  a: 5,
26  [
27    "pr" +
28    "op"]  : 6,
29
30  [f
31   ("g")
32   ]
33   : 7,
34
35  [f(
36   "s"
37   )  ]: 8,
38
39  get    [f
40    ("res")
41    ]
42    () { return 9 },
43
44  set
45  [f("res")](
46   value) { local = value },
47};
48
49assert(o.a === 5);
50assert(o.prop === 6);
51assert(o.get === 7);
52assert(o.set === 8);
53
54local = 0;
55o.reset = 10;
56assert(local === 10);
57assert(o.reset === 9);
58
59/* Test classes. */
60
61function fxy() {
62  return "xy";
63}
64
65class C {
66  [fxy()] () {
67    return 6;
68  }
69
70  static [fxy()]() {
71    return 7;
72  }
73
74  get ["a" + 1]() {
75    return 8;
76  }
77
78  set ["a" + 1](x) {
79    local = x;
80  }
81
82  static get ["a" + 1]() {
83    return 10;
84  }
85
86  static set ["a" + 1](x) {
87    local = x;
88  }
89};
90
91var c = new C;
92assert(c.xy() === 6);
93assert(C.xy() === 7);
94
95local = 0;
96c.a1 = 9;
97assert(local === 9);
98assert(c.a1 === 8);
99
100local = 0;
101C.a1 = 11;
102assert(local === 11);
103assert(C.a1 === 10);
104
105class D {
106  [(() => "const" + "ructor")()] (arg) {
107    this.a = arg;
108  }
109}
110
111var d = new D;
112assert(d.a === undefined);
113d.constructor(7);
114assert(d.a === 7);
115
116class E {
117  get ["_constructor_".substring(1,12)]() {
118    return this.a;
119  }
120}
121
122var e = new E;
123assert(e.constructor === undefined);
124e.a = 8;
125assert(e.constructor === 8);
126
127function throw_error(snippet)
128{
129  try {
130    eval(snippet);
131    assert(false);
132  } catch (e) {
133    assert(e instanceof TypeError);
134  }
135}
136
137throw_error("new class { static ['proto' + 'type'] () {} }");
138throw_error("new class { static get ['proto' + 'type'] () {} }");
139throw_error("new class { static set ['proto' + 'type'] (x) {} }");
140