• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
26description('Tests for ES6 class syntax "super"');
27
28var baseMethodValue = {};
29var valueInSetter = null;
30
31class Base {
32    constructor() { }
33    baseMethod() { return baseMethodValue; }
34    chainMethod() { return 'base'; }
35    static staticMethod() { return 'base3'; }
36}
37
38class 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
49class SecondDerived extends Derived {
50    constructor() { super(); }
51    chainMethod() { return super.chainMethod().concat(['secondDerived']); }
52}
53
54shouldBeTrue('(new Base) instanceof Base');
55shouldBeTrue('(new Derived) instanceof Derived');
56shouldBe('(new Derived).callBaseMethod()', 'baseMethodValue');
57shouldBe('x = (new Derived).callBaseMethod; x()', 'baseMethodValue');
58shouldBe('(new Derived).callBaseMethodInGetter', 'baseMethodValue');
59shouldBe('(new Derived).callBaseMethodInSetter = 1; valueInSetter', 'baseMethodValue');
60shouldBe('(new Derived).baseMethodInGetterSetter', '(new Base).baseMethod');
61shouldBe('(new Derived).baseMethodInGetterSetter = 1; valueInSetter', '(new Base).baseMethod');
62shouldBe('Derived.staticMethod()', '"base3"');
63shouldBe('(new SecondDerived).chainMethod()', '["base", "derived", "secondDerived"]');
64shouldNotThrow('x = class extends Base { constructor() { super(); } super() {} }');
65shouldThrow('x = class extends Base { constructor() { super(); } method() { super() } }',
66    '"SyntaxError: \'super\' keyword unexpected here"');
67shouldThrow('x = class extends Base { constructor() { super(); } method() { super } }', '"SyntaxError: \'super\' keyword unexpected here"');
68shouldThrow('x = class extends Base { constructor() { super(); } method() { return new super } }', '"SyntaxError: \'super\' keyword unexpected here"');
69// shouldBeTrue('(new x).method() instanceof Base');
70// shouldBeFalse('(new x).method() instanceof x');
71shouldNotThrow('x = class extends Base { constructor() { super(); } method1() { delete (super.foo) } method2() { delete super["foo"] } }');
72shouldThrow('(new x).method1()', '"ReferenceError: Unsupported reference to \'super\'"');
73shouldThrow('(new x).method2()', '"ReferenceError: Unsupported reference to \'super\'"');
74shouldBeTrue('new (class { constructor() { return undefined; } }) instanceof Object');
75shouldBeTrue('new (class { constructor() { return 1; } }) instanceof Object');
76shouldThrow('new (class extends Base { constructor() { return undefined } })');
77shouldBeTrue('new (class extends Base { constructor() { super(); return undefined } }) instanceof Object');
78shouldBe('x = { }; new (class extends Base { constructor() { return x } });', 'x');
79shouldBeFalse('x instanceof Base');
80shouldThrow('new (class extends Base { constructor() { } })', '"ReferenceError: this is not defined"');
81shouldThrow('new (class extends Base { constructor() { return 1; } })', '"TypeError: Derived constructors may only return object or undefined"');
82shouldThrow('new (class extends null { constructor() { return undefined } })');
83shouldThrow('new (class extends null { constructor() { super(); return undefined } })', '"TypeError: super is not a constructor"');
84shouldBe('x = { }; new (class extends null { constructor() { return x } });', 'x');
85shouldBeTrue('x instanceof Object');
86shouldThrow('new (class extends null { constructor() { } })', '"ReferenceError: this is not defined"');
87shouldThrow('new (class extends null { constructor() { return 1; } })', '"TypeError: Derived constructors may only return object or undefined"');
88shouldThrow('new (class extends null { constructor() { super() } })', '"TypeError: super is not a constructor"');
89shouldThrow('new (class { constructor() { super() } })', '"SyntaxError: \'super\' keyword unexpected here"');
90shouldThrow('function x() { super(); }', '"SyntaxError: \'super\' keyword unexpected here"');
91shouldThrow('new (class extends Object { constructor() { function x() { super() } } })', '"SyntaxError: \'super\' keyword unexpected here"');
92shouldThrow('new (class extends Object { constructor() { function x() { super.method } } })', '"SyntaxError: \'super\' keyword unexpected here"');
93shouldThrow('function x() { super.method(); }', '"SyntaxError: \'super\' keyword unexpected here"');
94shouldThrow('function x() { super(); }', '"SyntaxError: \'super\' keyword unexpected here"');
95
96var successfullyParsed = true;
97