Home | History | Annotate | Download | only in webkit
      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 --harmony-destructuring-bind
     25 
     26 description('Tests for ES6 class syntax declarations');
     27 
     28 var constructorCallCount = 0;
     29 const staticMethodValue = [1];
     30 const instanceMethodValue = [2];
     31 const getterValue = [3];
     32 var setterValue = undefined;
     33 class A {
     34     constructor() { constructorCallCount++; }
     35     static someStaticMethod() { return staticMethodValue; }
     36     static get someStaticGetter() { return getterValue; }
     37     static set someStaticSetter(value) { setterValue = value; }
     38     someInstanceMethod() { return instanceMethodValue; }
     39     get someGetter() { return getterValue; }
     40     set someSetter(value) { setterValue = value; }
     41 }
     42 
     43 shouldBe("constructorCallCount", "0");
     44 shouldBe("A.someStaticMethod()", "staticMethodValue");
     45 shouldBe("A.someStaticGetter", "getterValue");
     46 shouldBe("setterValue = undefined; A.someStaticSetter = 123; setterValue", "123");
     47 shouldBe("(new A).someInstanceMethod()", "instanceMethodValue");
     48 shouldBe("constructorCallCount", "1");
     49 shouldBe("(new A).someGetter", "getterValue");
     50 shouldBe("constructorCallCount", "2");
     51 shouldBe("(new A).someGetter", "getterValue");
     52 shouldBe("setterValue = undefined; (new A).someSetter = 789; setterValue", "789");
     53 shouldBe("(new A).__proto__", "A.prototype");
     54 shouldBe("A.prototype.constructor", "A");
     55 
     56 shouldThrow("class", "'SyntaxError: Unexpected end of input'");
     57 shouldThrow("class [", "'SyntaxError: Unexpected token ['");
     58 shouldThrow("class {", "'SyntaxError: Unexpected token {'");
     59 shouldThrow("class X {", "'SyntaxError: Unexpected end of input'");
     60 shouldThrow("class X { ( }", "'SyntaxError: Unexpected token ('");
     61 shouldNotThrow("class X {}");
     62 
     63 shouldThrow("class X { constructor() {} constructor() {} }", "'SyntaxError: A class may only have one constructor'");
     64 shouldThrow("class X { get constructor() {} }", "'SyntaxError: Class constructor may not be an accessor'");
     65 shouldThrow("class X { set constructor() {} }", "'SyntaxError: Class constructor may not be an accessor'");
     66 shouldNotThrow("class X { constructor() {} static constructor() { return staticMethodValue; } }");
     67 shouldBe("class X { constructor() {} static constructor() { return staticMethodValue; } }; X.constructor()", "staticMethodValue");
     68 
     69 shouldThrow("class X { constructor() {} static prototype() {} }", "'SyntaxError: Classes may not have static property named prototype'");
     70 shouldThrow("class X { constructor() {} static get prototype() {} }", "'SyntaxError: Classes may not have static property named prototype'");
     71 shouldThrow("class X { constructor() {} static set prototype() {} }", "'SyntaxError: Classes may not have static property named prototype'");
     72 shouldNotThrow("class X { constructor() {} prototype() { return instanceMethodValue; } }");
     73 shouldBe("class X { constructor() {} prototype() { return instanceMethodValue; } }; (new X).prototype()", "instanceMethodValue");
     74 
     75 shouldNotThrow("class X { constructor() {} set foo(a) {} }");
     76 shouldNotThrow("class X { constructor() {} set foo({x, y}) {} }");
     77 shouldThrow("class X { constructor() {} set foo() {} }");
     78 shouldThrow("class X { constructor() {} set foo(a, b) {} }");
     79 shouldNotThrow("class X { constructor() {} get foo() {} }");
     80 shouldThrow("class X { constructor() {} get foo(x) {} }");
     81 shouldThrow("class X { constructor() {} get foo({x, y}) {} }");
     82 
     83 var successfullyParsed = true;
     84