1 // Copyright 2015 the V8 project authors. All rights reserved. 2 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions 6 // are met: 7 // 1. Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // 2. Redistributions in binary form must reproduce the above copyright 10 // notice, this list of conditions and the following disclaimer in the 11 // documentation and/or other materials provided with the distribution. 12 // 13 // THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 // DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 24 // Flags: --harmony-sloppy 25 26 description('Tests for ES6 class syntax "extends"'); 27 28 class Base { 29 constructor() { } 30 baseMethod() { return 'base'; } 31 overridenMethod() { return 'base'; } 32 static staticBaseMethod() { return 'base'; } 33 static staticOverridenMethod() { return 'base'; } 34 } 35 36 class Derived extends Base { 37 constructor() { super(); } 38 overridenMethod() { return 'derived'; } 39 static staticOverridenMethod() { return 'derived'; } 40 } 41 42 shouldBeTrue('(new Base) instanceof Base'); 43 shouldBe('Object.getPrototypeOf(new Base)', 'Base.prototype'); 44 shouldBeTrue('(new Derived) instanceof Derived'); 45 shouldBe('Object.getPrototypeOf(new Derived)', 'Derived.prototype'); 46 shouldBe('Object.getPrototypeOf(Derived.prototype)', 'Base.prototype'); 47 shouldBe('(new Derived).baseMethod()', '"base"'); 48 shouldBe('(new Derived).overridenMethod()', '"derived"'); 49 shouldBe('Derived.staticBaseMethod()', '"base"'); 50 shouldBe('Derived.staticOverridenMethod()', '"derived"'); 51 52 shouldThrow('x = class extends', '"SyntaxError: Unexpected end of input"'); 53 shouldThrow('x = class extends', '"SyntaxError: Unexpected end of input"'); 54 shouldThrow('x = class extends Base {', '"SyntaxError: Unexpected end of input"'); 55 shouldNotThrow('x = class extends Base { }'); 56 shouldNotThrow('x = class extends Base { constructor() { } }'); 57 shouldBe('x.__proto__', 'Base'); 58 shouldBe('Object.getPrototypeOf(x)', 'Base'); 59 shouldBe('x.prototype.__proto__', 'Base.prototype'); 60 shouldBe('Object.getPrototypeOf(x.prototype)', 'Base.prototype'); 61 shouldBe('x = class extends null { constructor() { } }; x.__proto__', 'Function.prototype'); 62 shouldBe('x.__proto__', 'Function.prototype'); 63 shouldThrow('x = class extends 3 { constructor() { } }; x.__proto__', '"TypeError: Class extends value 3 is not a function or null"'); 64 shouldThrow('x = class extends "abc" { constructor() { } }; x.__proto__', '"TypeError: Class extends value abc is not a function or null"'); 65 shouldNotThrow('baseWithBadPrototype = function () {}; baseWithBadPrototype.prototype = 3; new baseWithBadPrototype'); 66 shouldThrow('x = class extends baseWithBadPrototype { constructor() { } }', '"TypeError: Class extends value does not have valid prototype property 3"'); 67 shouldNotThrow('baseWithBadPrototype.prototype = "abc"'); 68 shouldThrow('x = class extends baseWithBadPrototype { constructor() { } }', '"TypeError: Class extends value does not have valid prototype property abc"'); 69 shouldNotThrow('baseWithBadPrototype.prototype = null; x = class extends baseWithBadPrototype { constructor() { } }'); 70 71 shouldThrow('x = 1; c = class extends ++x { constructor() { } };'); 72 shouldThrow('x = 1; c = class extends x++ { constructor() { } };'); 73 shouldThrow('x = 1; c = class extends (++x) { constructor() { } };'); 74 shouldThrow('x = 1; c = class extends (x++) { constructor() { } };'); 75 shouldBe('x = 1; try { c = class extends (++x) { constructor() { } } } catch (e) { }; x', '2'); 76 shouldBe('x = 1; try { c = class extends (x++) { constructor() { } } } catch (e) { }; x', '2'); 77 78 shouldNotThrow('namespace = {}; namespace.A = class { }; namespace.B = class extends namespace.A { }'); 79 shouldNotThrow('namespace = {}; namespace.A = class A { }; namespace.B = class B extends namespace.A { }'); 80 shouldNotThrow('namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends namespace.A { constructor() { } }'); 81 shouldNotThrow('namespace = {}; namespace.A = class A { constructor() { } }; namespace.B = class B extends namespace.A { constructor() { } }'); 82 shouldNotThrow('namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends (namespace.A) { constructor() { } }'); 83 shouldNotThrow('namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends namespace["A"] { constructor() { } }'); 84 shouldNotThrow('namespace = {}; namespace.A = class { constructor() { } }; function getClassA() { return namespace.A }; namespace.B = class extends getClassA() { constructor() { } }'); 85 shouldNotThrow('namespace = {}; namespace.A = class { constructor() { } }; function getClass(prop) { return namespace[prop] }; namespace.B = class extends getClass("A") { constructor() { } }'); 86 shouldNotThrow('namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends (false||null||namespace.A) { constructor() { } }'); 87 shouldThrow('namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends false||null||namespace.A { constructor() { } }'); 88 shouldNotThrow('x = 1; namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends (x++, namespace.A) { constructor() { } };'); 89 shouldThrow('x = 1; namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends (namespace.A, x++) { constructor() { } };'); 90 shouldThrow('namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends new namespace.A { constructor() { } }'); 91 shouldThrow('namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends new namespace.A() { constructor() { } }'); 92 shouldBe('x = 1; namespace = {}; namespace.A = class { constructor() { } }; try { namespace.B = class extends (x++, namespace.A) { constructor() { } } } catch (e) { } x', '2'); 93 shouldBe('x = 1; namespace = {}; namespace.A = class { constructor() { } }; try { namespace.B = class extends (namespace.A, x++) { constructor() { } } } catch (e) { } x', '2'); 94 95 shouldBe('Object.getPrototypeOf((class { constructor () { } }).prototype)', 'Object.prototype'); 96 shouldBe('Object.getPrototypeOf((class extends null { constructor () { super(); } }).prototype)', 'null'); 97 shouldThrow('new (class extends undefined { constructor () { this } })', '"TypeError: Class extends value undefined is not a function or null"'); 98 shouldThrow('new (class extends undefined { constructor () { super(); } })', '"TypeError: Class extends value undefined is not a function or null"'); 99 shouldThrow('x = {}; new (class extends undefined { constructor () { return x; } })', '"TypeError: Class extends value undefined is not a function or null"'); 100 shouldThrow('y = 12; new (class extends undefined { constructor () { return y; } })', '"TypeError: Class extends value undefined is not a function or null"'); 101 shouldBeTrue ('class x {}; new (class extends null { constructor () { return new x; } }) instanceof x'); 102 shouldThrow('new (class extends null { constructor () { this; } })', '"ReferenceError: this is not defined"'); 103 shouldThrow('new (class extends null { constructor () { super(); } })', '"TypeError: super is not a constructor"'); 104 shouldBe('x = {}; new (class extends null { constructor () { return x } })', 'x'); 105 shouldThrow('y = 12; new (class extends null { constructor () { return y; } })', '"TypeError: Derived constructors may only return object or undefined"'); 106 shouldBeTrue ('class x {}; new (class extends null { constructor () { return new x; } }) instanceof x'); 107 shouldBe('x = null; Object.getPrototypeOf((class extends x { }).prototype)', 'null'); 108 shouldBeTrue('Object.prototype.isPrototypeOf(class { })'); 109 shouldBeTrue('Function.prototype.isPrototypeOf(class { })'); 110 111 var successfullyParsed = true; 112