OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --harmony-sloppy | 5 // Flags: --harmony-sloppy --allow-natives-syntax |
6 | 6 |
7 (function TestBasics() { | 7 (function TestBasics() { |
8 var C = class C {} | 8 var C = class C {} |
9 assertEquals(typeof C, 'function'); | 9 assertEquals(typeof C, 'function'); |
10 assertEquals(C.__proto__, Function.prototype); | 10 assertEquals(C.__proto__, Function.prototype); |
11 assertEquals(Object.prototype, Object.getPrototypeOf(C.prototype)); | 11 assertEquals(Object.prototype, Object.getPrototypeOf(C.prototype)); |
12 assertEquals(Function.prototype, Object.getPrototypeOf(C)); | 12 assertEquals(Function.prototype, Object.getPrototypeOf(C)); |
13 assertEquals('C', C.name); | 13 assertEquals('C', C.name); |
14 | 14 |
15 class D {} | 15 class D {} |
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
618 | 618 |
619 (function TestDefaultConstructorNoCrash() { | 619 (function TestDefaultConstructorNoCrash() { |
620 // Regression test for https://code.google.com/p/v8/issues/detail?id=3661 | 620 // Regression test for https://code.google.com/p/v8/issues/detail?id=3661 |
621 class C {} | 621 class C {} |
622 assertThrows(function () {C();}, TypeError); | 622 assertThrows(function () {C();}, TypeError); |
623 assertThrows(function () {C(1);}, TypeError); | 623 assertThrows(function () {C(1);}, TypeError); |
624 assertTrue(new C() instanceof C); | 624 assertTrue(new C() instanceof C); |
625 assertTrue(new C(1) instanceof C); | 625 assertTrue(new C(1) instanceof C); |
626 })(); | 626 })(); |
627 | 627 |
| 628 |
628 (function TestConstructorCall(){ | 629 (function TestConstructorCall(){ |
629 var realmIndex = Realm.create(); | 630 var realmIndex = Realm.create(); |
630 var otherTypeError = Realm.eval(realmIndex, "TypeError"); | 631 var otherTypeError = Realm.eval(realmIndex, "TypeError"); |
631 var A = Realm.eval(realmIndex, '"use strict"; class A {}'); | 632 var A = Realm.eval(realmIndex, '"use strict"; class A {}'); |
632 var instance = new A(); | 633 var instance = new A(); |
633 var constructor = instance.constructor; | 634 var constructor = instance.constructor; |
634 var otherTypeError = Realm.eval(realmIndex, 'TypeError'); | 635 var otherTypeError = Realm.eval(realmIndex, 'TypeError'); |
635 if (otherTypeError === TypeError) { | 636 if (otherTypeError === TypeError) { |
636 throw Error('Should not happen!'); | 637 throw Error('Should not happen!'); |
637 } | 638 } |
638 | 639 |
639 // ES6 9.2.1[[Call]] throws a TypeError in the caller context/Realm when the | 640 // ES6 9.2.1[[Call]] throws a TypeError in the caller context/Realm when the |
640 // called function is a classConstructor | 641 // called function is a classConstructor |
641 assertThrows(function() { Realm.eval(realmIndex, "A()") }, otherTypeError); | 642 assertThrows(function() { Realm.eval(realmIndex, "A()") }, otherTypeError); |
642 assertThrows(function() { instance.constructor() }, TypeError); | 643 assertThrows(function() { instance.constructor() }, TypeError); |
643 assertThrows(function() { A() }, TypeError); | 644 assertThrows(function() { A() }, TypeError); |
644 | 645 |
645 // ES6 9.3.1 call() first activates the callee context before invoking the | 646 // ES6 9.3.1 call() first activates the callee context before invoking the |
646 // method. The TypeError from the constructor is thus thrown in the other | 647 // method. The TypeError from the constructor is thus thrown in the other |
647 // Realm. | 648 // Realm. |
648 assertThrows(function() { Realm.eval(realmIndex, "A.call()") }, | 649 assertThrows(function() { Realm.eval(realmIndex, "A.call()") }, |
649 otherTypeError); | 650 otherTypeError); |
650 assertThrows(function() { constructor.call() }, otherTypeError); | 651 assertThrows(function() { constructor.call() }, otherTypeError); |
651 assertThrows(function() { A.call() }, otherTypeError); | 652 assertThrows(function() { A.call() }, otherTypeError); |
652 })(); | 653 })(); |
653 | 654 |
| 655 |
| 656 (function TestConstructorCallOptimized() { |
| 657 class A { }; |
| 658 |
| 659 function invoke_constructor() { A() } |
| 660 function call_constructor() { A.call() } |
| 661 function apply_constructor() { A.apply() } |
| 662 |
| 663 for (var i=0; i<3; i++) { |
| 664 assertThrows(invoke_constructor); |
| 665 assertThrows(call_constructor); |
| 666 assertThrows(apply_constructor); |
| 667 } |
| 668 // Make sure we still check for class constructors when calling optimized |
| 669 // code. |
| 670 %OptimizeFunctionOnNextCall(invoke_constructor); |
| 671 assertThrows(invoke_constructor); |
| 672 %OptimizeFunctionOnNextCall(call_constructor); |
| 673 assertThrows(call_constructor); |
| 674 %OptimizeFunctionOnNextCall(apply_constructor); |
| 675 assertThrows(apply_constructor); |
| 676 })(); |
| 677 |
| 678 |
654 (function TestDefaultConstructor() { | 679 (function TestDefaultConstructor() { |
655 var calls = 0; | 680 var calls = 0; |
656 class Base { | 681 class Base { |
657 constructor() { | 682 constructor() { |
658 calls++; | 683 calls++; |
659 } | 684 } |
660 } | 685 } |
661 class Derived extends Base {} | 686 class Derived extends Base {} |
662 var object = new Derived; | 687 var object = new Derived; |
663 assertEquals(1, calls); | 688 assertEquals(1, calls); |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
964 testClassRestrictedProperties(ClassWithDefaultConstructor); | 989 testClassRestrictedProperties(ClassWithDefaultConstructor); |
965 testClassRestrictedProperties(Class); | 990 testClassRestrictedProperties(Class); |
966 testClassRestrictedProperties(DerivedClassWithDefaultConstructor); | 991 testClassRestrictedProperties(DerivedClassWithDefaultConstructor); |
967 testClassRestrictedProperties(DerivedClass); | 992 testClassRestrictedProperties(DerivedClass); |
968 testClassRestrictedProperties(class { method() {} }); | 993 testClassRestrictedProperties(class { method() {} }); |
969 testClassRestrictedProperties(class { constructor() {} method() {} }); | 994 testClassRestrictedProperties(class { constructor() {} method() {} }); |
970 testClassRestrictedProperties(class extends Class { }); | 995 testClassRestrictedProperties(class extends Class { }); |
971 testClassRestrictedProperties( | 996 testClassRestrictedProperties( |
972 class extends Class { constructor() { super(); } }); | 997 class extends Class { constructor() { super(); } }); |
973 })(); | 998 })(); |
OLD | NEW |