OLD | NEW |
(Empty) | |
| 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 AN
Y |
| 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 AN
Y |
| 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 O
N |
| 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 "super"'); |
| 27 |
| 28 var baseMethodValue = {}; |
| 29 var valueInSetter = null; |
| 30 |
| 31 class Base { |
| 32 constructor() { } |
| 33 baseMethod() { return baseMethodValue; } |
| 34 chainMethod() { return 'base'; } |
| 35 static staticMethod() { return 'base3'; } |
| 36 } |
| 37 |
| 38 class Derived extends Base { |
| 39 constructor() { super(); } |
| 40 chainMethod() { return [super.chainMethod(), 'derived']; } |
| 41 callBaseMethod() { return super.baseMethod(); } |
| 42 get callBaseMethodInGetter() { return super['baseMethod'](); } |
| 43 set callBaseMethodInSetter(x) { valueInSetter = super.baseMethod(); } |
| 44 get baseMethodInGetterSetter() { return super.baseMethod; } |
| 45 set baseMethodInGetterSetter(x) { valueInSetter = super['baseMethod']; } |
| 46 static staticMethod() { return super.staticMethod(); } |
| 47 } |
| 48 |
| 49 class SecondDerived extends Derived { |
| 50 constructor() { super(); } |
| 51 chainMethod() { return super.chainMethod().concat(['secondDerived']); } |
| 52 } |
| 53 |
| 54 shouldBeTrue('(new Base) instanceof Base'); |
| 55 shouldBeTrue('(new Derived) instanceof Derived'); |
| 56 shouldBe('(new Derived).callBaseMethod()', 'baseMethodValue'); |
| 57 shouldBe('x = (new Derived).callBaseMethod; x()', 'baseMethodValue'); |
| 58 shouldBe('(new Derived).callBaseMethodInGetter', 'baseMethodValue'); |
| 59 shouldBe('(new Derived).callBaseMethodInSetter = 1; valueInSetter', 'baseMethodV
alue'); |
| 60 shouldBe('(new Derived).baseMethodInGetterSetter', '(new Base).baseMethod'); |
| 61 shouldBe('(new Derived).baseMethodInGetterSetter = 1; valueInSetter', '(new Base
).baseMethod'); |
| 62 shouldBe('Derived.staticMethod()', '"base3"'); |
| 63 shouldBe('(new SecondDerived).chainMethod()', '["base", "derived", "secondDerive
d"]'); |
| 64 shouldNotThrow('x = class extends Base { constructor() { super(); } super() {} }
'); |
| 65 shouldThrow('x = class extends Base { constructor() { super(); } method() { supe
r() } }', |
| 66 '"SyntaxError: \'super\' keyword unexpected here"'); |
| 67 shouldThrow('x = class extends Base { constructor() { super(); } method() { supe
r } }', '"SyntaxError: \'super\' keyword unexpected here"'); |
| 68 shouldThrow('x = class extends Base { constructor() { super(); } method() { retu
rn new super } }', '"SyntaxError: \'super\' keyword unexpected here"'); |
| 69 // shouldBeTrue('(new x).method() instanceof Base'); |
| 70 // shouldBeFalse('(new x).method() instanceof x'); |
| 71 shouldNotThrow('x = class extends Base { constructor() { super(); } method1() {
delete (super.foo) } method2() { delete super["foo"] } }'); |
| 72 shouldThrow('(new x).method1()', '"ReferenceError: Unsupported reference to \'su
per\'"'); |
| 73 shouldThrow('(new x).method2()', '"ReferenceError: Unsupported reference to \'su
per\'"'); |
| 74 shouldBeTrue('new (class { constructor() { return undefined; } }) instanceof Obj
ect'); |
| 75 shouldBeTrue('new (class { constructor() { return 1; } }) instanceof Object'); |
| 76 shouldThrow('new (class extends Base { constructor() { return undefined } })'); |
| 77 shouldBeTrue('new (class extends Base { constructor() { super(); return undefine
d } }) instanceof Object'); |
| 78 shouldBe('x = { }; new (class extends Base { constructor() { return x } });', 'x
'); |
| 79 shouldBeFalse('x instanceof Base'); |
| 80 shouldThrow('new (class extends Base { constructor() { } })', '"ReferenceError:
this is not defined"'); |
| 81 shouldThrow('new (class extends Base { constructor() { return 1; } })', '"TypeEr
ror: Derived constructors may only return object or undefined"'); |
| 82 shouldThrow('new (class extends null { constructor() { return undefined } })'); |
| 83 shouldThrow('new (class extends null { constructor() { super(); return undefined
} })', '"TypeError: function () {} is not a constructor"'); |
| 84 shouldBe('x = { }; new (class extends null { constructor() { return x } });', 'x
'); |
| 85 shouldBeTrue('x instanceof Object'); |
| 86 shouldThrow('new (class extends null { constructor() { } })', '"ReferenceError:
this is not defined"'); |
| 87 shouldThrow('new (class extends null { constructor() { return 1; } })', '"TypeEr
ror: Derived constructors may only return object or undefined"'); |
| 88 shouldThrow('new (class extends null { constructor() { super() } })', '"TypeErro
r: function () {} is not a constructor"'); |
| 89 shouldThrow('new (class { constructor() { super() } })', '"SyntaxError: \'super\
' keyword unexpected here"'); |
| 90 shouldThrow('function x() { super(); }', '"SyntaxError: \'super\' keyword unexpe
cted here"'); |
| 91 shouldThrow('new (class extends Object { constructor() { function x() { super()
} } })', '"SyntaxError: \'super\' keyword unexpected here"'); |
| 92 shouldThrow('new (class extends Object { constructor() { function x() { super.me
thod } } })', '"SyntaxError: \'super\' keyword unexpected here"'); |
| 93 shouldThrow('function x() { super.method(); }', '"SyntaxError: \'super\' keyword
unexpected here"'); |
| 94 shouldThrow('function x() { super(); }', '"SyntaxError: \'super\' keyword unexpe
cted here"'); |
| 95 |
| 96 var successfullyParsed = true; |
OLD | NEW |