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: --allow-natives-syntax | |
6 // Flags: --harmony-spread-calls --harmony-destructuring | |
7 // Flags: --harmony-rest-parameters --harmony-sloppy | |
8 | |
9 (function TestSuperNamedLoads() { | |
10 function Base() { } | |
11 function fBase() { } | |
12 Base.prototype = { | |
13 f() { | |
14 return "Base " + this.toString(); | |
15 }, | |
16 x: 15, | |
17 toString() { | |
18 return "this is Base"; | |
19 } | |
20 }; | |
21 | |
22 function Derived() { | |
23 this.derivedDataProperty = "xxx"; | |
24 } | |
25 Derived.prototype = { | |
26 __proto__: Base.prototype, | |
27 toString() { return "this is Derived"; }, | |
28 x: 27, | |
29 f() { | |
30 assertEquals("Base this is Derived", super.f()); | |
31 var a = super.x; | |
32 assertEquals(15, a); | |
33 assertEquals(15, super.x); | |
34 assertEquals(27, this.x); | |
35 return "Derived"; | |
36 } | |
37 }; | |
38 | |
39 assertEquals("Base this is Base", new Base().f()); | |
40 assertEquals("Derived", new Derived().f()); | |
41 }()); | |
42 | |
43 | |
44 (function TestSuperKeyedLoads() { | |
45 'use strict'; | |
46 | |
47 var x = 'x'; | |
48 var derivedDataProperty = 'derivedDataProperty'; | |
49 var f = 'f'; | |
50 | |
51 class Base { | |
52 f() { | |
53 return "Base " + this.toString(); | |
54 } | |
55 toString() { | |
56 return "this is Base"; | |
57 } | |
58 } | |
59 | |
60 Base.prototype[x] = 15; | |
61 | |
62 function Derived() { | |
63 this[derivedDataProperty] = "xxx"; | |
64 } | |
65 Derived.prototype = { | |
66 __proto__: Base.prototype, | |
67 toString() { return "this is Derived"; }, | |
68 x: 27, | |
69 f() { | |
70 assertEquals("Base this is Derived", super[f]()); | |
71 var a = super[x]; | |
72 assertEquals(15, a); | |
73 assertEquals(15, super[x]); | |
74 assertEquals(27, this[x]); | |
75 return "Derived"; | |
76 } | |
77 }; | |
78 | |
79 assertEquals("Base this is Base", new Base().f()); | |
80 assertEquals("Derived", new Derived().f()); | |
81 }()); | |
82 | |
83 | |
84 (function TestSuperKeywordNonMethod() { | |
85 'use strict'; | |
86 | |
87 class C { | |
88 f() { | |
89 super.unknown(); | |
90 } | |
91 } | |
92 | |
93 assertThrows(function() { | |
94 new C().f(); | |
95 }, TypeError); | |
96 }()); | |
97 | |
98 | |
99 (function TestGetter() { | |
100 function Base() {} | |
101 var derived; | |
102 Base.prototype = { | |
103 constructor: Base, | |
104 get x() { | |
105 assertSame(this, derived); | |
106 return this._x; | |
107 }, | |
108 _x: 'base' | |
109 }; | |
110 | |
111 function Derived() {} | |
112 Derived.__proto__ = Base; | |
113 Derived.prototype = { | |
114 __proto__: Base.prototype, | |
115 constructor: Derived, | |
116 _x: 'derived', | |
117 testGetter() { | |
118 return super.x; | |
119 }, | |
120 testGetterStrict() { | |
121 'use strict'; | |
122 return super.x; | |
123 } | |
124 }; | |
125 | |
126 derived = new Derived(); | |
127 assertEquals('derived', derived.testGetter()); | |
128 derived = new Derived(); | |
129 assertEquals('derived', derived.testGetterStrict()); | |
130 }()); | |
131 | |
132 | |
133 (function TestGetterKeyed() { | |
134 var x = 'x'; | |
135 function Base() {} | |
136 var derived; | |
137 Base.prototype = { | |
138 constructor: Base, | |
139 get x() { | |
140 assertSame(this, derived); | |
141 return this._x; | |
142 }, | |
143 _x: 'base' | |
144 }; | |
145 | |
146 function Derived() {} | |
147 Derived.__proto__ = Base; | |
148 Derived.prototype = { | |
149 __proto__: Base.prototype, | |
150 constructor: Derived, | |
151 _x: 'derived', | |
152 testGetter() { | |
153 return super[x]; | |
154 }, | |
155 testGetterStrict() { | |
156 'use strict'; | |
157 return super[x]; | |
158 }, | |
159 testGetterWithToString() { | |
160 var toStringCalled; | |
161 var o = { toString: function() { | |
162 toStringCalled++; | |
163 return 'x'; | |
164 } }; | |
165 | |
166 toStringCalled = 0; | |
167 assertEquals('derived', super[o]); | |
168 assertEquals(1, toStringCalled); | |
169 | |
170 var eToThrow = new Error(); | |
171 var oThrowsInToString = { toString: function() { | |
172 throw eToThrow; | |
173 } }; | |
174 | |
175 var ex = null; | |
176 try { | |
177 super[oThrowsInToString]; | |
178 } catch(e) { ex = e } | |
179 assertEquals(eToThrow, ex); | |
180 | |
181 var oReturnsNumericString = { toString: function() { | |
182 return "1"; | |
183 } }; | |
184 | |
185 assertEquals(undefined, super[oReturnsNumericString]); | |
186 assertEquals(undefined, super[1]); | |
187 } | |
188 }; | |
189 | |
190 derived = new Derived(); | |
191 assertEquals('derived', derived.testGetter()); | |
192 derived = new Derived(); | |
193 assertEquals('derived', derived.testGetterStrict()); | |
194 derived = new Derived(); | |
195 derived.testGetterWithToString(); | |
196 }()); | |
197 | |
198 | |
199 (function TestGetterNumericKeyed() { | |
200 var x = 42; | |
201 function Base() {} | |
202 var derived; | |
203 Base.prototype = { | |
204 constructor: Base, | |
205 _x: 'base' | |
206 }; | |
207 | |
208 Object.defineProperty(Base.prototype, x, { get: function() { | |
209 assertSame(this, derived); | |
210 return this._x; | |
211 }}); | |
212 | |
213 function Derived() {} | |
214 Derived.__proto__ = Base; | |
215 Derived.prototype = { | |
216 __proto__: Base.prototype, | |
217 constructor: Derived, | |
218 _x: 'derived', | |
219 testGetter() { | |
220 return super[x]; | |
221 }, | |
222 testGetterStrict() { | |
223 'use strict'; | |
224 return super[x]; | |
225 }, | |
226 testGetterWithToString() { | |
227 var toStringCalled; | |
228 var o = { | |
229 toString: function() { | |
230 toStringCalled++; | |
231 return '42'; | |
232 } | |
233 }; | |
234 | |
235 toStringCalled = 0; | |
236 assertEquals('derived', super[o]); | |
237 assertEquals(1, toStringCalled); | |
238 | |
239 var eToThrow = new Error(); | |
240 var oThrowsInToString = { | |
241 toString: function() { | |
242 throw eToThrow; | |
243 } | |
244 }; | |
245 | |
246 var ex = null; | |
247 try { | |
248 super[oThrowsInToString]; | |
249 } catch(e) { ex = e } | |
250 assertEquals(eToThrow, ex); | |
251 | |
252 var oReturnsNumericString = { | |
253 toString: function() { | |
254 return "42"; | |
255 } | |
256 }; | |
257 | |
258 assertEquals('derived', super[oReturnsNumericString]); | |
259 assertEquals('derived', super[42]); | |
260 } | |
261 }; | |
262 | |
263 derived = new Derived(); | |
264 assertEquals('derived', derived.testGetter()); | |
265 derived = new Derived(); | |
266 assertEquals('derived', derived.testGetterStrict()); | |
267 derived = new Derived(); | |
268 derived.testGetterWithToString(); | |
269 }()); | |
270 | |
271 | |
272 (function TestSetter() { | |
273 function Base() {} | |
274 Base.prototype = { | |
275 constructor: Base, | |
276 get x() { | |
277 return this._x; | |
278 }, | |
279 set x(v) { | |
280 this._x = v; | |
281 }, | |
282 _x: 'base' | |
283 }; | |
284 | |
285 function Derived() {} | |
286 Derived.__proto__ = Base; | |
287 Derived.prototype = { | |
288 __proto__: Base.prototype, | |
289 constructor: Derived, | |
290 _x: 'derived', | |
291 testSetter() { | |
292 assertEquals('foobar', super.x = 'foobar'); | |
293 assertEquals('foobarabc', super.x += 'abc'); | |
294 }, | |
295 testSetterStrict() { | |
296 'use strict'; | |
297 assertEquals('foobar', super.x = 'foobar'); | |
298 assertEquals('foobarabc', super.x += 'abc'); | |
299 } | |
300 }; | |
301 | |
302 var d = new Derived(); | |
303 d.testSetter(); | |
304 assertEquals('base', Base.prototype._x); | |
305 assertEquals('foobarabc', d._x); | |
306 d._x = ''; | |
307 | |
308 d.testSetterStrict(); | |
309 assertEquals('base', Base.prototype._x); | |
310 assertEquals('foobarabc', d._x); | |
311 }()); | |
312 | |
313 | |
314 (function TestSetterNumericKeyed() { | |
315 var x = 42; | |
316 function Base() {} | |
317 Base.prototype = { | |
318 constructor: Base, | |
319 _x: 'base' | |
320 }; | |
321 | |
322 Object.defineProperty(Base.prototype, x, | |
323 { get: function() { return this._x; }, | |
324 set: function(v) { this._x = v; } | |
325 }); | |
326 | |
327 function Derived() {} | |
328 Derived.__proto__ = Base; | |
329 Derived.prototype = { | |
330 __proto__: Base.prototype, | |
331 constructor: Derived, | |
332 _x: 'derived', | |
333 testSetter() { | |
334 assertEquals('foobar', super[x] = 'foobar'); | |
335 assertEquals('foobarabc', super[x] += 'abc'); | |
336 }, | |
337 testSetterStrict() { | |
338 'use strict'; | |
339 assertEquals('foobar', super[x] = 'foobar'); | |
340 assertEquals('foobarabc', super[x] += 'abc'); | |
341 }, | |
342 testSetterWithToString() { | |
343 var toStringCalled; | |
344 var o = { | |
345 toString: function() { | |
346 toStringCalled++; | |
347 return x; | |
348 } | |
349 }; | |
350 | |
351 toStringCalled = 0; | |
352 super[o] = 'set'; | |
353 assertEquals(1, toStringCalled); | |
354 assertEquals('set', this._x); | |
355 | |
356 var eToThrow = new Error(); | |
357 var oThrowsInToString = { | |
358 toString: function() { | |
359 throw eToThrow; | |
360 } | |
361 }; | |
362 | |
363 var ex = null; | |
364 try { | |
365 super[oThrowsInToString] = 'xyz'; | |
366 } catch(e) { ex = e } | |
367 assertEquals(eToThrow, ex); | |
368 assertEquals('set', this._x); | |
369 } | |
370 }; | |
371 | |
372 var d = new Derived(); | |
373 d.testSetter(); | |
374 assertEquals('base', Base.prototype._x); | |
375 assertEquals('foobarabc', d._x); | |
376 d._x = ''; | |
377 | |
378 d.testSetterStrict(); | |
379 assertEquals('base', Base.prototype._x); | |
380 assertEquals('foobarabc', d._x); | |
381 | |
382 d = new Derived(); | |
383 d.testSetterWithToString(); | |
384 }()); | |
385 | |
386 | |
387 (function TestSetterKeyed() { | |
388 var x = 'x'; | |
389 function Base() {} | |
390 Base.prototype = { | |
391 constructor: Base, | |
392 get x() { | |
393 return this._x; | |
394 }, | |
395 set x(v) { | |
396 this._x = v; | |
397 }, | |
398 _x: 'base' | |
399 }; | |
400 | |
401 function Derived() {} | |
402 Derived.__proto__ = Base; | |
403 Derived.prototype = { | |
404 __proto__: Base.prototype, | |
405 constructor: Derived, | |
406 _x: 'derived', | |
407 testSetter() { | |
408 assertEquals('foobar', super[x] = 'foobar'); | |
409 assertEquals('foobarabc', super[x] += 'abc'); | |
410 }, | |
411 testSetterStrict() { | |
412 'use strict'; | |
413 assertEquals('foobar', super[x] = 'foobar'); | |
414 assertEquals('foobarabc', super[x] += 'abc'); | |
415 }, | |
416 testSetterWithToString() { | |
417 var toStringCalled; | |
418 var o = { | |
419 toString: function() { | |
420 toStringCalled++; | |
421 return 'x'; | |
422 } | |
423 }; | |
424 | |
425 toStringCalled = 0; | |
426 super[o] = 'set'; | |
427 assertEquals(1, toStringCalled); | |
428 assertEquals('set', this._x); | |
429 | |
430 var eToThrow = new Error(); | |
431 var oThrowsInToString = { | |
432 toString: function() { | |
433 throw eToThrow; | |
434 } | |
435 }; | |
436 | |
437 var ex = null; | |
438 try { | |
439 super[oThrowsInToString] = 'xyz'; | |
440 } catch(e) { ex = e } | |
441 assertEquals(eToThrow, ex); | |
442 assertEquals('set', this._x); | |
443 | |
444 var oReturnsNumericString = { | |
445 toString: function() { | |
446 return "1"; | |
447 } | |
448 }; | |
449 | |
450 assertEquals('abc', super[oReturnsNumericString] = 'abc'); | |
451 | |
452 assertEquals('set', this._x); | |
453 | |
454 assertEquals(10, super[1] = 10); | |
455 } | |
456 }; | |
457 | |
458 var d = new Derived(); | |
459 d.testSetter(); | |
460 assertEquals('base', Base.prototype._x); | |
461 assertEquals('foobarabc', d._x); | |
462 d._x = ''; | |
463 d.testSetterStrict(); | |
464 assertEquals('base', Base.prototype._x); | |
465 assertEquals('foobarabc', d._x); | |
466 | |
467 d = new Derived(); | |
468 d.testSetterWithToString(); | |
469 }()); | |
470 | |
471 | |
472 (function TestSetterDataProperties() { | |
473 function Base() {} | |
474 Base.prototype = { | |
475 constructor: Base, | |
476 x: 'x from Base' | |
477 }; | |
478 | |
479 function Derived() {} | |
480 Derived.prototype = { | |
481 __proto__: Base.prototype, | |
482 constructor: Derived, | |
483 testSetter() { | |
484 assertEquals('x from Base', super.x); | |
485 super.x = 'data property'; | |
486 assertEquals('x from Base', super.x); | |
487 assertEquals('data property', this.x); | |
488 } | |
489 }; | |
490 | |
491 new Derived().testSetter(); | |
492 }()); | |
493 | |
494 | |
495 (function TestKeyedSetterDataProperties() { | |
496 var x = 'x'; | |
497 function Base() {} | |
498 Base.prototype = { | |
499 constructor: Base, | |
500 x: 'x from Base' | |
501 }; | |
502 | |
503 function Derived() {} | |
504 Derived.prototype = { | |
505 __proto__: Base.prototype, | |
506 constructor: Derived, | |
507 testSetter() { | |
508 assertEquals('x from Base', super[x]); | |
509 super[x] = 'data property'; | |
510 assertEquals('x from Base', super[x]); | |
511 assertEquals('data property', this[x]); | |
512 } | |
513 }; | |
514 | |
515 new Derived().testSetter(); | |
516 }()); | |
517 | |
518 | |
519 (function TestKeyedNumericSetterDataProperties() { | |
520 var x = 42; | |
521 function Base() {} | |
522 Base.prototype = { | |
523 constructor: Base, | |
524 42: 'x from Base' | |
525 }; | |
526 | |
527 function Derived() {} | |
528 Derived.prototype = { | |
529 __proto__: Base.prototype, | |
530 constructor: Derived, | |
531 testSetter() { | |
532 assertEquals('x from Base', super[x]); | |
533 super[x] = 'data property'; | |
534 assertEquals('x from Base', super[x]); | |
535 assertEquals('data property', this[x]); | |
536 } | |
537 }; | |
538 | |
539 new Derived().testSetter(); | |
540 }()); | |
541 | |
542 | |
543 (function TestAccessorsOnPrimitives() { | |
544 var getCalled = 0; | |
545 var setCalled = 0; | |
546 function Base() {} | |
547 Base.prototype = { | |
548 constructor: Base, | |
549 get x() { | |
550 getCalled++; | |
551 return 1; | |
552 }, | |
553 set x(v) { | |
554 setCalled++; | |
555 return v; | |
556 }, | |
557 }; | |
558 | |
559 function Derived() {} | |
560 Derived.prototype = { | |
561 __proto__: Base.prototype, | |
562 constructor: Derived, | |
563 testSetter() { | |
564 setCalled = 0; | |
565 getCalled = 0; | |
566 assertEquals('object', typeof this); | |
567 assertInstanceof(this, Number) | |
568 assertEquals(42, this.valueOf()); | |
569 assertEquals(1, super.x); | |
570 assertEquals(1, getCalled); | |
571 assertEquals(0, setCalled); | |
572 | |
573 assertEquals(5, super.x = 5); | |
574 assertEquals(1, getCalled); | |
575 assertEquals(1, setCalled); | |
576 | |
577 assertEquals(6, super.x += 5); | |
578 assertEquals(2, getCalled); | |
579 assertEquals(2, setCalled); | |
580 | |
581 super.newProperty = 15; | |
582 assertEquals(15, this.newProperty); | |
583 assertEquals(undefined, super.newProperty); | |
584 }, | |
585 testSetterStrict() { | |
586 'use strict'; | |
587 getCalled = 0; | |
588 setCalled = 0; | |
589 assertTrue(42 === this); | |
590 | |
591 assertEquals(1, super.x); | |
592 assertEquals(1, getCalled); | |
593 assertEquals(0, setCalled); | |
594 | |
595 assertEquals(5, super.x = 5); | |
596 assertEquals(1, getCalled); | |
597 assertEquals(1, setCalled); | |
598 | |
599 assertEquals(6, super.x += 5); | |
600 assertEquals(2, getCalled); | |
601 assertEquals(2, setCalled); | |
602 | |
603 var ex; | |
604 try { | |
605 super.newProperty = 15; | |
606 } catch (e) { ex = e; } | |
607 assertInstanceof(ex, TypeError); | |
608 } | |
609 } | |
610 | |
611 Derived.prototype.testSetter.call(42); | |
612 Derived.prototype.testSetterStrict.call(42); | |
613 | |
614 function DerivedFromString() {} | |
615 DerivedFromString.prototype = { | |
616 __proto__: String.prototype, | |
617 f() { | |
618 'use strict'; | |
619 assertTrue(42 === this); | |
620 assertEquals(String.prototype.toString, super.toString); | |
621 var ex; | |
622 try { | |
623 super.toString(); | |
624 } catch(e) { ex = e; } | |
625 | |
626 assertInstanceof(ex, TypeError); | |
627 } | |
628 }; | |
629 | |
630 DerivedFromString.prototype.f.call(42); | |
631 }()); | |
632 | |
633 | |
634 (function TestKeyedAccessorsOnPrimitives() { | |
635 var x = 'x'; | |
636 var newProperty = 'newProperty'; | |
637 var toString = 'toString'; | |
638 var getCalled = 0; | |
639 var setCalled = 0; | |
640 function Base() {} | |
641 Base.prototype = { | |
642 constructor: Base, | |
643 get x() { | |
644 getCalled++; | |
645 return 1; | |
646 }, | |
647 set x(v) { | |
648 setCalled++; | |
649 return v; | |
650 }, | |
651 }; | |
652 | |
653 function Derived() {} | |
654 Derived.prototype = { | |
655 __proto__: Base.prototype, | |
656 constructor: Derived, | |
657 testSetter() { | |
658 setCalled = 0; | |
659 getCalled = 0; | |
660 assertEquals('object', typeof this); | |
661 assertInstanceof(this, Number) | |
662 assertEquals(42, this.valueOf()); | |
663 assertEquals(1, super[x]); | |
664 assertEquals(1, getCalled); | |
665 assertEquals(0, setCalled); | |
666 | |
667 assertEquals(5, super[x] = 5); | |
668 assertEquals(1, getCalled); | |
669 assertEquals(1, setCalled); | |
670 | |
671 assertEquals(6, super[x] += 5); | |
672 assertEquals(2, getCalled); | |
673 assertEquals(2, setCalled); | |
674 | |
675 super[newProperty] = 15; | |
676 assertEquals(15, this[newProperty]); | |
677 assertEquals(undefined, super[newProperty]); | |
678 }, | |
679 testSetterStrict() { | |
680 'use strict'; | |
681 getCalled = 0; | |
682 setCalled = 0; | |
683 assertTrue(42 === this); | |
684 | |
685 assertEquals(1, super[x]); | |
686 assertEquals(1, getCalled); | |
687 assertEquals(0, setCalled); | |
688 | |
689 assertEquals(5, super[x] = 5); | |
690 assertEquals(1, getCalled); | |
691 assertEquals(1, setCalled); | |
692 | |
693 assertEquals(6, super[x] += 5); | |
694 assertEquals(2, getCalled); | |
695 assertEquals(2, setCalled); | |
696 | |
697 var ex; | |
698 try { | |
699 super[newProperty] = 15; | |
700 } catch (e) { ex = e; } | |
701 assertInstanceof(ex,TypeError); | |
702 } | |
703 }; | |
704 | |
705 Derived.prototype.testSetter.call(42); | |
706 Derived.prototype.testSetterStrict.call(42); | |
707 | |
708 function DerivedFromString() {} | |
709 DerivedFromString.prototype = { | |
710 __proto__: String.prototype, | |
711 f() { | |
712 'use strict'; | |
713 assertTrue(42 === this); | |
714 assertEquals(String.prototype.toString, super[toString]); | |
715 var ex; | |
716 try { | |
717 super[toString](); | |
718 } catch(e) { ex = e; } | |
719 | |
720 assertInstanceof(ex, TypeError); | |
721 } | |
722 }; | |
723 DerivedFromString.prototype.f.call(42); | |
724 }()); | |
725 | |
726 | |
727 (function TestNumericKeyedAccessorsOnPrimitives() { | |
728 var x = 42; | |
729 var newProperty = 43; | |
730 var getCalled = 0; | |
731 var setCalled = 0; | |
732 function Base() {} | |
733 Base.prototype = { | |
734 constructor: Base, | |
735 }; | |
736 | |
737 Object.defineProperty(Base.prototype, x, { | |
738 get: function() { | |
739 getCalled++; | |
740 return 1; | |
741 }, | |
742 set: function(v) { | |
743 setCalled++; | |
744 return v; | |
745 } | |
746 }); | |
747 | |
748 function Derived() {} | |
749 Derived.prototype = { | |
750 __proto__: Base.prototype, | |
751 constructor: Derived, | |
752 testSetter() { | |
753 setCalled = 0; | |
754 getCalled = 0; | |
755 assertEquals('object', typeof this); | |
756 assertInstanceof(this, Number) | |
757 assertEquals(42, this.valueOf()); | |
758 assertEquals(1, super[x]); | |
759 assertEquals(1, getCalled); | |
760 assertEquals(0, setCalled); | |
761 | |
762 assertEquals(5, super[x] = 5); | |
763 assertEquals(1, getCalled); | |
764 assertEquals(1, setCalled); | |
765 | |
766 assertEquals(6, super[x] += 5); | |
767 assertEquals(2, getCalled); | |
768 assertEquals(2, setCalled); | |
769 | |
770 super[newProperty] = 15; | |
771 assertEquals(15, this[newProperty]); | |
772 assertEquals(undefined, super[newProperty]); | |
773 }, | |
774 testSetterStrict() { | |
775 'use strict'; | |
776 getCalled = 0; | |
777 setCalled = 0; | |
778 assertTrue(42 === this); | |
779 | |
780 assertEquals(1, super[x]); | |
781 assertEquals(1, getCalled); | |
782 assertEquals(0, setCalled); | |
783 | |
784 assertEquals(5, super[x] = 5); | |
785 assertEquals(1, getCalled); | |
786 assertEquals(1, setCalled); | |
787 | |
788 assertEquals(6, super[x] += 5); | |
789 assertEquals(2, getCalled); | |
790 assertEquals(2, setCalled); | |
791 | |
792 var ex; | |
793 try { | |
794 super[newProperty] = 15; | |
795 } catch (e) { ex = e; } | |
796 assertInstanceof(ex, TypeError); | |
797 } | |
798 }; | |
799 | |
800 Derived.prototype.testSetter.call(42); | |
801 Derived.prototype.testSetterStrict.call(42); | |
802 }()); | |
803 | |
804 | |
805 (function TestKeyedNumericSetterOnExotics() { | |
806 function Base() {} | |
807 function Derived() {} | |
808 Derived.prototype = { | |
809 __proto__: Base.prototype, | |
810 callSetterOnArray() { | |
811 super[42] = 1; | |
812 }, | |
813 callStrictSetterOnString() { | |
814 'use strict'; | |
815 assertEquals('string', typeof this); | |
816 assertTrue('abcdef' === this); | |
817 var ex = null; | |
818 try { | |
819 super[5] = 'q'; | |
820 } catch(e) { ex = e; } | |
821 assertInstanceof(ex, TypeError); | |
822 | |
823 ex = null; | |
824 try { | |
825 super[1024] = 'q'; | |
826 } catch(e) { ex = e; } | |
827 assertInstanceof(ex, TypeError); | |
828 } | |
829 }; | |
830 | |
831 var x = []; | |
832 assertEquals(0, x.length); | |
833 Derived.prototype.callSetterOnArray.call(x); | |
834 assertEquals(43, x.length); | |
835 assertEquals(1, x[42]); | |
836 | |
837 var s = 'abcdef'; | |
838 Derived.prototype.callStrictSetterOnString.call(s) | |
839 }()); | |
840 | |
841 | |
842 (function TestSetterUndefinedProperties() { | |
843 function Base() {} | |
844 function Derived() {} | |
845 Derived.prototype = { | |
846 __proto__: Base.prototype, | |
847 mSloppy() { | |
848 assertEquals(undefined, super.x); | |
849 assertEquals(undefined, this.x); | |
850 super.x = 10; | |
851 assertEquals(10, this.x); | |
852 assertEquals(undefined, super.x); | |
853 }, | |
854 mStrict() { | |
855 'use strict'; | |
856 assertEquals(undefined, super.x); | |
857 assertEquals(undefined, this.x); | |
858 super.x = 10; | |
859 assertEquals(10, this.x); | |
860 assertEquals(undefined, super.x); | |
861 } | |
862 }; | |
863 | |
864 var d = new Derived(); | |
865 d.mSloppy(); | |
866 assertEquals(10, d.x); | |
867 var d1 = new Derived(); | |
868 d1.mStrict(); | |
869 assertEquals(10, d.x); | |
870 }()); | |
871 | |
872 | |
873 (function TestKeyedSetterUndefinedProperties() { | |
874 var x = 'x'; | |
875 function Base() {} | |
876 function Derived() {} | |
877 Derived.prototype = { | |
878 __proto__: Base.prototype, | |
879 mSloppy() { | |
880 assertEquals(undefined, super[x]); | |
881 assertEquals(undefined, this[x]); | |
882 super[x] = 10; | |
883 assertEquals(10, this[x]); | |
884 assertEquals(undefined, super[x]); | |
885 }, | |
886 mStrict() { | |
887 'use strict'; | |
888 assertEquals(undefined, super[x]); | |
889 assertEquals(undefined, this[x]); | |
890 super[x] = 10; | |
891 assertEquals(10, this[x]); | |
892 assertEquals(undefined, super[x]); | |
893 } | |
894 }; | |
895 var d = new Derived(); | |
896 d.mSloppy(); | |
897 assertEquals(10, d.x); | |
898 var d1 = new Derived(); | |
899 d1.mStrict(); | |
900 assertEquals(10, d.x); | |
901 }()); | |
902 | |
903 | |
904 (function TestKeyedNumericSetterUndefinedProperties() { | |
905 var x = 42; | |
906 function Base() {} | |
907 function Derived() {} | |
908 Derived.prototype = { | |
909 __proto__: Base.prototype, | |
910 mSloppy() { | |
911 assertEquals(undefined, super[x]); | |
912 assertEquals(undefined, this[x]); | |
913 super[x] = 10; | |
914 assertEquals(10, this[x]); | |
915 assertEquals(undefined, super[x]); | |
916 }, | |
917 mStrict() { | |
918 'use strict'; | |
919 assertEquals(undefined, super[x]); | |
920 assertEquals(undefined, this[x]); | |
921 super[x] = 10; | |
922 assertEquals(10, this[x]); | |
923 assertEquals(undefined, super[x]); | |
924 } | |
925 }; | |
926 var d = new Derived(); | |
927 d.mSloppy(); | |
928 assertEquals(10, d[x]); | |
929 var d1 = new Derived(); | |
930 d1.mStrict(); | |
931 assertEquals(10, d[x]); | |
932 }()); | |
933 | |
934 | |
935 (function TestSetterCreatingOwnPropertiesReconfigurable() { | |
936 function Base() {} | |
937 function Derived() {} | |
938 Derived.prototype = { | |
939 __proto__: Base.prototype, | |
940 mSloppy() { | |
941 assertEquals(42, this.ownReadOnly); | |
942 super.ownReadOnly = 55; | |
943 assertEquals(55, this.ownReadOnly); | |
944 var descr = Object.getOwnPropertyDescriptor(this, 'ownReadOnly'); | |
945 assertEquals(55, descr.value); | |
946 assertTrue(descr.configurable); | |
947 assertFalse(descr.enumerable); | |
948 assertFalse(descr.writable); | |
949 assertFalse(Base.prototype.hasOwnProperty('ownReadOnly')); | |
950 | |
951 assertEquals(15, this.ownReadonlyAccessor); | |
952 super.ownReadonlyAccessor = 25; | |
953 assertEquals(25, this.ownReadonlyAccessor); | |
954 var descr = Object.getOwnPropertyDescriptor(this, 'ownReadonlyAccessor'); | |
955 assertEquals(25, descr.value); | |
956 assertTrue(descr.configurable); | |
957 assertFalse(descr.enumerable); | |
958 assertTrue(descr.writable); | |
959 assertFalse(Base.prototype.hasOwnProperty('ownReadonlyAccessor')); | |
960 | |
961 super.ownSetter = 35; | |
962 assertEquals(35, this.ownSetter); | |
963 var descr = Object.getOwnPropertyDescriptor(this, 'ownSetter'); | |
964 assertEquals(35, descr.value); | |
965 assertTrue(descr.configurable); | |
966 assertFalse(descr.enumerable); | |
967 assertTrue(descr.writable); | |
968 assertFalse(Base.prototype.hasOwnProperty('ownSetter')); | |
969 }, | |
970 mStrict() { | |
971 'use strict'; | |
972 assertEquals(42, this.ownReadOnly); | |
973 super.ownReadOnly = 55; | |
974 assertEquals(55, this.ownReadOnly); | |
975 var descr = Object.getOwnPropertyDescriptor(this, 'ownReadOnly'); | |
976 assertEquals(55, descr.value); | |
977 assertTrue(descr.configurable); | |
978 assertFalse(descr.enumerable); | |
979 assertFalse(descr.writable); | |
980 assertFalse(Base.prototype.hasOwnProperty('ownReadOnly')); | |
981 | |
982 assertEquals(15, this.ownReadonlyAccessor); | |
983 super.ownReadonlyAccessor = 25; | |
984 assertEquals(25, this.ownReadonlyAccessor); | |
985 var descr = Object.getOwnPropertyDescriptor(this, 'ownReadonlyAccessor'); | |
986 assertEquals(25, descr.value); | |
987 assertTrue(descr.configurable); | |
988 assertFalse(descr.enumerable); | |
989 assertTrue(descr.writable); | |
990 assertFalse(Base.prototype.hasOwnProperty('ownReadonlyAccessor')); | |
991 | |
992 super.ownSetter = 35; | |
993 assertEquals(35, this.ownSetter); | |
994 var descr = Object.getOwnPropertyDescriptor(this, 'ownSetter'); | |
995 assertEquals(35, descr.value); | |
996 assertTrue(descr.configurable); | |
997 assertFalse(descr.enumerable); | |
998 assertTrue(descr.writable); | |
999 assertFalse(Base.prototype.hasOwnProperty('ownSetter')); | |
1000 }, | |
1001 }; | |
1002 | |
1003 var d = new Derived(); | |
1004 Object.defineProperty(d, 'ownReadOnly', { | |
1005 value: 42, | |
1006 writable: false, | |
1007 configurable: true | |
1008 }); | |
1009 Object.defineProperty(d, 'ownSetter', { | |
1010 set: function() { assertUnreachable(); }, | |
1011 configurable: true | |
1012 }); | |
1013 Object.defineProperty(d, 'ownReadonlyAccessor', { | |
1014 get: function() { return 15; }, | |
1015 configurable: true | |
1016 }); | |
1017 | |
1018 d.mSloppy(); | |
1019 | |
1020 var d = new Derived(); | |
1021 Object.defineProperty(d, 'ownReadOnly', { | |
1022 value: 42, | |
1023 writable: false, | |
1024 configurable: true | |
1025 }); | |
1026 Object.defineProperty(d, 'ownSetter', { | |
1027 set: function() { assertUnreachable(); }, | |
1028 configurable: true | |
1029 }); | |
1030 Object.defineProperty(d, 'ownReadonlyAccessor', { | |
1031 get: function() { return 15; }, | |
1032 configurable: true | |
1033 }); | |
1034 d.mStrict(); | |
1035 }()); | |
1036 | |
1037 | |
1038 (function TestSetterCreatingOwnPropertiesNonConfigurable() { | |
1039 function Base() {} | |
1040 function Derived() {} | |
1041 Derived.prototype = { | |
1042 __proto__: Base.prototype, | |
1043 mSloppy() { | |
1044 assertEquals(42, this.ownReadOnly); | |
1045 super.ownReadOnly = 55; | |
1046 assertEquals(42, this.ownReadOnly); | |
1047 var descr = Object.getOwnPropertyDescriptor(this, 'ownReadOnly'); | |
1048 assertEquals(42, descr.value); | |
1049 assertFalse(descr.configurable); | |
1050 assertFalse(descr.enumerable); | |
1051 assertFalse(descr.writable); | |
1052 assertFalse(Base.prototype.hasOwnProperty('ownReadOnly')); | |
1053 | |
1054 assertEquals(15, this.ownReadonlyAccessor); | |
1055 super.ownReadonlyAccessor = 25; | |
1056 assertEquals(15, this.ownReadonlyAccessor); | |
1057 var descr = Object.getOwnPropertyDescriptor(this, 'ownReadonlyAccessor'); | |
1058 assertFalse(descr.configurable); | |
1059 assertFalse(descr.enumerable); | |
1060 assertFalse(Base.prototype.hasOwnProperty('ownReadonlyAccessor')); | |
1061 | |
1062 super.ownSetter = 35; | |
1063 var descr = Object.getOwnPropertyDescriptor(this, 'ownSetter'); | |
1064 assertFalse(descr.configurable); | |
1065 assertFalse(descr.enumerable); | |
1066 assertFalse(Base.prototype.hasOwnProperty('ownSetter')); | |
1067 }, | |
1068 mStrict() { | |
1069 'use strict'; | |
1070 var ex; | |
1071 assertEquals(42, this.ownReadOnly); | |
1072 try { | |
1073 super.ownReadOnly = 55; | |
1074 } catch (e) { | |
1075 ex = e; | |
1076 } | |
1077 assertInstanceof(ex, TypeError); | |
1078 assertEquals( | |
1079 "Cannot assign to read only property 'ownReadOnly' of object '#<Base>'
", | |
1080 ex.message); | |
1081 assertEquals(42, this.ownReadOnly); | |
1082 | |
1083 ex = null; | |
1084 assertEquals(15, this.ownReadonlyAccessor); | |
1085 try { | |
1086 super.ownReadonlyAccessor = 25; | |
1087 } catch (e) { | |
1088 ex = e; | |
1089 } | |
1090 assertInstanceof(ex, TypeError); | |
1091 assertEquals('Cannot redefine property: ownReadonlyAccessor', ex.message); | |
1092 assertEquals(15, this.ownReadonlyAccessor); | |
1093 | |
1094 ex = null; | |
1095 try { | |
1096 super.ownSetter = 35; | |
1097 } catch (e) { | |
1098 ex = e; | |
1099 } | |
1100 assertInstanceof(ex, TypeError); | |
1101 assertEquals('Cannot redefine property: ownSetter', ex.message); | |
1102 } | |
1103 }; | |
1104 | |
1105 var d = new Derived(); | |
1106 Object.defineProperty(d, 'ownReadOnly', { value : 42, writable : false }); | |
1107 Object.defineProperty(d, 'ownSetter', | |
1108 { set : function() { assertUnreachable(); } }); | |
1109 Object.defineProperty(d, 'ownReadonlyAccessor', | |
1110 { get : function() { return 15; }}); | |
1111 d.mSloppy(); | |
1112 d.mStrict(); | |
1113 }()); | |
1114 | |
1115 | |
1116 (function TestSetterInForIn() { | |
1117 var setCalled = 0; | |
1118 var getCalled = 0; | |
1119 function Base() {} | |
1120 Base.prototype = { | |
1121 constructor: Base, | |
1122 get x() { | |
1123 getCalled++; | |
1124 return 1; | |
1125 }, | |
1126 set x(v) { | |
1127 setCalled++; | |
1128 this.x_.push(v); | |
1129 }, | |
1130 }; | |
1131 | |
1132 function Derived() { | |
1133 this.x_ = []; | |
1134 } | |
1135 Derived.prototype = { | |
1136 __proto__: Base.prototype, | |
1137 constructor: Derived, | |
1138 testIter() { | |
1139 setCalled = 0; | |
1140 getCalled = 0; | |
1141 for (super.x in [1,2,3]) {} | |
1142 assertEquals(0, getCalled); | |
1143 assertEquals(3, setCalled); | |
1144 assertEquals(["0", "1", "2"], this.x_); | |
1145 }, | |
1146 testIterKeyed() { | |
1147 setCalled = 0; | |
1148 getCalled = 0; | |
1149 for (super[x] in [1,2,3]) {} | |
1150 assertEquals(0, getCalled); | |
1151 assertEquals(3, setCalled); | |
1152 assertEquals(["0","1","2"], this.x_); | |
1153 | |
1154 this.x_ = []; | |
1155 setCalled = 0; | |
1156 getCalled = 0; | |
1157 var toStringCalled = 0; | |
1158 var o = {toString: function () { toStringCalled++; return x }}; | |
1159 for (super[o] in [1,2,3]) {} | |
1160 assertEquals(0, getCalled); | |
1161 assertEquals(3, setCalled); | |
1162 assertEquals(3, toStringCalled); | |
1163 assertEquals(["0","1","2"], this.x_); | |
1164 } | |
1165 }; | |
1166 | |
1167 new Derived().testIter(); | |
1168 | |
1169 var x = 'x'; | |
1170 | |
1171 new Derived().testIterKeyed(); | |
1172 }()); | |
1173 | |
1174 | |
1175 function TestKeyedSetterCreatingOwnPropertiesReconfigurable(ownReadOnly, | |
1176 ownReadonlyAccessor, ownSetter) { | |
1177 function Base() {} | |
1178 function Derived() {} | |
1179 Derived.prototype = { | |
1180 __proto__: Base.prototype, | |
1181 mSloppy() { | |
1182 assertEquals(42, this[ownReadOnly]); | |
1183 super[ownReadOnly] = 55; | |
1184 assertEquals(55, this[ownReadOnly]); | |
1185 var descr = Object.getOwnPropertyDescriptor(this, ownReadOnly); | |
1186 assertEquals(55, descr.value); | |
1187 assertTrue(descr.configurable); | |
1188 assertFalse(descr.enumerable); | |
1189 assertFalse(descr.writable); | |
1190 assertFalse(Base.prototype.hasOwnProperty(ownReadOnly)); | |
1191 | |
1192 assertEquals(15, this[ownReadonlyAccessor]); | |
1193 super[ownReadonlyAccessor] = 25; | |
1194 assertEquals(25, this[ownReadonlyAccessor]); | |
1195 var descr = Object.getOwnPropertyDescriptor(this, ownReadonlyAccessor); | |
1196 assertEquals(25, descr.value); | |
1197 assertTrue(descr.configurable); | |
1198 assertFalse(descr.enumerable); | |
1199 assertTrue(descr.writable); | |
1200 assertFalse(Base.prototype.hasOwnProperty(ownReadonlyAccessor)); | |
1201 | |
1202 super[ownSetter] = 35; | |
1203 assertEquals(35, this[ownSetter]); | |
1204 var descr = Object.getOwnPropertyDescriptor(this, ownSetter); | |
1205 assertEquals(35, descr.value); | |
1206 assertTrue(descr.configurable); | |
1207 assertFalse(descr.enumerable); | |
1208 assertTrue(descr.writable); | |
1209 assertFalse(Base.prototype.hasOwnProperty(ownSetter)); | |
1210 }, | |
1211 mStrict() { | |
1212 'use strict'; | |
1213 assertEquals(42, this[ownReadOnly]); | |
1214 super[ownReadOnly] = 55; | |
1215 assertEquals(55, this[ownReadOnly]); | |
1216 var descr = Object.getOwnPropertyDescriptor(this, ownReadOnly); | |
1217 assertEquals(55, descr.value); | |
1218 assertTrue(descr.configurable); | |
1219 assertFalse(descr.enumerable); | |
1220 assertFalse(descr.writable); | |
1221 assertFalse(Base.prototype.hasOwnProperty(ownReadOnly)); | |
1222 | |
1223 assertEquals(15, this[ownReadonlyAccessor]); | |
1224 super[ownReadonlyAccessor] = 25; | |
1225 assertEquals(25, this[ownReadonlyAccessor]); | |
1226 var descr = Object.getOwnPropertyDescriptor(this, ownReadonlyAccessor); | |
1227 assertEquals(25, descr.value); | |
1228 assertTrue(descr.configurable); | |
1229 assertFalse(descr.enumerable); | |
1230 assertTrue(descr.writable); | |
1231 assertFalse(Base.prototype.hasOwnProperty(ownReadonlyAccessor)); | |
1232 | |
1233 super[ownSetter] = 35; | |
1234 assertEquals(35, this[ownSetter]); | |
1235 var descr = Object.getOwnPropertyDescriptor(this, ownSetter); | |
1236 assertEquals(35, descr.value); | |
1237 assertTrue(descr.configurable); | |
1238 assertFalse(descr.enumerable); | |
1239 assertTrue(descr.writable); | |
1240 assertFalse(Base.prototype.hasOwnProperty(ownSetter)); | |
1241 }, | |
1242 }; | |
1243 | |
1244 var d = new Derived(); | |
1245 Object.defineProperty(d, ownReadOnly, { | |
1246 value: 42, | |
1247 writable: false, | |
1248 configurable: true | |
1249 }); | |
1250 Object.defineProperty(d, ownSetter, { | |
1251 set: function() { assertUnreachable(); }, | |
1252 configurable: true | |
1253 }); | |
1254 Object.defineProperty(d, ownReadonlyAccessor, { | |
1255 get: function() { return 15; }, | |
1256 configurable: true | |
1257 }); | |
1258 | |
1259 d.mSloppy(); | |
1260 | |
1261 var d = new Derived(); | |
1262 Object.defineProperty(d, ownReadOnly, { | |
1263 value: 42, | |
1264 writable: false, | |
1265 configurable: true | |
1266 }); | |
1267 Object.defineProperty(d, ownSetter, { | |
1268 set: function() { assertUnreachable(); }, | |
1269 configurable: true | |
1270 }); | |
1271 Object.defineProperty(d, ownReadonlyAccessor, { | |
1272 get: function() { return 15; }, | |
1273 configurable: true | |
1274 }); | |
1275 d.mStrict(); | |
1276 } | |
1277 TestKeyedSetterCreatingOwnPropertiesReconfigurable('ownReadOnly', | |
1278 'ownReadonlyAccessor', | |
1279 'ownSetter'); | |
1280 TestKeyedSetterCreatingOwnPropertiesReconfigurable(42, 43, 44); | |
1281 | |
1282 | |
1283 function TestKeyedSetterCreatingOwnPropertiesNonConfigurable( | |
1284 ownReadOnly, ownReadonlyAccessor, ownSetter) { | |
1285 function Base() {} | |
1286 function Derived() {} | |
1287 Derived.prototype = { | |
1288 __proto__: Base.prototype, | |
1289 mSloppy() { | |
1290 assertEquals(42, this[ownReadOnly]); | |
1291 super[ownReadOnly] = 55; | |
1292 assertEquals(42, this[ownReadOnly]); | |
1293 var descr = Object.getOwnPropertyDescriptor(this, ownReadOnly); | |
1294 assertEquals(42, descr.value); | |
1295 assertFalse(descr.configurable); | |
1296 assertFalse(descr.enumerable); | |
1297 assertFalse(descr.writable); | |
1298 assertFalse(Base.prototype.hasOwnProperty(ownReadOnly)); | |
1299 | |
1300 assertEquals(15, this[ownReadonlyAccessor]); | |
1301 super[ownReadonlyAccessor] = 25; | |
1302 assertEquals(15, this[ownReadonlyAccessor]); | |
1303 var descr = Object.getOwnPropertyDescriptor(this, ownReadonlyAccessor); | |
1304 assertFalse(descr.configurable); | |
1305 assertFalse(descr.enumerable); | |
1306 assertFalse(Base.prototype.hasOwnProperty(ownReadonlyAccessor)); | |
1307 | |
1308 super[ownSetter] = 35; | |
1309 var descr = Object.getOwnPropertyDescriptor(this, ownSetter); | |
1310 assertFalse(descr.configurable); | |
1311 assertFalse(descr.enumerable); | |
1312 assertFalse(Base.prototype.hasOwnProperty(ownSetter)); | |
1313 }, | |
1314 mStrict() { | |
1315 'use strict'; | |
1316 var ex; | |
1317 assertEquals(42, this[ownReadOnly]); | |
1318 try { | |
1319 super[ownReadOnly] = 55; | |
1320 } catch (e) { | |
1321 ex = e; | |
1322 } | |
1323 assertInstanceof(ex, TypeError); | |
1324 assertEquals( | |
1325 "Cannot assign to read only property '" + ownReadOnly + | |
1326 "' of object '#<Base>'", | |
1327 ex.message); | |
1328 assertEquals(42, this[ownReadOnly]); | |
1329 | |
1330 ex = null; | |
1331 assertEquals(15, this[ownReadonlyAccessor]); | |
1332 try { | |
1333 super[ownReadonlyAccessor] = 25; | |
1334 } catch (e) { | |
1335 ex = e; | |
1336 } | |
1337 assertInstanceof(ex, TypeError); | |
1338 assertEquals('Cannot redefine property: ' + ownReadonlyAccessor, | |
1339 ex.message); | |
1340 assertEquals(15, this[ownReadonlyAccessor]); | |
1341 | |
1342 ex = null; | |
1343 try { | |
1344 super[ownSetter] = 35; | |
1345 } catch (e) { | |
1346 ex = e; | |
1347 } | |
1348 assertInstanceof(ex, TypeError); | |
1349 assertEquals('Cannot redefine property: ' + ownSetter, ex.message); | |
1350 } | |
1351 }; | |
1352 | |
1353 var d = new Derived(); | |
1354 Object.defineProperty(d, ownReadOnly, { value : 42, writable : false }); | |
1355 Object.defineProperty(d, ownSetter, | |
1356 { set : function() { assertUnreachable(); } }); | |
1357 Object.defineProperty(d, ownReadonlyAccessor, | |
1358 { get : function() { return 15; }}); | |
1359 d.mSloppy(); | |
1360 d.mStrict(); | |
1361 } | |
1362 TestKeyedSetterCreatingOwnPropertiesNonConfigurable('ownReadOnly', | |
1363 'ownReadonlyAccessor', 'ownSetter'); | |
1364 TestKeyedSetterCreatingOwnPropertiesNonConfigurable(42, 43, 44); | |
1365 | |
1366 | |
1367 (function TestSetterNoProtoWalk() { | |
1368 function Base() {} | |
1369 function Derived() {} | |
1370 var getCalled; | |
1371 var setCalled; | |
1372 Derived.prototype = { | |
1373 __proto__: Base.prototype, | |
1374 get x() { getCalled++; return 42; }, | |
1375 set x(v) { setCalled++; }, | |
1376 mSloppy() { | |
1377 setCalled = 0; | |
1378 getCalled = 0; | |
1379 assertEquals(42, this.x); | |
1380 assertEquals(1, getCalled); | |
1381 assertEquals(0, setCalled); | |
1382 | |
1383 getCalled = 0; | |
1384 setCalled = 0; | |
1385 this.x = 43; | |
1386 assertEquals(0, getCalled); | |
1387 assertEquals(1, setCalled); | |
1388 | |
1389 getCalled = 0; | |
1390 setCalled = 0; | |
1391 super.x = 15; | |
1392 assertEquals(0, setCalled); | |
1393 assertEquals(0, getCalled); | |
1394 | |
1395 assertEquals(15, this.x); | |
1396 assertEquals(0, getCalled); | |
1397 assertEquals(0, setCalled); | |
1398 }, | |
1399 mStrict() { | |
1400 'use strict'; | |
1401 setCalled = 0; | |
1402 getCalled = 0; | |
1403 assertEquals(42, this.x); | |
1404 assertEquals(1, getCalled); | |
1405 assertEquals(0, setCalled); | |
1406 | |
1407 getCalled = 0; | |
1408 setCalled = 0; | |
1409 this.x = 43; | |
1410 assertEquals(0, getCalled); | |
1411 assertEquals(1, setCalled); | |
1412 | |
1413 getCalled = 0; | |
1414 setCalled = 0; | |
1415 super.x = 15; | |
1416 assertEquals(0, setCalled); | |
1417 assertEquals(0, getCalled); | |
1418 | |
1419 assertEquals(15, this.x); | |
1420 assertEquals(0, getCalled); | |
1421 assertEquals(0, setCalled); | |
1422 } | |
1423 }; | |
1424 | |
1425 new Derived().mSloppy(); | |
1426 new Derived().mStrict(); | |
1427 }()); | |
1428 | |
1429 | |
1430 (function TestKeyedSetterNoProtoWalk() { | |
1431 var x = 'x'; | |
1432 function Base() {} | |
1433 function Derived() {} | |
1434 var getCalled; | |
1435 var setCalled; | |
1436 Derived.prototype = { | |
1437 __proto__: Base.prototype, | |
1438 get x() { getCalled++; return 42; }, | |
1439 set x(v) { setCalled++; }, | |
1440 mSloppy() { | |
1441 setCalled = 0; | |
1442 getCalled = 0; | |
1443 assertEquals(42, this[x]); | |
1444 assertEquals(1, getCalled); | |
1445 assertEquals(0, setCalled); | |
1446 | |
1447 getCalled = 0; | |
1448 setCalled = 0; | |
1449 this[x] = 43; | |
1450 assertEquals(0, getCalled); | |
1451 assertEquals(1, setCalled); | |
1452 | |
1453 getCalled = 0; | |
1454 setCalled = 0; | |
1455 super[x] = 15; | |
1456 assertEquals(0, setCalled); | |
1457 assertEquals(0, getCalled); | |
1458 | |
1459 assertEquals(15, this[x]); | |
1460 assertEquals(0, getCalled); | |
1461 assertEquals(0, setCalled); | |
1462 }, | |
1463 mStrict() { | |
1464 'use strict'; | |
1465 setCalled = 0; | |
1466 getCalled = 0; | |
1467 assertEquals(42, this[x]); | |
1468 assertEquals(1, getCalled); | |
1469 assertEquals(0, setCalled); | |
1470 | |
1471 getCalled = 0; | |
1472 setCalled = 0; | |
1473 this[x] = 43; | |
1474 assertEquals(0, getCalled); | |
1475 assertEquals(1, setCalled); | |
1476 | |
1477 getCalled = 0; | |
1478 setCalled = 0; | |
1479 super[x] = 15; | |
1480 assertEquals(0, setCalled); | |
1481 assertEquals(0, getCalled); | |
1482 | |
1483 assertEquals(15, this[x]); | |
1484 assertEquals(0, getCalled); | |
1485 assertEquals(0, setCalled); | |
1486 } | |
1487 }; | |
1488 | |
1489 new Derived().mSloppy(); | |
1490 new Derived().mStrict(); | |
1491 }()); | |
1492 | |
1493 | |
1494 (function TestKeyedNumericSetterNoProtoWalk() { | |
1495 var x = 42; | |
1496 function Base() {} | |
1497 function Derived() {} | |
1498 var getCalled; | |
1499 var setCalled; | |
1500 Derived.prototype = { | |
1501 __proto__: Base.prototype, | |
1502 mSloppy() { | |
1503 setCalled = 0; | |
1504 getCalled = 0; | |
1505 assertEquals(42, this[x]); | |
1506 assertEquals(1, getCalled); | |
1507 assertEquals(0, setCalled); | |
1508 | |
1509 getCalled = 0; | |
1510 setCalled = 0; | |
1511 this[x] = 43; | |
1512 assertEquals(0, getCalled); | |
1513 assertEquals(1, setCalled); | |
1514 | |
1515 getCalled = 0; | |
1516 setCalled = 0; | |
1517 super[x] = 15; | |
1518 assertEquals(0, setCalled); | |
1519 assertEquals(0, getCalled); | |
1520 | |
1521 assertEquals(15, this[x]); | |
1522 assertEquals(0, getCalled); | |
1523 assertEquals(0, setCalled); | |
1524 }, | |
1525 mStrict() { | |
1526 'use strict'; | |
1527 setCalled = 0; | |
1528 getCalled = 0; | |
1529 assertEquals(42, this[x]); | |
1530 assertEquals(1, getCalled); | |
1531 assertEquals(0, setCalled); | |
1532 | |
1533 getCalled = 0; | |
1534 setCalled = 0; | |
1535 this[x] = 43; | |
1536 assertEquals(0, getCalled); | |
1537 assertEquals(1, setCalled); | |
1538 | |
1539 getCalled = 0; | |
1540 setCalled = 0; | |
1541 super[x] = 15; | |
1542 assertEquals(0, setCalled); | |
1543 assertEquals(0, getCalled); | |
1544 | |
1545 assertEquals(15, this[x]); | |
1546 assertEquals(0, getCalled); | |
1547 assertEquals(0, setCalled); | |
1548 } | |
1549 }; | |
1550 | |
1551 Object.defineProperty(Derived.prototype, x, { | |
1552 get: function() { getCalled++; return 42; }, | |
1553 set: function(v) { setCalled++; } | |
1554 }); | |
1555 | |
1556 new Derived().mSloppy(); | |
1557 new Derived().mStrict(); | |
1558 }()); | |
1559 | |
1560 | |
1561 (function TestSetterDoesNotReconfigure() { | |
1562 function Base() {} | |
1563 function Derived() {} | |
1564 Derived.prototype = { | |
1565 __proto__: Derived.prototype, | |
1566 mStrict(){ | |
1567 'use strict'; | |
1568 super.nonEnumConfig = 5; | |
1569 var d1 = Object.getOwnPropertyDescriptor(this, 'nonEnumConfig'); | |
1570 assertEquals(5, d1.value); | |
1571 assertTrue(d1.configurable); | |
1572 assertFalse(d1.enumerable); | |
1573 | |
1574 super.nonEnumNonConfig = 5; | |
1575 var d1 = Object.getOwnPropertyDescriptor(this, 'nonEnumNonConfig'); | |
1576 assertEquals(5, d1.value); | |
1577 assertFalse(d1.configurable); | |
1578 assertFalse(d1.enumerable); | |
1579 }, | |
1580 mSloppy(){ | |
1581 super.nonEnumConfig = 42; | |
1582 var d1 = Object.getOwnPropertyDescriptor(this, 'nonEnumConfig'); | |
1583 assertEquals(42, d1.value); | |
1584 assertTrue(d1.configurable); | |
1585 assertFalse(d1.enumerable); | |
1586 | |
1587 super.nonEnumNonConfig = 42; | |
1588 var d1 = Object.getOwnPropertyDescriptor(this, 'nonEnumNonConfig'); | |
1589 assertEquals(42, d1.value); | |
1590 assertFalse(d1.configurable); | |
1591 assertFalse(d1.enumerable); | |
1592 } | |
1593 }; | |
1594 | |
1595 var d = new Derived(); | |
1596 Object.defineProperty(d, 'nonEnumConfig', | |
1597 { value : 0, enumerable : false, configurable : true, writable : true }); | |
1598 Object.defineProperty(d, 'nonEnumNonConfig', | |
1599 { value : 0, enumerable : false, configurable : false, writable : true }); | |
1600 d.mStrict(); | |
1601 d.mSloppy(); | |
1602 }()); | |
1603 | |
1604 | |
1605 (function TestKeyedSetterDoesNotReconfigure() { | |
1606 var nonEnumConfig = 'nonEnumConfig'; | |
1607 var nonEnumNonConfig = 'nonEnumNonConfig'; | |
1608 function Base() {} | |
1609 function Derived() {} | |
1610 | |
1611 Derived.prototype = { | |
1612 __proto__: Base.prototype, | |
1613 mStrict(){ | |
1614 'use strict'; | |
1615 super[nonEnumConfig] = 5; | |
1616 var d1 = Object.getOwnPropertyDescriptor(this, nonEnumConfig); | |
1617 assertEquals(5, d1.value); | |
1618 assertTrue(d1.configurable); | |
1619 assertFalse(d1.enumerable); | |
1620 | |
1621 super[nonEnumNonConfig] = 5; | |
1622 var d1 = Object.getOwnPropertyDescriptor(this, nonEnumNonConfig); | |
1623 assertEquals(5, d1.value); | |
1624 assertFalse(d1.configurable); | |
1625 assertFalse(d1.enumerable); | |
1626 }, | |
1627 mSloppy(){ | |
1628 super[nonEnumConfig] = 42; | |
1629 var d1 = Object.getOwnPropertyDescriptor(this, nonEnumConfig); | |
1630 assertEquals(42, d1.value); | |
1631 assertTrue(d1.configurable); | |
1632 assertFalse(d1.enumerable); | |
1633 | |
1634 super[nonEnumNonConfig] = 42; | |
1635 var d1 = Object.getOwnPropertyDescriptor(this, nonEnumNonConfig); | |
1636 assertEquals(42, d1.value); | |
1637 assertFalse(d1.configurable); | |
1638 assertFalse(d1.enumerable); | |
1639 } | |
1640 }; | |
1641 | |
1642 var d = new Derived(); | |
1643 Object.defineProperty(d, nonEnumConfig, | |
1644 { value : 0, enumerable : false, configurable : true, writable : true }); | |
1645 Object.defineProperty(d, nonEnumNonConfig, | |
1646 { value : 0, enumerable : false, configurable : false, writable : true }); | |
1647 d.mStrict(); | |
1648 d.mSloppy(); | |
1649 }()); | |
1650 | |
1651 | |
1652 (function TestKeyedNumericSetterDoesNotReconfigure() { | |
1653 var nonEnumConfig = 42; | |
1654 var nonEnumNonConfig = 43; | |
1655 function Base() {} | |
1656 function Derived() {} | |
1657 | |
1658 Derived.prototype = { | |
1659 __proto__: Base.prototype, | |
1660 mStrict(){ | |
1661 'use strict'; | |
1662 super[nonEnumConfig] = 5; | |
1663 var d1 = Object.getOwnPropertyDescriptor(this, nonEnumConfig); | |
1664 assertEquals(5, d1.value); | |
1665 assertTrue(d1.configurable); | |
1666 assertFalse(d1.enumerable); | |
1667 | |
1668 super[nonEnumNonConfig] = 5; | |
1669 var d1 = Object.getOwnPropertyDescriptor(this, nonEnumNonConfig); | |
1670 assertEquals(5, d1.value); | |
1671 assertFalse(d1.configurable); | |
1672 assertFalse(d1.enumerable); | |
1673 }, | |
1674 mSloppy(){ | |
1675 super[nonEnumConfig] = 42; | |
1676 var d1 = Object.getOwnPropertyDescriptor(this, nonEnumConfig); | |
1677 assertEquals(42, d1.value); | |
1678 assertTrue(d1.configurable); | |
1679 assertFalse(d1.enumerable); | |
1680 | |
1681 super[nonEnumNonConfig] = 42; | |
1682 var d1 = Object.getOwnPropertyDescriptor(this, nonEnumNonConfig); | |
1683 assertEquals(42, d1.value); | |
1684 assertFalse(d1.configurable); | |
1685 assertFalse(d1.enumerable); | |
1686 } | |
1687 }; | |
1688 | |
1689 var d = new Derived(); | |
1690 Object.defineProperty(d, nonEnumConfig, | |
1691 { value : 0, enumerable : false, configurable : true, writable : true }); | |
1692 Object.defineProperty(d, nonEnumNonConfig, | |
1693 { value : 0, enumerable : false, configurable : false, writable : true }); | |
1694 d.mStrict(); | |
1695 d.mSloppy(); | |
1696 }()); | |
1697 | |
1698 | |
1699 (function TestCountOperations() { | |
1700 function Base() {} | |
1701 Base.prototype = { | |
1702 constructor: Base, | |
1703 get x() { | |
1704 return this._x; | |
1705 }, | |
1706 set x(v) { | |
1707 this._x = v; | |
1708 }, | |
1709 _x: 1 | |
1710 }; | |
1711 | |
1712 function Derived() {} | |
1713 Derived.__proto__ = Base; | |
1714 Derived.prototype = { | |
1715 __proto__: Base.prototype, | |
1716 constructor: Derived, | |
1717 _x: 2, | |
1718 testCounts() { | |
1719 assertEquals(2, this._x); | |
1720 assertEquals(2, super.x); | |
1721 super.x++; | |
1722 assertEquals(3, super.x); | |
1723 ++super.x; | |
1724 assertEquals(4, super.x); | |
1725 assertEquals(4, super.x++); | |
1726 assertEquals(5, super.x); | |
1727 assertEquals(6, ++super.x); | |
1728 assertEquals(6, super.x); | |
1729 assertEquals(6, this._x); | |
1730 | |
1731 super.x--; | |
1732 assertEquals(5, super.x); | |
1733 --super.x; | |
1734 assertEquals(4, super.x); | |
1735 assertEquals(4, super.x--); | |
1736 assertEquals(3, super.x); | |
1737 assertEquals(2, --super.x); | |
1738 assertEquals(2, super.x); | |
1739 assertEquals(2, this._x); | |
1740 } | |
1741 }; | |
1742 new Derived().testCounts(); | |
1743 }()); | |
1744 | |
1745 | |
1746 (function TestKeyedCountOperations() { | |
1747 var x = 'x'; | |
1748 function Base() {} | |
1749 Base.prototype = { | |
1750 constructor: Base, | |
1751 get x() { | |
1752 return this._x; | |
1753 }, | |
1754 set x(v) { | |
1755 this._x = v; | |
1756 }, | |
1757 _x: 1 | |
1758 }; | |
1759 | |
1760 function Derived() {} | |
1761 Derived.__proto__ = Base; | |
1762 Derived.prototype = { | |
1763 __proto__: Base.prototype, | |
1764 constructor: Derived, | |
1765 _x: 2, | |
1766 testCounts() { | |
1767 assertEquals(2, this._x); | |
1768 assertEquals(2, super[x]); | |
1769 super[x]++; | |
1770 assertEquals(3, super[x]); | |
1771 ++super[x]; | |
1772 assertEquals(4, super[x]); | |
1773 assertEquals(4, super[x]++); | |
1774 assertEquals(5, super[x]); | |
1775 assertEquals(6, ++super[x]); | |
1776 assertEquals(6, super[x]); | |
1777 assertEquals(6, this._x); | |
1778 | |
1779 super[x]--; | |
1780 assertEquals(5, super[x]); | |
1781 --super[x]; | |
1782 assertEquals(4, super[x]); | |
1783 assertEquals(4, super[x]--); | |
1784 assertEquals(3, super[x]); | |
1785 assertEquals(2, --super[x]); | |
1786 assertEquals(2, super[x]); | |
1787 assertEquals(2, this._x); | |
1788 } | |
1789 }; | |
1790 new Derived().testCounts(); | |
1791 }()); | |
1792 | |
1793 | |
1794 (function TestKeyedNumericCountOperations() { | |
1795 var x = 42; | |
1796 function Base() {} | |
1797 Base.prototype = { | |
1798 constructor: Base, | |
1799 _x: 1 | |
1800 }; | |
1801 | |
1802 Object.defineProperty(Base.prototype, x, { | |
1803 get: function() { return this._x; }, | |
1804 set: function(v) { this._x = v;; } | |
1805 }); | |
1806 | |
1807 function Derived() {} | |
1808 Derived.__proto__ = Base; | |
1809 Derived.prototype = { | |
1810 __proto__: Base.prototype, | |
1811 constructor: Derived, | |
1812 _x: 2, | |
1813 testCounts() { | |
1814 assertEquals(2, this._x); | |
1815 assertEquals(2, super[x]); | |
1816 super[x]++; | |
1817 assertEquals(3, super[x]); | |
1818 ++super[x]; | |
1819 assertEquals(4, super[x]); | |
1820 assertEquals(4, super[x]++); | |
1821 assertEquals(5, super[x]); | |
1822 assertEquals(6, ++super[x]); | |
1823 assertEquals(6, super[x]); | |
1824 assertEquals(6, this._x); | |
1825 | |
1826 super[x]--; | |
1827 assertEquals(5, super[x]); | |
1828 --super[x]; | |
1829 assertEquals(4, super[x]); | |
1830 assertEquals(4, super[x]--); | |
1831 assertEquals(3, super[x]); | |
1832 assertEquals(2, --super[x]); | |
1833 assertEquals(2, super[x]); | |
1834 assertEquals(2, this._x); | |
1835 } | |
1836 }; | |
1837 new Derived().testCounts(); | |
1838 }()); | |
1839 | |
1840 | |
1841 (function TestSetterSuperNonWritable() { | |
1842 function Base() {} | |
1843 Object.defineProperty(Base.prototype, 'x', { value : 27, writable: false }); | |
1844 function Derived() {} | |
1845 Derived.prototype = { | |
1846 __proto__: Base.prototype, | |
1847 constructor: Derived, | |
1848 mSloppy() { | |
1849 assertEquals(27, super.x); | |
1850 assertEquals(27, this.x); | |
1851 super.x = 10; | |
1852 assertEquals(27, super.x); | |
1853 assertEquals(27, this.x); | |
1854 }, | |
1855 mStrict() { | |
1856 'use strict'; | |
1857 assertEquals(27, super.x); | |
1858 assertEquals(27, this.x); | |
1859 var ex = null; | |
1860 try { super.x = 10; } catch(e) { ex = e; } | |
1861 assertInstanceof(ex, TypeError); | |
1862 assertEquals(27, super.x); | |
1863 assertEquals(27, this.x); | |
1864 } | |
1865 }; | |
1866 new Derived().mSloppy(); | |
1867 new Derived().mStrict(); | |
1868 }()); | |
1869 | |
1870 | |
1871 (function TestSetterKeyedSuperNonWritable() { | |
1872 var x = 'xyz'; | |
1873 function Base() {} | |
1874 Object.defineProperty(Base.prototype, x, { value : 27, writable: false }); | |
1875 function Derived() {} | |
1876 | |
1877 Derived.prototype = { | |
1878 __proto__: Base.prototype, | |
1879 constructor: Derived, | |
1880 mSloppy() { | |
1881 assertEquals(27, super[x]); | |
1882 assertEquals(27, this[x]); | |
1883 super[x] = 10; | |
1884 assertEquals(27, super[x]); | |
1885 assertEquals(27, this[x]); | |
1886 }, | |
1887 mStrict() { | |
1888 'use strict'; | |
1889 assertEquals(27, super[x]); | |
1890 assertEquals(27, this[x]); | |
1891 var ex = null; | |
1892 try { super[x] = 10; } catch(e) { ex = e; } | |
1893 assertInstanceof(ex, TypeError); | |
1894 assertEquals(27, super[x]); | |
1895 assertEquals(27, this[x]); | |
1896 } | |
1897 }; | |
1898 new Derived().mSloppy(); | |
1899 new Derived().mStrict(); | |
1900 }()); | |
1901 | |
1902 | |
1903 (function TestSetterKeyedNumericSuperNonWritable() { | |
1904 var x = 42; | |
1905 function Base() {} | |
1906 Object.defineProperty(Base.prototype, x, { value : 27, writable: false }); | |
1907 function Derived() {} | |
1908 | |
1909 Derived.prototype = { | |
1910 __proto__: Base.prototype, | |
1911 constructor: Derived, | |
1912 mSloppy() { | |
1913 assertEquals(27, super[x]); | |
1914 assertEquals(27, this[x]); | |
1915 super[x] = 10; | |
1916 assertEquals(27, super[x]); | |
1917 assertEquals(27, this[x]); | |
1918 }, | |
1919 mStrict() { | |
1920 'use strict'; | |
1921 assertEquals(27, super[x]); | |
1922 assertEquals(27, this[x]); | |
1923 var ex = null; | |
1924 try { super[x] = 10; } catch(e) { ex = e; } | |
1925 assertInstanceof(ex, TypeError); | |
1926 assertEquals(27, super[x]); | |
1927 assertEquals(27, this[x]); | |
1928 } | |
1929 }; | |
1930 new Derived().mSloppy(); | |
1931 new Derived().mStrict(); | |
1932 }()); | |
1933 | |
1934 | |
1935 (function TestSuperCall() { | |
1936 'use strict'; | |
1937 | |
1938 var baseCalled = 0; | |
1939 var derivedCalled = 0; | |
1940 var derivedDerivedCalled = 0; | |
1941 | |
1942 class Base { | |
1943 constructor() { | |
1944 baseCalled++; | |
1945 } | |
1946 } | |
1947 | |
1948 class Derived extends Base { | |
1949 constructor() { | |
1950 let r = super(); | |
1951 assertEquals(this, r); | |
1952 derivedCalled++; | |
1953 } | |
1954 } | |
1955 | |
1956 assertEquals(Base, Base.prototype.constructor); | |
1957 assertEquals(Base.prototype, Derived.prototype.__proto__); | |
1958 | |
1959 baseCalled = 0; | |
1960 derivedCalled = 0; | |
1961 new Derived(); | |
1962 assertEquals(1, baseCalled); | |
1963 assertEquals(1, derivedCalled); | |
1964 | |
1965 class DerivedDerived extends Derived { | |
1966 constructor() { | |
1967 let r = super(); | |
1968 assertEquals(this, r); | |
1969 derivedDerivedCalled++; | |
1970 } | |
1971 } | |
1972 | |
1973 baseCalled = 0; | |
1974 derivedCalled = 0; | |
1975 derivedDerivedCalled = 0; | |
1976 new DerivedDerived(); | |
1977 assertEquals(1, baseCalled); | |
1978 assertEquals(1, derivedCalled); | |
1979 assertEquals(1, derivedDerivedCalled); | |
1980 | |
1981 class Base2 { | |
1982 constructor(v) { | |
1983 this.fromBase = v; | |
1984 } | |
1985 } | |
1986 class Derived2 extends Base2 { | |
1987 constructor(v1, v2) { | |
1988 let r = super(v1); | |
1989 assertEquals(this, r); | |
1990 this.fromDerived = v2; | |
1991 } | |
1992 } | |
1993 | |
1994 var d = new Derived2("base", "derived"); | |
1995 assertEquals("base", d.fromBase); | |
1996 assertEquals("derived", d.fromDerived); | |
1997 | |
1998 var calls = 0; | |
1999 class G { | |
2000 constructor() { | |
2001 calls++; | |
2002 } | |
2003 } | |
2004 | |
2005 class F extends Object { | |
2006 constructor() { | |
2007 super(); | |
2008 } | |
2009 } | |
2010 F.__proto__ = G; | |
2011 new F(); | |
2012 assertEquals(1, calls); | |
2013 F.__proto__ = function() {}; | |
2014 new F(); | |
2015 assertEquals(1, calls); | |
2016 }()); | |
2017 | |
2018 | |
2019 (function TestExtendsObject() { | |
2020 'use strict'; | |
2021 class F extends Object { } | |
2022 var f = new F(42); | |
2023 | |
2024 // TODO(dslomov,arv): Fix this. BUG=v8:3886. | |
2025 assertInstanceof(f, Number); | |
2026 }()); | |
2027 | |
2028 | |
2029 (function TestSuperCallErrorCases() { | |
2030 'use strict'; | |
2031 class T extends Object { | |
2032 constructor() { | |
2033 super(); | |
2034 } | |
2035 } | |
2036 | |
2037 T.__proto__ = null; | |
2038 assertThrows(function() { new T(); }, TypeError); | |
2039 }()); | |
2040 | |
2041 | |
2042 (function TestSuperPropertyInEval() { | |
2043 'use strict'; | |
2044 let y = 3; | |
2045 class Base { | |
2046 m() { return 1; } | |
2047 get x() { return 2; } | |
2048 } | |
2049 class Derived extends Base { | |
2050 evalM() { | |
2051 assertEquals(1, eval('super.m()')); | |
2052 } | |
2053 evalX() { | |
2054 assertEquals(2, eval('super.x')); | |
2055 } | |
2056 globalEval1() { | |
2057 assertThrows('super.x', SyntaxError); | |
2058 assertThrows('super.m()', SyntaxError); | |
2059 } | |
2060 globalEval2() { | |
2061 super.x; | |
2062 assertThrows('super.x', SyntaxError); | |
2063 assertThrows('super.m()', SyntaxError); | |
2064 } | |
2065 } | |
2066 let d = new Derived(); | |
2067 d.globalEval1(); | |
2068 d.globalEval2(); | |
2069 d.evalM(); | |
2070 d.evalX(); | |
2071 })(); | |
2072 | |
2073 | |
2074 (function TestSuperPropertyInArrow() { | |
2075 'use strict'; | |
2076 let y = 3; | |
2077 class Base { | |
2078 m() { return 1; } | |
2079 get x() { return 2; } | |
2080 } | |
2081 class Derived extends Base { | |
2082 arrow() { | |
2083 assertSame(super.x, (() => super.x)()); | |
2084 assertSame(super.m(), (() => super.m())()); | |
2085 return (() => super.m())(); | |
2086 } | |
2087 } | |
2088 let d = new Derived(); | |
2089 assertSame(1, d.arrow()); | |
2090 })(); | |
2091 | |
2092 | |
2093 (function TestSuperInOtherScopes() { | |
2094 var p = {x: 99}; | |
2095 var o0 = {__proto__: p, f() { return eval("'use strict'; super.x") }}; | |
2096 assertEquals(p.x, o0.f()); | |
2097 var o1 = {__proto__: p, f() { with ({}) return super.x }}; | |
2098 assertEquals(p.x, o1.f()); | |
2099 var o2 = {__proto__: p, f({a}) { return super.x }}; | |
2100 assertEquals(p.x, o2.f({})); | |
2101 var o3 = {__proto__: p, f(...a) { return super.x }}; | |
2102 assertEquals(p.x, o3.f()); | |
2103 var o4 = {__proto__: p, f() { 'use strict'; { let x; return super.x } }}; | |
2104 assertEquals(p.x, o4.f()); | |
2105 })(); | |
2106 | |
2107 | |
2108 (function TestSuperCallInOtherScopes() { | |
2109 class C {constructor() { this.x = 99 }} | |
2110 class D0 extends C {constructor() { eval("'use strict'; super()") }} | |
2111 assertEquals(99, (new D0).x); | |
2112 class D2 extends C {constructor({a}) { super() }} | |
2113 assertEquals(99, (new D2({})).x); | |
2114 class D3 extends C {constructor(...a) { super() }} | |
2115 assertEquals(99, (new D3()).x); | |
2116 class D4 extends C {constructor() { { let x; super() } }} | |
2117 assertEquals(99, (new D4).x); | |
2118 })(); | |
2119 | |
2120 | |
2121 (function TestSuperCallInEval() { | |
2122 'use strict'; | |
2123 class Base { | |
2124 constructor(x) { | |
2125 this.x = x; | |
2126 } | |
2127 } | |
2128 class Derived extends Base { | |
2129 constructor(x) { | |
2130 let r = eval('super(x)'); | |
2131 assertEquals(this, r); | |
2132 } | |
2133 } | |
2134 let d = new Derived(42); | |
2135 assertSame(42, d.x); | |
2136 })(); | |
2137 | |
2138 | |
2139 (function TestSuperCallInArrow() { | |
2140 'use strict'; | |
2141 class Base { | |
2142 constructor(x) { | |
2143 this.x = x; | |
2144 } | |
2145 } | |
2146 class Derived extends Base { | |
2147 constructor(x) { | |
2148 let r = (() => super(x))(); | |
2149 assertEquals(this, r); | |
2150 } | |
2151 } | |
2152 let d = new Derived(42); | |
2153 assertSame(42, d.x); | |
2154 })(); | |
2155 | |
2156 | |
2157 (function TestSuperCallEscapes() { | |
2158 'use strict'; | |
2159 class Base { | |
2160 constructor(x) { | |
2161 this.x = x; | |
2162 } | |
2163 } | |
2164 | |
2165 let f; | |
2166 class Derived extends Base { | |
2167 constructor() { | |
2168 f = () => super(2); | |
2169 } | |
2170 } | |
2171 assertThrows(function() { | |
2172 new Derived(); | |
2173 }, ReferenceError); | |
2174 | |
2175 let o = f(); | |
2176 assertEquals(2, o.x); | |
2177 assertInstanceof(o, Derived); | |
2178 | |
2179 assertThrows(function() { | |
2180 f(); | |
2181 }, ReferenceError); | |
2182 })(); | |
2183 | |
2184 | |
2185 (function TestSuperCallInLoop() { | |
2186 'use strict'; | |
2187 class Base { | |
2188 constructor(x) { | |
2189 this.x = x; | |
2190 } | |
2191 } | |
2192 class Derived extends Base { | |
2193 constructor(x, n) { | |
2194 for (var i = 0; i < n; ++i) { | |
2195 super(x); | |
2196 } | |
2197 } | |
2198 } | |
2199 | |
2200 let o = new Derived(23, 1); | |
2201 assertEquals(23, o.x); | |
2202 assertInstanceof(o, Derived); | |
2203 | |
2204 assertThrows("new Derived(42, 0)", ReferenceError); | |
2205 assertThrows("new Derived(65, 2)", ReferenceError); | |
2206 })(); | |
2207 | |
2208 | |
2209 (function TestSuperCallReentrant() { | |
2210 'use strict'; | |
2211 class Base { | |
2212 constructor(fun) { | |
2213 this.x = fun(); | |
2214 } | |
2215 } | |
2216 class Derived extends Base { | |
2217 constructor(x) { | |
2218 let f = () => super(() => x) | |
2219 super(f); | |
2220 } | |
2221 } | |
2222 assertThrows("new Derived(23)", ReferenceError); | |
2223 })(); | |
2224 | |
2225 | |
2226 (function TestSuperCallSpreadInEval() { | |
2227 'use strict'; | |
2228 class Base { | |
2229 constructor(x) { | |
2230 this.x = x; | |
2231 } | |
2232 } | |
2233 class Derived extends Base { | |
2234 constructor(x) { | |
2235 let r = eval('super(...[x])'); | |
2236 assertEquals(this, r); | |
2237 } | |
2238 } | |
2239 let d = new Derived(42); | |
2240 assertSame(42, d.x); | |
2241 })(); | |
2242 | |
2243 | |
2244 (function TestSuperCallSpreadInArrow() { | |
2245 'use strict'; | |
2246 class Base { | |
2247 constructor(x) { | |
2248 this.x = x; | |
2249 } | |
2250 } | |
2251 class Derived extends Base { | |
2252 constructor(x) { | |
2253 let r = (() => super(...[x]))(); | |
2254 assertEquals(this, r); | |
2255 } | |
2256 } | |
2257 let d = new Derived(42); | |
2258 assertSame(42, d.x); | |
2259 })(); | |
OLD | NEW |