• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16function must_throw(str) {
17  try {
18    eval("switch (1) { default: " + str + "}");
19    assert(false);
20  } catch (e) {
21  }
22
23  try {
24    eval(str);
25    assert(false);
26  }
27  catch (e) {
28  }
29
30  must_throw_strict(str);
31}
32
33function must_throw_strict(str) {
34  try {
35    eval ("'use strict'; switch (1) { default: " + str + "}");
36    assert (false);
37  } catch (e) {
38  }
39
40  try {
41    eval("'use strict'; " + str);
42    assert(false);
43  } catch (e) {
44  }
45}
46
47must_throw("class {}");
48must_throw("class class {}");
49must_throw("class A { constructor() {} this.a = 5 }");
50must_throw("class A { constructor() {} constructor() {} }");
51must_throw("class A { static prototype() {} }");
52must_throw("class A { get constructor() {} }");
53must_throw("class A { set constructor() {} }");
54must_throw("class A {}; A()");
55must_throw("class X {}; var o = {}; Object.defineProperty(o, 'p', { get: X, set: X }); o.p;");
56must_throw("var a = new A; class A {};");
57must_throw("class A { g\\u0065t e() {} }");
58must_throw('class A { "static" e() {} }');
59must_throw('class A { *constructor() {} }');
60
61assert(eval("class A {}") === undefined);
62assert(eval("var a = class A {}") === undefined);
63assert(eval("var a = class {}") === undefined);
64assert(eval("class A { ; ; ; ;;;;;;;;;;;; ; ; ;;;;;;;;;;;;;;;;;;;;;;;;; }") === undefined);
65assert(eval('class A {"constructor"() {} }') === undefined);
66assert(isNaN (eval('switch(1) { default: (class A{} % 1) }')));
67
68class A1 {
69  ["constructor"]() {
70    return 5;
71  }
72}
73
74assert ((new A1).constructor() === 5);
75
76class A2 {
77  *["constructor"]() {
78    yield 5;
79  }
80}
81
82assert ((new A2).constructor().next().value === 5);
83
84class B {
85}
86
87var b = new B;
88assert (typeof B  === "function");
89assert (typeof b === "object");
90assert (b.constructor === B);
91
92class C {
93  c1() {
94    return 5;
95  }
96
97  c2() {
98    return this._c;
99  }
100  3() {
101    return 3;
102  }
103
104  super() {
105    return 42;
106  }
107  return() {
108    return 43;
109  }
110
111  static *constructor() {
112    return 44;
113  }
114}
115
116var c = new C;
117assert (c.c1() === 5);
118assert (c.c2() === undefined);
119assert (c["3"]() === 3);
120assert (c.super() === 42);
121assert (c.return() === 43);
122assert (c.constructor === C);
123assert (C.constructor().next().value === 44);
124
125class D {
126  constructor(d) {
127    this._d = d;
128  }
129
130  d1() {
131    return this._d;
132  }
133}
134var d = new D(5);
135assert (d.d1() === 5);
136assert (d.constructor === D);
137
138class E {
139  constructor(e) {
140    this._e = e;
141  }
142
143  get e() {
144    return this._e;
145  }
146
147  set e(e) {
148    this._e = e;
149  }
150
151  get () {
152    return 11;
153  }
154
155  set () {
156    return 12;
157  }
158}
159var e = new E (5);
160assert (e.e === 5);
161e.e = 10;
162assert (e.e === 10);
163assert (e.get() === 11);
164assert (e.set() === 12);
165assert (e.constructor === E);
166
167var F = class ClassF {
168  constructor(f) {
169    this._f = f;
170  }
171
172  static f1() {
173    return this;
174  }
175
176  static f2() {
177    return this._f;
178  }
179
180  static f3(a, b) {
181    return a + b;
182  }
183
184  static constructor(a) {
185    return a;
186  }
187
188  static static(a) {
189    return a;
190  }
191
192  static 2 (a) {
193    return 2 * a;
194  }
195
196  static function(a) {
197    return 3 * a;
198  }
199}
200
201var f = new F(5);
202
203assert (f.f1 === undefined);
204assert (f.f2 === undefined);
205assert (F.f1() === F);
206assert (F.f2() === undefined);
207assert (F.f3(1, 1) === 2);
208assert (F.constructor(5) === 5);
209assert (F.static(5) === 5);
210assert (F["2"](5) === 10);
211assert (F.function(5) === 15);
212assert (f.constructor === F);
213
214var G = class {
215  static set a(a) {
216    this._a = a;
217  }
218  static get a() {
219    return this._a;
220  }
221  static set 1(a) {
222    this._a = a;
223  }
224  static get 1() {
225    return this._a;
226  }
227
228  static get() {
229    return 11;
230  }
231
232  static set() {
233    return 12;
234  }
235
236  static set constructor(a) {
237    this._a = a;
238  }
239  static get constructor() {
240    return this._a;
241  }
242
243  static g1() {
244    return 5;
245  }
246
247  static g1() {
248    return 10;
249  }
250}
251
252G.a = 10;
253assert (G.a === 10);
254assert (G.g1() === 10);
255G["1"] = 20;
256assert (G["1"] === 20);
257assert (G.get() == 11);
258assert (G.set() == 12);
259G.constructor = 30;
260assert (G.constructor === 30);
261
262class H {
263  method() { assert (typeof H === 'function'); return H; }
264}
265
266let H_original = H;
267var H_method = H.prototype.method;
268C = undefined;
269assert(C === undefined);
270C = H_method();
271assert(C === H_original);
272
273var I = class C {
274  method() { assert(typeof C === 'function'); return C; }
275}
276
277let I_original = I;
278var I_method = I.prototype.method;
279I = undefined;
280assert(I === undefined);
281I = I_method();
282assert(I == I_original);
283
284var J_method;
285class J {
286  static [(J_method = eval('(function() { return J; })'), "X")]() {}
287}
288var J_original = J;
289J = 6;
290assert (J_method() == J_original);
291
292var K_method;
293class K {
294  constructor () {
295    K_method = function() { return K; }
296  }
297}
298var K_original = K;
299new K;
300K = 6;
301assert (K_method() == K_original);
302
303var L_method;
304class L extends (L_method = function() { return L; }) {
305}
306var L_original = L;
307L = 6;
308assert (L_method() == L_original);
309
310/* Test cleanup class environment */
311try {
312  class A {
313    [d]() {}
314  }
315  let d;
316  assert(false);
317} catch (e) {
318  assert(e instanceof ReferenceError);
319}
320
321try {
322  class A extends d {}
323  let d;
324  assert(false);
325} catch (e) {
326  assert(e instanceof ReferenceError);
327}
328try {
329  var a = 1 + 2 * 3 >> class A extends d {};
330  let d;
331  assert(false);
332} catch (e) {
333  assert(e instanceof ReferenceError);
334}
335