OLD | NEW |
| (Empty) |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Flags: --harmony-classes --harmony-sloppy | |
6 | |
7 (function TestBasics() { | |
8 var C = class C {} | |
9 assertEquals(typeof C, 'function'); | |
10 assertEquals(C.__proto__, Function.prototype); | |
11 assertEquals(Object.prototype, Object.getPrototypeOf(C.prototype)); | |
12 assertEquals(Function.prototype, Object.getPrototypeOf(C)); | |
13 assertEquals('C', C.name); | |
14 | |
15 class D {} | |
16 assertEquals(typeof D, 'function'); | |
17 assertEquals(D.__proto__, Function.prototype); | |
18 assertEquals(Object.prototype, Object.getPrototypeOf(D.prototype)); | |
19 assertEquals(Function.prototype, Object.getPrototypeOf(D)); | |
20 assertEquals('D', D.name); | |
21 | |
22 class D2 { constructor() {} } | |
23 assertEquals('D2', D2.name); | |
24 | |
25 // TODO(arv): The logic for the name of anonymous functions in ES6 requires | |
26 // the below to be 'E'; | |
27 var E = class {} | |
28 assertEquals('', E.name); // Should be 'E'. | |
29 | |
30 var F = class { constructor() {} }; | |
31 assertEquals('', F.name); // Should be 'F'. | |
32 })(); | |
33 | |
34 | |
35 (function TestBasicsExtends() { | |
36 class C extends null {} | |
37 assertEquals(typeof C, 'function'); | |
38 assertEquals(C.__proto__, Function.prototype); | |
39 assertEquals(null, Object.getPrototypeOf(C.prototype)); | |
40 | |
41 class D extends C {} | |
42 assertEquals(typeof D, 'function'); | |
43 assertEquals(D.__proto__, C); | |
44 assertEquals(C.prototype, Object.getPrototypeOf(D.prototype)); | |
45 })(); | |
46 | |
47 | |
48 (function TestSideEffectInExtends() { | |
49 var calls = 0; | |
50 class C {} | |
51 class D extends (calls++, C) {} | |
52 assertEquals(1, calls); | |
53 assertEquals(typeof D, 'function'); | |
54 assertEquals(D.__proto__, C); | |
55 assertEquals(C.prototype, Object.getPrototypeOf(D.prototype)); | |
56 })(); | |
57 | |
58 | |
59 (function TestInvalidExtends() { | |
60 assertThrows(function() { | |
61 class C extends 42 {} | |
62 }, TypeError); | |
63 | |
64 assertThrows(function() { | |
65 // Function but its .prototype is not null or a function. | |
66 class C extends Math.abs {} | |
67 }, TypeError); | |
68 | |
69 assertThrows(function() { | |
70 Math.abs.prototype = 42; | |
71 class C extends Math.abs {} | |
72 }, TypeError); | |
73 delete Math.abs.prototype; | |
74 | |
75 assertThrows(function() { | |
76 function* g() {} | |
77 class C extends g {} | |
78 }, TypeError); | |
79 })(); | |
80 | |
81 | |
82 (function TestConstructorProperty() { | |
83 class C {} | |
84 assertEquals(C, C.prototype.constructor); | |
85 var descr = Object.getOwnPropertyDescriptor(C.prototype, 'constructor'); | |
86 assertTrue(descr.configurable); | |
87 assertFalse(descr.enumerable); | |
88 assertTrue(descr.writable); | |
89 })(); | |
90 | |
91 | |
92 (function TestPrototypeProperty() { | |
93 class C {} | |
94 var descr = Object.getOwnPropertyDescriptor(C, 'prototype'); | |
95 assertFalse(descr.configurable); | |
96 assertFalse(descr.enumerable); | |
97 assertFalse(descr.writable); | |
98 })(); | |
99 | |
100 | |
101 (function TestConstructor() { | |
102 var count = 0; | |
103 class C { | |
104 constructor() { | |
105 assertEquals(Object.getPrototypeOf(this), C.prototype); | |
106 count++; | |
107 } | |
108 } | |
109 assertEquals(C, C.prototype.constructor); | |
110 var descr = Object.getOwnPropertyDescriptor(C.prototype, 'constructor'); | |
111 assertTrue(descr.configurable); | |
112 assertFalse(descr.enumerable); | |
113 assertTrue(descr.writable); | |
114 | |
115 var c = new C(); | |
116 assertEquals(1, count); | |
117 assertEquals(Object.getPrototypeOf(c), C.prototype); | |
118 })(); | |
119 | |
120 | |
121 (function TestImplicitConstructor() { | |
122 class C {} | |
123 var c = new C(); | |
124 assertEquals(Object.getPrototypeOf(c), C.prototype); | |
125 })(); | |
126 | |
127 | |
128 (function TestConstructorStrict() { | |
129 class C { | |
130 constructor() { | |
131 assertThrows(function() { | |
132 nonExistingBinding = 42; | |
133 }, ReferenceError); | |
134 } | |
135 } | |
136 new C(); | |
137 })(); | |
138 | |
139 | |
140 (function TestSuperInConstructor() { | |
141 var calls = 0; | |
142 class B {} | |
143 B.prototype.x = 42; | |
144 | |
145 class C extends B { | |
146 constructor() { | |
147 super(); | |
148 calls++; | |
149 assertEquals(42, super.x); | |
150 } | |
151 } | |
152 | |
153 new C; | |
154 assertEquals(1, calls); | |
155 })(); | |
156 | |
157 | |
158 (function TestStrictMode() { | |
159 class C {} | |
160 | |
161 with ({a: 1}) { | |
162 assertEquals(1, a); | |
163 } | |
164 | |
165 assertThrows('class C extends function B() { with ({}); return B; }() {}', | |
166 SyntaxError); | |
167 | |
168 var D = class extends function() { | |
169 arguments.caller; | |
170 } {}; | |
171 assertThrows(function() { | |
172 Object.getPrototypeOf(D).arguments; | |
173 }, TypeError); | |
174 assertThrows(function() { | |
175 new D; | |
176 }, TypeError); | |
177 })(); | |
178 | |
179 | |
180 (function TestToString() { | |
181 class C {} | |
182 assertEquals('class C {}', C.toString()); | |
183 | |
184 class D { constructor() { 42; } } | |
185 assertEquals('class D { constructor() { 42; } }', D.toString()); | |
186 | |
187 class E { x() { 42; } } | |
188 assertEquals('class E { x() { 42; } }', E.toString()); | |
189 })(); | |
190 | |
191 | |
192 function assertMethodDescriptor(object, name) { | |
193 var descr = Object.getOwnPropertyDescriptor(object, name); | |
194 assertTrue(descr.configurable); | |
195 assertFalse(descr.enumerable); | |
196 assertTrue(descr.writable); | |
197 assertEquals('function', typeof descr.value); | |
198 assertFalse('prototype' in descr.value); | |
199 } | |
200 | |
201 | |
202 function assertGetterDescriptor(object, name) { | |
203 var descr = Object.getOwnPropertyDescriptor(object, name); | |
204 assertTrue(descr.configurable); | |
205 assertFalse(descr.enumerable); | |
206 assertEquals('function', typeof descr.get); | |
207 assertFalse('prototype' in descr.get); | |
208 assertEquals(undefined, descr.set); | |
209 } | |
210 | |
211 | |
212 function assertSetterDescriptor(object, name) { | |
213 var descr = Object.getOwnPropertyDescriptor(object, name); | |
214 assertTrue(descr.configurable); | |
215 assertFalse(descr.enumerable); | |
216 assertEquals(undefined, descr.get); | |
217 assertEquals('function', typeof descr.set); | |
218 assertFalse('prototype' in descr.set); | |
219 } | |
220 | |
221 | |
222 function assertAccessorDescriptor(object, name) { | |
223 var descr = Object.getOwnPropertyDescriptor(object, name); | |
224 assertTrue(descr.configurable); | |
225 assertFalse(descr.enumerable); | |
226 assertEquals('function', typeof descr.get); | |
227 assertEquals('function', typeof descr.set); | |
228 assertFalse('prototype' in descr.get); | |
229 assertFalse('prototype' in descr.set); | |
230 } | |
231 | |
232 | |
233 (function TestMethods() { | |
234 class C { | |
235 method() { return 1; } | |
236 static staticMethod() { return 2; } | |
237 method2() { return 3; } | |
238 static staticMethod2() { return 4; } | |
239 } | |
240 | |
241 assertMethodDescriptor(C.prototype, 'method'); | |
242 assertMethodDescriptor(C.prototype, 'method2'); | |
243 assertMethodDescriptor(C, 'staticMethod'); | |
244 assertMethodDescriptor(C, 'staticMethod2'); | |
245 | |
246 assertEquals(1, new C().method()); | |
247 assertEquals(2, C.staticMethod()); | |
248 assertEquals(3, new C().method2()); | |
249 assertEquals(4, C.staticMethod2()); | |
250 })(); | |
251 | |
252 | |
253 (function TestGetters() { | |
254 class C { | |
255 get x() { return 1; } | |
256 static get staticX() { return 2; } | |
257 get y() { return 3; } | |
258 static get staticY() { return 4; } | |
259 } | |
260 | |
261 assertGetterDescriptor(C.prototype, 'x'); | |
262 assertGetterDescriptor(C.prototype, 'y'); | |
263 assertGetterDescriptor(C, 'staticX'); | |
264 assertGetterDescriptor(C, 'staticY'); | |
265 | |
266 assertEquals(1, new C().x); | |
267 assertEquals(2, C.staticX); | |
268 assertEquals(3, new C().y); | |
269 assertEquals(4, C.staticY); | |
270 })(); | |
271 | |
272 | |
273 | |
274 (function TestSetters() { | |
275 var x, staticX, y, staticY; | |
276 class C { | |
277 set x(v) { x = v; } | |
278 static set staticX(v) { staticX = v; } | |
279 set y(v) { y = v; } | |
280 static set staticY(v) { staticY = v; } | |
281 } | |
282 | |
283 assertSetterDescriptor(C.prototype, 'x'); | |
284 assertSetterDescriptor(C.prototype, 'y'); | |
285 assertSetterDescriptor(C, 'staticX'); | |
286 assertSetterDescriptor(C, 'staticY'); | |
287 | |
288 assertEquals(1, new C().x = 1); | |
289 assertEquals(1, x); | |
290 assertEquals(2, C.staticX = 2); | |
291 assertEquals(2, staticX); | |
292 assertEquals(3, new C().y = 3); | |
293 assertEquals(3, y); | |
294 assertEquals(4, C.staticY = 4); | |
295 assertEquals(4, staticY); | |
296 })(); | |
297 | |
298 | |
299 (function TestSideEffectsInPropertyDefine() { | |
300 function B() {} | |
301 B.prototype = { | |
302 constructor: B, | |
303 set m(v) { | |
304 throw Error(); | |
305 } | |
306 }; | |
307 | |
308 class C extends B { | |
309 m() { return 1; } | |
310 } | |
311 | |
312 assertEquals(1, new C().m()); | |
313 })(); | |
314 | |
315 | |
316 (function TestAccessors() { | |
317 class C { | |
318 constructor(x) { | |
319 this._x = x; | |
320 } | |
321 | |
322 get x() { return this._x; } | |
323 set x(v) { this._x = v; } | |
324 | |
325 static get staticX() { return this._x; } | |
326 static set staticX(v) { this._x = v; } | |
327 } | |
328 | |
329 assertAccessorDescriptor(C.prototype, 'x'); | |
330 assertAccessorDescriptor(C, 'staticX'); | |
331 | |
332 var c = new C(1); | |
333 c._x = 1; | |
334 assertEquals(1, c.x); | |
335 c.x = 2; | |
336 assertEquals(2, c._x); | |
337 | |
338 C._x = 3; | |
339 assertEquals(3, C.staticX); | |
340 C._x = 4; | |
341 assertEquals(4, C.staticX ); | |
342 })(); | |
343 | |
344 | |
345 (function TestProto() { | |
346 class C { | |
347 __proto__() { return 1; } | |
348 } | |
349 assertMethodDescriptor(C.prototype, '__proto__'); | |
350 assertEquals(1, new C().__proto__()); | |
351 })(); | |
352 | |
353 | |
354 (function TestProtoStatic() { | |
355 class C { | |
356 static __proto__() { return 1; } | |
357 } | |
358 assertMethodDescriptor(C, '__proto__'); | |
359 assertEquals(1, C.__proto__()); | |
360 })(); | |
361 | |
362 | |
363 (function TestProtoAccessor() { | |
364 class C { | |
365 get __proto__() { return this._p; } | |
366 set __proto__(v) { this._p = v; } | |
367 } | |
368 assertAccessorDescriptor(C.prototype, '__proto__'); | |
369 var c = new C(); | |
370 c._p = 1; | |
371 assertEquals(1, c.__proto__); | |
372 c.__proto__ = 2; | |
373 assertEquals(2, c.__proto__); | |
374 })(); | |
375 | |
376 | |
377 (function TestStaticProtoAccessor() { | |
378 class C { | |
379 static get __proto__() { return this._p; } | |
380 static set __proto__(v) { this._p = v; } | |
381 } | |
382 assertAccessorDescriptor(C, '__proto__'); | |
383 C._p = 1; | |
384 assertEquals(1, C.__proto__); | |
385 C.__proto__ = 2; | |
386 assertEquals(2, C.__proto__); | |
387 })(); | |
388 | |
389 | |
390 (function TestSettersOnProto() { | |
391 function Base() {} | |
392 Base.prototype = { | |
393 set constructor(_) { | |
394 assertUnreachable(); | |
395 }, | |
396 set m(_) { | |
397 assertUnreachable(); | |
398 } | |
399 }; | |
400 Object.defineProperty(Base, 'staticM', { | |
401 set: function() { | |
402 assertUnreachable(); | |
403 } | |
404 }); | |
405 | |
406 class C extends Base { | |
407 m() { | |
408 return 1; | |
409 } | |
410 static staticM() { | |
411 return 2; | |
412 } | |
413 } | |
414 | |
415 assertEquals(1, new C().m()); | |
416 assertEquals(2, C.staticM()); | |
417 })(); | |
418 | |
419 | |
420 (function TestConstructableButNoPrototype() { | |
421 var Base = function() {}.bind(); | |
422 assertThrows(function() { | |
423 class C extends Base {} | |
424 }, TypeError); | |
425 })(); | |
426 | |
427 | |
428 (function TestPrototypeGetter() { | |
429 var calls = 0; | |
430 var Base = function() {}.bind(); | |
431 Object.defineProperty(Base, 'prototype', { | |
432 get: function() { | |
433 calls++; | |
434 return null; | |
435 }, | |
436 configurable: true | |
437 }); | |
438 class C extends Base {} | |
439 assertEquals(1, calls); | |
440 | |
441 calls = 0; | |
442 Object.defineProperty(Base, 'prototype', { | |
443 get: function() { | |
444 calls++; | |
445 return 42; | |
446 }, | |
447 configurable: true | |
448 }); | |
449 assertThrows(function() { | |
450 class C extends Base {} | |
451 }, TypeError); | |
452 assertEquals(1, calls); | |
453 })(); | |
454 | |
455 | |
456 (function TestPrototypeSetter() { | |
457 var Base = function() {}.bind(); | |
458 Object.defineProperty(Base, 'prototype', { | |
459 set: function() { | |
460 assertUnreachable(); | |
461 } | |
462 }); | |
463 assertThrows(function() { | |
464 class C extends Base {} | |
465 }, TypeError); | |
466 })(); | |
467 | |
468 | |
469 (function TestSuperInMethods() { | |
470 class B { | |
471 method() { | |
472 return 1; | |
473 } | |
474 get x() { | |
475 return 2; | |
476 } | |
477 } | |
478 class C extends B { | |
479 method() { | |
480 assertEquals(2, super.x); | |
481 return super.method(); | |
482 } | |
483 } | |
484 assertEquals(1, new C().method()); | |
485 })(); | |
486 | |
487 | |
488 (function TestSuperInGetter() { | |
489 class B { | |
490 method() { | |
491 return 1; | |
492 } | |
493 get x() { | |
494 return 2; | |
495 } | |
496 } | |
497 class C extends B { | |
498 get y() { | |
499 assertEquals(2, super.x); | |
500 return super.method(); | |
501 } | |
502 } | |
503 assertEquals(1, new C().y); | |
504 })(); | |
505 | |
506 | |
507 (function TestSuperInSetter() { | |
508 class B { | |
509 method() { | |
510 return 1; | |
511 } | |
512 get x() { | |
513 return 2; | |
514 } | |
515 } | |
516 class C extends B { | |
517 set y(v) { | |
518 assertEquals(3, v); | |
519 assertEquals(2, super.x); | |
520 assertEquals(1, super.method()); | |
521 } | |
522 } | |
523 assertEquals(3, new C().y = 3); | |
524 })(); | |
525 | |
526 | |
527 (function TestSuperInStaticMethods() { | |
528 class B { | |
529 static method() { | |
530 return 1; | |
531 } | |
532 static get x() { | |
533 return 2; | |
534 } | |
535 } | |
536 class C extends B { | |
537 static method() { | |
538 assertEquals(2, super.x); | |
539 return super.method(); | |
540 } | |
541 } | |
542 assertEquals(1, C.method()); | |
543 })(); | |
544 | |
545 | |
546 (function TestSuperInStaticGetter() { | |
547 class B { | |
548 static method() { | |
549 return 1; | |
550 } | |
551 static get x() { | |
552 return 2; | |
553 } | |
554 } | |
555 class C extends B { | |
556 static get x() { | |
557 assertEquals(2, super.x); | |
558 return super.method(); | |
559 } | |
560 } | |
561 assertEquals(1, C.x); | |
562 })(); | |
563 | |
564 | |
565 (function TestSuperInStaticSetter() { | |
566 class B { | |
567 static method() { | |
568 return 1; | |
569 } | |
570 static get x() { | |
571 return 2; | |
572 } | |
573 } | |
574 class C extends B { | |
575 static set x(v) { | |
576 assertEquals(3, v); | |
577 assertEquals(2, super.x); | |
578 assertEquals(1, super.method()); | |
579 } | |
580 } | |
581 assertEquals(3, C.x = 3); | |
582 })(); | |
583 | |
584 | |
585 (function TestNumericPropertyNames() { | |
586 class B { | |
587 1() { return 1; } | |
588 get 2() { return 2; } | |
589 set 3(_) {} | |
590 | |
591 static 4() { return 4; } | |
592 static get 5() { return 5; } | |
593 static set 6(_) {} | |
594 } | |
595 | |
596 assertMethodDescriptor(B.prototype, '1'); | |
597 assertGetterDescriptor(B.prototype, '2'); | |
598 assertSetterDescriptor(B.prototype, '3'); | |
599 | |
600 assertMethodDescriptor(B, '4'); | |
601 assertGetterDescriptor(B, '5'); | |
602 assertSetterDescriptor(B, '6'); | |
603 | |
604 class C extends B { | |
605 1() { return super[1](); } | |
606 get 2() { return super[2]; } | |
607 | |
608 static 4() { return super[4](); } | |
609 static get 5() { return super[5]; } | |
610 } | |
611 | |
612 assertEquals(1, new C()[1]()); | |
613 assertEquals(2, new C()[2]); | |
614 assertEquals(4, C[4]()); | |
615 assertEquals(5, C[5]); | |
616 })(); | |
617 | |
618 | |
619 (function TestDefaultConstructorNoCrash() { | |
620 // Regression test for https://code.google.com/p/v8/issues/detail?id=3661 | |
621 class C {} | |
622 assertThrows(function () {C();}, TypeError); | |
623 assertThrows(function () {C(1);}, TypeError); | |
624 assertTrue(new C() instanceof C); | |
625 assertTrue(new C(1) instanceof C); | |
626 })(); | |
627 | |
628 | |
629 (function TestDefaultConstructor() { | |
630 var calls = 0; | |
631 class Base { | |
632 constructor() { | |
633 calls++; | |
634 } | |
635 } | |
636 class Derived extends Base {} | |
637 var object = new Derived; | |
638 assertEquals(1, calls); | |
639 | |
640 calls = 0; | |
641 assertThrows(function() { Derived(); }, TypeError); | |
642 assertEquals(0, calls); | |
643 })(); | |
644 | |
645 | |
646 (function TestDefaultConstructorArguments() { | |
647 var args, self; | |
648 class Base { | |
649 constructor() { | |
650 self = this; | |
651 args = arguments; | |
652 } | |
653 } | |
654 class Derived extends Base {} | |
655 | |
656 new Derived; | |
657 assertEquals(0, args.length); | |
658 | |
659 new Derived(0, 1, 2); | |
660 assertEquals(3, args.length); | |
661 assertTrue(self instanceof Derived); | |
662 | |
663 var arr = new Array(100); | |
664 var obj = {}; | |
665 assertThrows(function() {Derived.apply(obj, arr);}, TypeError); | |
666 })(); | |
667 | |
668 | |
669 (function TestDefaultConstructorArguments2() { | |
670 var args; | |
671 class Base { | |
672 constructor(x, y) { | |
673 args = arguments; | |
674 } | |
675 } | |
676 class Derived extends Base {} | |
677 | |
678 new Derived; | |
679 assertEquals(0, args.length); | |
680 | |
681 new Derived(1); | |
682 assertEquals(1, args.length); | |
683 assertEquals(1, args[0]); | |
684 | |
685 new Derived(1, 2, 3); | |
686 assertEquals(3, args.length); | |
687 assertEquals(1, args[0]); | |
688 assertEquals(2, args[1]); | |
689 assertEquals(3, args[2]); | |
690 })(); | |
691 | |
692 | |
693 (function TestNameBindingConst() { | |
694 assertThrows('class C { constructor() { C = 42; } }; new C();', TypeError); | |
695 assertThrows('new (class C { constructor() { C = 42; } })', TypeError); | |
696 assertThrows('class C { m() { C = 42; } }; new C().m()', TypeError); | |
697 assertThrows('new (class C { m() { C = 42; } }).m()', TypeError); | |
698 assertThrows('class C { get x() { C = 42; } }; new C().x', TypeError); | |
699 assertThrows('(new (class C { get x() { C = 42; } })).x', TypeError); | |
700 assertThrows('class C { set x(_) { C = 42; } }; new C().x = 15;', TypeError); | |
701 assertThrows('(new (class C { set x(_) { C = 42; } })).x = 15;', TypeError); | |
702 })(); | |
703 | |
704 | |
705 (function TestNameBinding() { | |
706 var C2; | |
707 class C { | |
708 constructor() { | |
709 C2 = C; | |
710 } | |
711 m() { | |
712 C2 = C; | |
713 } | |
714 get x() { | |
715 C2 = C; | |
716 } | |
717 set x(_) { | |
718 C2 = C; | |
719 } | |
720 } | |
721 new C(); | |
722 assertEquals(C, C2); | |
723 | |
724 C2 = undefined; | |
725 new C().m(); | |
726 assertEquals(C, C2); | |
727 | |
728 C2 = undefined; | |
729 new C().x; | |
730 assertEquals(C, C2); | |
731 | |
732 C2 = undefined; | |
733 new C().x = 1; | |
734 assertEquals(C, C2); | |
735 })(); | |
736 | |
737 | |
738 (function TestNameBindingExpression() { | |
739 var C3; | |
740 var C = class C2 { | |
741 constructor() { | |
742 assertEquals(C2, C); | |
743 C3 = C2; | |
744 } | |
745 m() { | |
746 assertEquals(C2, C); | |
747 C3 = C2; | |
748 } | |
749 get x() { | |
750 assertEquals(C2, C); | |
751 C3 = C2; | |
752 } | |
753 set x(_) { | |
754 assertEquals(C2, C); | |
755 C3 = C2; | |
756 } | |
757 } | |
758 new C(); | |
759 assertEquals(C, C3); | |
760 | |
761 C3 = undefined; | |
762 new C().m(); | |
763 assertEquals(C, C3); | |
764 | |
765 C3 = undefined; | |
766 new C().x; | |
767 assertEquals(C, C3); | |
768 | |
769 C3 = undefined; | |
770 new C().x = 1; | |
771 assertEquals(C, C3); | |
772 })(); | |
773 | |
774 | |
775 (function TestNameBindingInExtendsExpression() { | |
776 assertThrows(function() { | |
777 class x extends x {} | |
778 }, ReferenceError); | |
779 | |
780 assertThrows(function() { | |
781 (class x extends x {}); | |
782 }, ReferenceError); | |
783 | |
784 assertThrows(function() { | |
785 var x = (class x extends x {}); | |
786 }, ReferenceError); | |
787 })(); | |
788 | |
789 | |
790 (function TestThisAccessRestriction() { | |
791 class Base {} | |
792 (function() { | |
793 class C extends Base { | |
794 constructor() { | |
795 var y; | |
796 super(); | |
797 } | |
798 }; new C(); | |
799 }()); | |
800 assertThrows(function() { | |
801 class C extends Base { | |
802 constructor() { | |
803 super(this.x); | |
804 } | |
805 }; new C(); | |
806 }, ReferenceError); | |
807 assertThrows(function() { | |
808 class C extends Base { | |
809 constructor() { | |
810 super(this); | |
811 } | |
812 }; new C(); | |
813 }, ReferenceError); | |
814 assertThrows(function() { | |
815 class C extends Base { | |
816 constructor() { | |
817 super.method(); | |
818 super(this); | |
819 } | |
820 }; new C(); | |
821 }, ReferenceError); | |
822 assertThrows(function() { | |
823 class C extends Base { | |
824 constructor() { | |
825 super(super.method()); | |
826 } | |
827 }; new C(); | |
828 }, ReferenceError); | |
829 assertThrows(function() { | |
830 class C extends Base { | |
831 constructor() { | |
832 super(super()); | |
833 } | |
834 }; new C(); | |
835 }, ReferenceError); | |
836 assertThrows(function() { | |
837 class C extends Base { | |
838 constructor() { | |
839 super(1, 2, Object.getPrototypeOf(this)); | |
840 } | |
841 }; new C(); | |
842 }, ReferenceError); | |
843 (function() { | |
844 class C extends Base { | |
845 constructor() { | |
846 { super(1, 2); } | |
847 } | |
848 }; new C(); | |
849 }()); | |
850 (function() { | |
851 class C extends Base { | |
852 constructor() { | |
853 if (1) super(); | |
854 } | |
855 }; new C(); | |
856 }()); | |
857 | |
858 class C1 extends Object { | |
859 constructor() { | |
860 'use strict'; | |
861 super(); | |
862 } | |
863 }; | |
864 new C1(); | |
865 | |
866 class C2 extends Object { | |
867 constructor() { | |
868 ; 'use strict';;;;; | |
869 super(); | |
870 } | |
871 }; | |
872 new C2(); | |
873 | |
874 class C3 extends Object { | |
875 constructor() { | |
876 ; 'use strict';;;;; | |
877 // This is a comment. | |
878 super(); | |
879 } | |
880 }; | |
881 new C3(); | |
882 }()); | |
883 | |
884 | |
885 function testClassRestrictedProperties(C) { | |
886 assertEquals(false, C.hasOwnProperty("arguments")); | |
887 assertThrows(function() { return C.arguments; }, TypeError); | |
888 assertThrows(function() { C.arguments = {}; }, TypeError); | |
889 | |
890 assertEquals(false, C.hasOwnProperty("caller")); | |
891 assertThrows(function() { return C.caller; }, TypeError); | |
892 assertThrows(function() { C.caller = {}; }, TypeError); | |
893 | |
894 assertEquals(false, (new C).method.hasOwnProperty("arguments")); | |
895 assertThrows(function() { return new C().method.arguments; }, TypeError); | |
896 assertThrows(function() { new C().method.arguments = {}; }, TypeError); | |
897 | |
898 assertEquals(false, (new C).method.hasOwnProperty("caller")); | |
899 assertThrows(function() { return new C().method.caller; }, TypeError); | |
900 assertThrows(function() { new C().method.caller = {}; }, TypeError); | |
901 } | |
902 | |
903 | |
904 (function testRestrictedPropertiesStrict() { | |
905 "use strict"; | |
906 class ClassWithDefaultConstructor { | |
907 method() {} | |
908 } | |
909 class Class { | |
910 constructor() {} | |
911 method() {} | |
912 } | |
913 class DerivedClassWithDefaultConstructor extends Class {} | |
914 class DerivedClass extends Class { constructor() { super(); } } | |
915 | |
916 testClassRestrictedProperties(ClassWithDefaultConstructor); | |
917 testClassRestrictedProperties(Class); | |
918 testClassRestrictedProperties(DerivedClassWithDefaultConstructor); | |
919 testClassRestrictedProperties(DerivedClass); | |
920 testClassRestrictedProperties(class { method() {} }); | |
921 testClassRestrictedProperties(class { constructor() {} method() {} }); | |
922 testClassRestrictedProperties(class extends Class { }); | |
923 testClassRestrictedProperties( | |
924 class extends Class { constructor() { super(); } }); | |
925 })(); | |
926 | |
927 | |
928 (function testRestrictedPropertiesSloppy() { | |
929 class ClassWithDefaultConstructor { | |
930 method() {} | |
931 } | |
932 class Class { | |
933 constructor() {} | |
934 method() {} | |
935 } | |
936 class DerivedClassWithDefaultConstructor extends Class {} | |
937 class DerivedClass extends Class { constructor() { super(); } } | |
938 | |
939 testClassRestrictedProperties(ClassWithDefaultConstructor); | |
940 testClassRestrictedProperties(Class); | |
941 testClassRestrictedProperties(DerivedClassWithDefaultConstructor); | |
942 testClassRestrictedProperties(DerivedClass); | |
943 testClassRestrictedProperties(class { method() {} }); | |
944 testClassRestrictedProperties(class { constructor() {} method() {} }); | |
945 testClassRestrictedProperties(class extends Class { }); | |
946 testClassRestrictedProperties( | |
947 class extends Class { constructor() { super(); } }); | |
948 })(); | |
OLD | NEW |