Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 24 | description('Tests for ES6 class constructor return values'); |
| 25 | |
| 26 | // ES6 |
| 27 | // - 9.2.2 [[Construct]] (ECMAScript Function Objects) |
| 28 | // - 12.3.5.1 Runtime Semantics: Evaluation (The super Keyword) |
| 29 | // - 14.5.14 Runtime Semantics: ClassDefinitionEvaluation (Default Constructor) |
| 30 | |
| 31 | var globalVariable = {name:"globalVariable"}; |
| 32 | var globalSymbol = Symbol(); |
| 33 | |
| 34 | debug('Base class'); |
| 35 | class BaseNoReturn { constructor() { } }; |
| 36 | class BaseReturnImplicit { constructor() { return; } }; |
| 37 | class BaseReturnUndefined { constructor() { return undefined; } }; |
| 38 | class BaseReturnThis { constructor() { return this; } }; |
| 39 | class BaseReturnObject { constructor() { return {a:1}; } }; |
| 40 | class BaseReturnObject2 { constructor() { return globalVariable; } }; |
| 41 | class BaseReturnString { constructor() { return "test"; } }; |
| 42 | class BaseReturnNumber { constructor() { return 1; } }; |
| 43 | class BaseReturnNull { constructor() { return null; } }; |
| 44 | class BaseReturnSymbol { constructor() { return Symbol(); } }; |
| 45 | class BaseThrow { constructor() { throw "Thrown Exception String"; } }; |
| 46 | |
| 47 | // Base - Implicit => return this. |
| 48 | shouldBeTrue('(new BaseNoReturn) instanceof BaseNoReturn'); |
| 49 | |
| 50 | // Base - Early return => return this. |
| 51 | shouldBeTrue('(new BaseReturnImplicit) instanceof BaseReturnImplicit'); |
| 52 | shouldBeTrue('(new BaseReturnImplicit) !== undefined'); |
| 53 | shouldBeTrue('(new BaseReturnUndefined) instanceof BaseReturnUndefined'); |
| 54 | shouldBeTrue('(new BaseReturnUndefined) !== undefined'); |
| 55 | |
| 56 | // Base - return this => return this. |
| 57 | shouldBeTrue('(new BaseReturnThis) instanceof BaseReturnThis'); |
| 58 | |
| 59 | // Base - return Object => return object, not instance. |
| 60 | shouldBeFalse('(new BaseReturnObject) instanceof BaseReturnObject'); |
| 61 | shouldBeTrue('typeof (new BaseReturnObject) === "object"'); |
| 62 | shouldBeFalse('(new BaseReturnObject2) instanceof BaseReturnObject'); |
| 63 | shouldBeTrue('(new BaseReturnObject2) === globalVariable'); |
| 64 | |
| 65 | // Base - return non-Object => return this. |
| 66 | shouldBeTrue('(new BaseReturnString) instanceof BaseReturnString'); |
| 67 | shouldBeTrue('typeof (new BaseReturnString) !== "string"'); |
| 68 | shouldBeTrue('(new BaseReturnNumber) instanceof BaseReturnNumber'); |
| 69 | shouldBeTrue('typeof (new BaseReturnNumber) !== "number"'); |
| 70 | shouldBeTrue('(new BaseReturnNull) instanceof BaseReturnNull'); |
| 71 | shouldBeTrue('(new BaseReturnNull) !== null'); |
| 72 | shouldBeTrue('(new BaseReturnSymbol) instanceof BaseReturnSymbol'); |
| 73 | shouldBeTrue('(new BaseReturnSymbol) !== globalSymbol'); |
| 74 | |
| 75 | // Base - throw => throw |
| 76 | shouldThrow('(new BaseThrow)'); |
| 77 | |
| 78 | // Same behavior for Functions. |
| 79 | debug(''); debug('Function constructor (non-class)'); |
| 80 | function FunctionNoReturn() { }; |
| 81 | function FunctionReturnImplicit() { return; }; |
| 82 | function FunctionReturnUndefined() { return undefined; }; |
| 83 | function FunctionReturnThis() { return this; }; |
| 84 | function FunctionReturnObject() { return {a:1}; }; |
| 85 | function FunctionReturnObject2() { return globalVariable; }; |
| 86 | function FunctionReturnString() { return "test"; }; |
| 87 | function FunctionReturnNumber() { return 1; }; |
| 88 | function FunctionReturnNull() { return null; }; |
| 89 | function FunctionReturnSymbol() { return Symbol(); }; |
| 90 | function FunctionThrow() { throw "Thrown Exception String"; }; |
| 91 | |
| 92 | shouldBeTrue('(new FunctionNoReturn) instanceof FunctionNoReturn'); |
| 93 | shouldBeTrue('(new FunctionReturnImplicit) instanceof FunctionReturnImplicit'); |
| 94 | shouldBeTrue('(new FunctionReturnImplicit) !== undefined'); |
| 95 | shouldBeTrue('(new FunctionReturnUndefined) instanceof FunctionReturnUndefined'); |
| 96 | shouldBeTrue('(new FunctionReturnUndefined) !== undefined'); |
| 97 | shouldBeTrue('(new FunctionReturnThis) instanceof FunctionReturnThis'); |
| 98 | shouldBeFalse('(new FunctionReturnObject) instanceof FunctionReturnObject'); |
| 99 | shouldBeTrue('typeof (new FunctionReturnObject) === "object"'); |
| 100 | shouldBeFalse('(new FunctionReturnObject2) instanceof FunctionReturnObject'); |
| 101 | shouldBeTrue('(new FunctionReturnObject2) === globalVariable'); |
| 102 | shouldBeTrue('(new FunctionReturnString) instanceof FunctionReturnString'); |
| 103 | shouldBeTrue('typeof (new FunctionReturnString) !== "string"'); |
| 104 | shouldBeTrue('(new FunctionReturnNumber) instanceof FunctionReturnNumber'); |
| 105 | shouldBeTrue('typeof (new FunctionReturnNumber) !== "number"'); |
| 106 | shouldBeTrue('(new FunctionReturnNull) instanceof FunctionReturnNull'); |
| 107 | shouldBeTrue('(new FunctionReturnNull) !== null'); |
| 108 | shouldBeTrue('(new FunctionReturnSymbol) instanceof FunctionReturnSymbol'); |
| 109 | shouldBeTrue('(new FunctionReturnSymbol) !== globalSymbol'); |
| 110 | shouldThrow('(new FunctionThrow)'); |
| 111 | |
| 112 | |
| 113 | debug(''); debug('Derived class calling super()'); |
| 114 | class DerivedNoReturn extends BaseNoReturn { constructor() { super(); } }; |
| 115 | class DerivedReturnImplicit extends BaseNoReturn { constructor() { super(); return; } }; |
| 116 | class DerivedReturnUndefined extends BaseNoReturn { constructor() { super(); return undefined; } }; |
| 117 | class DerivedReturnThis extends BaseNoReturn { constructor() { super(); return this; } }; |
| 118 | class DerivedReturnObject extends BaseNoReturn { constructor() { super(); return {a:1}; } }; |
| 119 | class DerivedReturnObject2 extends BaseNoReturn { constructor() { super(); return globalVariable; } }; |
| 120 | class DerivedReturnString extends BaseNoReturn { constructor() { super(); return "test"; } }; |
| 121 | class DerivedReturnNumber extends BaseNoReturn { constructor() { super(); return 1; } }; |
| 122 | class DerivedReturnNull extends BaseNoReturn { constructor() { super(); return null; } }; |
| 123 | class DerivedReturnSymbol extends BaseNoReturn { constructor() { super(); return globalSymbol; } }; |
| 124 | class DerivedThrow extends BaseNoReturn { constructor() { super(); throw "Thrown Exception String"; } }; |
| 125 | |
| 126 | // Derived - Implicit => return this. |
| 127 | shouldBeTrue('(new DerivedNoReturn) instanceof DerivedNoReturn'); |
| 128 | |
| 129 | // Derived - Early return => return this. |
| 130 | shouldBeTrue('(new DerivedReturnImplicit) instanceof DerivedReturnImplicit'); |
| 131 | shouldBeTrue('(new DerivedReturnImplicit) !== undefined'); |
| 132 | shouldBeTrue('(new DerivedReturnUndefined) instanceof DerivedReturnUndefined'); |
| 133 | shouldBeTrue('(new DerivedReturnUndefined) !== undefined'); |
| 134 | |
| 135 | // Derived - return this => return this. |
| 136 | shouldBeTrue('(new DerivedReturnThis) instanceof DerivedReturnThis'); |
| 137 | |
| 138 | // Derived - return Object => return object, not instance. |
| 139 | shouldBeFalse('(new DerivedReturnObject) instanceof DerivedReturnObject'); |
| 140 | shouldBeTrue('typeof (new DerivedReturnObject) === "object"'); |
| 141 | shouldBeFalse('(new DerivedReturnObject2) instanceof DerivedReturnObject2'); |
| 142 | shouldBeTrue('(new DerivedReturnObject2) === globalVariable'); |
| 143 | |
| 144 | // Derived - return non-Object => exception. |
| 145 | shouldThrow('(new DerivedReturnString)'); |
| 146 | shouldThrow('(new DerivedReturnNumber)'); |
| 147 | shouldThrow('(new DerivedReturnNull)'); |
| 148 | shouldThrow('(new DerivedReturnSymbol)'); |
| 149 | shouldThrow('(new DerivedThrow)'); |
| 150 | |
| 151 | |
| 152 | debug(''); debug('Derived class not calling super()'); |
| 153 | class DerivedNoSuperNoReturn extends BaseNoReturn { constructor() { } }; |
| 154 | class DerivedNoSuperReturn extends BaseNoReturn { constructor() { return; } }; |
| 155 | class DerivedNoSuperReturnUndefined extends BaseNoReturn { constructor() { return undefined; } }; |
| 156 | class DerivedNoSuperReturnObject extends BaseNoReturn { constructor() { return {a:1}; } }; |
| 157 | class DerivedNoSuperReturnObject2 extends BaseNoReturn { constructor() { return globalVariable; } }; |
| 158 | class DerivedNoSuperReturnThis extends BaseNoReturn { constructor() { return this; } }; |
| 159 | class DerivedNoSuperReturnString extends BaseNoReturn { constructor() { return "test"; } }; |
| 160 | class DerivedNoSuperReturnNumber extends BaseNoReturn { constructor() { return 1; } }; |
| 161 | class DerivedNoSuperReturnNull extends BaseNoReturn { constructor() { return null; } }; |
| 162 | class DerivedNoSuperReturnSymbol extends BaseNoReturn { constructor() { return globalSymbol; } }; |
| 163 | class DerivedNoSuperThrow extends BaseNoReturn { constructor() { throw "Thrown Exception String"; } }; |
| 164 | |
| 165 | // Derived without super() - Implicit => return this => TDZ. |
| 166 | shouldThrow('(new DerivedNoSuperNoReturn)'); |
| 167 | |
| 168 | // Derived without super() - Early return => return this => TDZ. |
| 169 | shouldThrow('(new DerivedNoSuperReturnImplicit)'); |
| 170 | shouldThrow('(new DerivedNoSuperReturnUndefined)'); |
| 171 | |
| 172 | // Derived without super() - return this => return this => TDZ |
| 173 | shouldThrow('(new DerivedNoSuperReturnThis)'); |
| 174 | |
| 175 | // Derived without super() - return Object => no this access => return object, not instance |
| 176 | shouldNotThrow('(new DerivedNoSuperReturnObject)'); |
| 177 | shouldNotThrow('(new DerivedNoSuperReturnObject2)'); |
| 178 | |
| 179 | // Derived without super() - return non-Object => exception |
| 180 | shouldThrow('(new DerivedNoSuperReturnString)'); // TypeError |
| 181 | shouldThrow('(new DerivedNoSuperReturnNumber)'); // TypeError |
| 182 | shouldThrow('(new DerivedNoSuperReturnNull)'); // TypeError |
| 183 | shouldThrow('(new DerivedNoSuperReturnSymbol)'); // TypeError |
| 184 | shouldThrow('(new DerivedNoSuperThrow)'); // Thrown exception |
| 185 | |
| 186 | |
| 187 | debug(''); debug('Derived class with default constructor and base class returning different values'); |
| 188 | class DerivedDefaultConstructorWithBaseNoReturn extends BaseNoReturn { }; |
| 189 | class DerivedDefaultConstructorWithBaseReturnImplicit extends BaseReturnImplicit { }; |
| 190 | class DerivedDefaultConstructorWithBaseReturnUndefined extends BaseReturnUndefined { }; |
| 191 | class DerivedDefaultConstructorWithBaseReturnThis extends BaseReturnThis { }; |
| 192 | class DerivedDefaultConstructorWithBaseReturnObject extends BaseReturnObject { }; |
| 193 | class DerivedDefaultConstructorWithBaseReturnObject2 extends BaseReturnObject2 { }; |
| 194 | class DerivedDefaultConstructorWithBaseReturnString extends BaseReturnString { }; |
| 195 | class DerivedDefaultConstructorWithBaseReturnNumber extends BaseReturnNumber { }; |
| 196 | class DerivedDefaultConstructorWithBaseReturnNull extends BaseReturnNull { }; |
| 197 | class DerivedDefaultConstructorWithBaseReturnSymbol extends BaseReturnSymbol { }; |
| 198 | class DerivedDefaultConstructorWithBaseThrow extends BaseThrow { }; |
| 199 | |
| 200 | // Derived default constructor - implicit "super(...arguments)" return the result of the base (Object or this). |
| 201 | shouldBeTrue('(new DerivedDefaultConstructorWithBaseNoReturn) instanceof DerivedDefaultConstructorWithBaseNoReturn'); |
| 202 | shouldBeTrue('(new DerivedDefaultConstructorWithBaseReturnImplicit) instanceof DerivedDefaultConstructorWithBaseReturnImplicit'); |
| 203 | shouldBeTrue('(new DerivedDefaultConstructorWithBaseReturnUndefined) instanceof DerivedDefaultConstructorWithBaseReturnUndefined'); |
| 204 | shouldBeFalse('(new DerivedDefaultConstructorWithBaseReturnObject) instanceof DerivedDefaultConstructorWithBaseReturnObject'); |
| 205 | shouldBeTrue('typeof (new DerivedDefaultConstructorWithBaseReturnObject) === "object"'); |
| 206 | shouldBeFalse('(new DerivedDefaultConstructorWithBaseReturnObject2) instanceof DerivedDefaultConstructorWithBaseReturnObject2'); |
| 207 | shouldBeTrue('(new DerivedDefaultConstructorWithBaseReturnObject2) === globalVariable'); |
| 208 | shouldBeTrue('(new DerivedDefaultConstructorWithBaseReturnThis) instanceof DerivedDefaultConstructorWithBaseReturnThis'); |
| 209 | shouldBeTrue('(new DerivedDefaultConstructorWithBaseReturnString) instanceof DerivedDefaultConstructorWithBaseReturnString'); |
| 210 | shouldBeTrue('(new DerivedDefaultConstructorWithBaseReturnNumber) instanceof DerivedDefaultConstructorWithBaseReturnNumber'); |
| 211 | shouldBeTrue('(new DerivedDefaultConstructorWithBaseReturnNull) instanceof DerivedDefaultConstructorWithBaseReturnNull'); |
| 212 | shouldBeTrue('(new DerivedDefaultConstructorWithBaseReturnSymbol) instanceof DerivedDefaultConstructorWithBaseReturnSymbol'); |
| 213 | shouldThrow('(new DerivedDefaultConstructorWithBaseThrow)'); |
| 214 | |
| 215 | var successfullyParsed = true; |