| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Flags: --harmony-new-target --harmony-reflect --harmony-destructuring | |
| 6 // Flags: --harmony-rest-parameters | |
| 7 | |
| 8 | |
| 9 (function TestClass() { | |
| 10 'use strict'; | |
| 11 | |
| 12 var calls = 0; | |
| 13 class Base { | |
| 14 constructor(_) { | |
| 15 assertEquals(Base, new.target); | |
| 16 calls++; | |
| 17 } | |
| 18 } | |
| 19 assertInstanceof(new Base(1), Base); | |
| 20 assertInstanceof(new Base(1, 2), Base); | |
| 21 assertInstanceof(new Base(), Base); | |
| 22 assertEquals(3, calls); | |
| 23 })(); | |
| 24 | |
| 25 | |
| 26 (function TestDerivedClass() { | |
| 27 'use strict'; | |
| 28 | |
| 29 var calls = 0; | |
| 30 class Base { | |
| 31 constructor(expected) { | |
| 32 assertEquals(expected, new.target); | |
| 33 } | |
| 34 } | |
| 35 class Derived extends Base { | |
| 36 constructor(expected) { | |
| 37 super(expected); | |
| 38 assertEquals(expected, new.target); | |
| 39 calls++; | |
| 40 } | |
| 41 } | |
| 42 new Derived(Derived, 'extra'); | |
| 43 new Derived(Derived); | |
| 44 assertEquals(2, calls); | |
| 45 | |
| 46 class Derived2 extends Derived {} | |
| 47 calls = 0; | |
| 48 new Derived2(Derived2); | |
| 49 new Derived2(Derived2, 'extra'); | |
| 50 assertEquals(2, calls); | |
| 51 })(); | |
| 52 | |
| 53 | |
| 54 (function TestFunctionCall() { | |
| 55 var calls; | |
| 56 function f(expected) { | |
| 57 calls++; | |
| 58 assertEquals(expected, new.target); | |
| 59 } | |
| 60 | |
| 61 calls = 0; | |
| 62 f(undefined); | |
| 63 f(undefined, 'extra'); | |
| 64 f(); | |
| 65 assertEquals(3, calls); | |
| 66 | |
| 67 calls = 0; | |
| 68 f.call({}, undefined); | |
| 69 f.call({}, undefined, 'extra'); | |
| 70 f.call({}); | |
| 71 assertEquals(3, calls); | |
| 72 | |
| 73 calls = 0; | |
| 74 f.apply({}, [undefined]); | |
| 75 f.apply({}, [undefined, 'extra']); | |
| 76 f.apply({}, []); | |
| 77 assertEquals(3, calls); | |
| 78 })(); | |
| 79 | |
| 80 | |
| 81 (function TestFunctionConstruct() { | |
| 82 var calls; | |
| 83 function f(expected) { | |
| 84 calls++; | |
| 85 assertEquals(expected, new.target); | |
| 86 } | |
| 87 | |
| 88 calls = 0; | |
| 89 new f(f); | |
| 90 new f(f, 'extra'); | |
| 91 assertEquals(2, calls); | |
| 92 })(); | |
| 93 | |
| 94 | |
| 95 (function TestClassExtendsFunction() { | |
| 96 'use strict'; | |
| 97 | |
| 98 var calls = 0; | |
| 99 function f(expected) { | |
| 100 assertEquals(expected, new.target); | |
| 101 } | |
| 102 class Derived extends f { | |
| 103 constructor(expected) { | |
| 104 super(expected); | |
| 105 assertEquals(expected, new.target); | |
| 106 calls++; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 new Derived(Derived); | |
| 111 new Derived(Derived, 'extra'); | |
| 112 assertEquals(2, calls); | |
| 113 })(); | |
| 114 | |
| 115 | |
| 116 (function TestFunctionReturnObject() { | |
| 117 function f(expected) { | |
| 118 assertEquals(expected, new.target); | |
| 119 return /abc/; | |
| 120 } | |
| 121 | |
| 122 assertInstanceof(new f(f), RegExp); | |
| 123 assertInstanceof(new f(f, 'extra'), RegExp); | |
| 124 | |
| 125 assertInstanceof(f(undefined), RegExp); | |
| 126 assertInstanceof(f(), RegExp); | |
| 127 assertInstanceof(f(undefined, 'extra'), RegExp); | |
| 128 })(); | |
| 129 | |
| 130 | |
| 131 (function TestClassReturnObject() { | |
| 132 'use strict'; | |
| 133 | |
| 134 class Base { | |
| 135 constructor(expected) { | |
| 136 assertEquals(expected, new.target); | |
| 137 return /abc/; | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 assertInstanceof(new Base(Base), RegExp); | |
| 142 assertInstanceof(new Base(Base, 'extra'), RegExp); | |
| 143 | |
| 144 class Derived extends Base {} | |
| 145 assertInstanceof(new Derived(Derived), RegExp); | |
| 146 assertInstanceof(new Derived(Derived, 'extra'), RegExp); | |
| 147 | |
| 148 class Derived2 extends Base { | |
| 149 constructor(expected) { | |
| 150 super(expected); | |
| 151 assertInstanceof(this, RegExp); | |
| 152 } | |
| 153 } | |
| 154 assertInstanceof(new Derived2(Derived2), RegExp); | |
| 155 assertInstanceof(new Derived2(Derived2, 'extra'), RegExp); | |
| 156 })(); | |
| 157 | |
| 158 | |
| 159 (function TestReflectConstruct() { | |
| 160 var calls = 0; | |
| 161 function f(expected) { | |
| 162 calls++; | |
| 163 assertEquals(expected, new.target); | |
| 164 } | |
| 165 | |
| 166 var o = Reflect.construct(f, [f]); | |
| 167 assertEquals(Object.getPrototypeOf(o), f.prototype); | |
| 168 o = Reflect.construct(f, [f, 'extra']); | |
| 169 assertEquals(Object.getPrototypeOf(o), f.prototype); | |
| 170 assertEquals(2, calls); | |
| 171 | |
| 172 calls = 0; | |
| 173 o = Reflect.construct(f, [f], f); | |
| 174 assertEquals(Object.getPrototypeOf(o), f.prototype); | |
| 175 o = Reflect.construct(f, [f, 'extra'], f); | |
| 176 assertEquals(Object.getPrototypeOf(o), f.prototype); | |
| 177 assertEquals(2, calls); | |
| 178 | |
| 179 | |
| 180 function g() {} | |
| 181 calls = 0; | |
| 182 o = Reflect.construct(f, [g], g); | |
| 183 assertEquals(Object.getPrototypeOf(o), g.prototype); | |
| 184 o = Reflect.construct(f, [g, 'extra'], g); | |
| 185 assertEquals(Object.getPrototypeOf(o), g.prototype); | |
| 186 assertEquals(2, calls); | |
| 187 })(); | |
| 188 | |
| 189 | |
| 190 (function TestRestParametersFunction() { | |
| 191 function f(...rest) { | |
| 192 assertEquals(rest[0], new.target); | |
| 193 } | |
| 194 | |
| 195 assertInstanceof(new f(f), f); | |
| 196 assertInstanceof(new f(f, 'extra'), f); | |
| 197 })(); | |
| 198 | |
| 199 | |
| 200 (function TestRestParametersClass() { | |
| 201 'use strict'; | |
| 202 | |
| 203 class Base { | |
| 204 constructor(...rest) { | |
| 205 assertEquals(rest[0], new.target); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 assertInstanceof(new Base(Base), Base); | |
| 210 assertInstanceof(new Base(Base, 'extra'), Base); | |
| 211 | |
| 212 class Derived extends Base {} | |
| 213 | |
| 214 assertInstanceof(new Derived(Derived), Derived); | |
| 215 assertInstanceof(new Derived(Derived, 'extra'), Derived); | |
| 216 })(); | |
| 217 | |
| 218 | |
| 219 (function TestArrowFunction() { | |
| 220 function f(expected) { | |
| 221 (() => { | |
| 222 assertEquals(expected, new.target); | |
| 223 })(); | |
| 224 } | |
| 225 | |
| 226 assertInstanceof(new f(f), f); | |
| 227 assertInstanceof(new f(f, 'extra'), f); | |
| 228 })(); | |
| 229 | |
| 230 | |
| 231 (function TestRestParametersClass() { | |
| 232 'use strict'; | |
| 233 | |
| 234 class Base { | |
| 235 constructor(expected) { | |
| 236 (() => { | |
| 237 assertEquals(expected, new.target); | |
| 238 })(); | |
| 239 } | |
| 240 } | |
| 241 | |
| 242 assertInstanceof(new Base(Base), Base); | |
| 243 assertInstanceof(new Base(Base, 'extra'), Base); | |
| 244 | |
| 245 class Derived extends Base {} | |
| 246 | |
| 247 assertInstanceof(new Derived(Derived), Derived); | |
| 248 assertInstanceof(new Derived(Derived, 'extra'), Derived); | |
| 249 })(); | |
| 250 | |
| 251 | |
| 252 (function TestSloppyArguments() { | |
| 253 var length, a0, a1, a2, nt; | |
| 254 function f(x) { | |
| 255 assertEquals(length, arguments.length); | |
| 256 assertEquals(a0, arguments[0]); | |
| 257 assertEquals(a1, arguments[1]); | |
| 258 assertEquals(a2, arguments[2]); | |
| 259 assertEquals(nt, new.target); | |
| 260 | |
| 261 if (length > 0) { | |
| 262 x = 42; | |
| 263 assertEquals(42, x); | |
| 264 assertEquals(42, arguments[0]); | |
| 265 | |
| 266 arguments[0] = 33; | |
| 267 assertEquals(33, x); | |
| 268 assertEquals(33, arguments[0]); | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 nt = f; | |
| 273 length = 0; | |
| 274 new f(); | |
| 275 | |
| 276 length = 1; | |
| 277 a0 = 1; | |
| 278 new f(1); | |
| 279 | |
| 280 length = 2; | |
| 281 a0 = 1; | |
| 282 a1 = 2; | |
| 283 new f(1, 2); | |
| 284 | |
| 285 length = 3; | |
| 286 a0 = 1; | |
| 287 a1 = 2; | |
| 288 a2 = 3; | |
| 289 new f(1, 2, 3); | |
| 290 | |
| 291 nt = undefined; | |
| 292 a0 = a1 = a2 = undefined; | |
| 293 length = 0; | |
| 294 f(); | |
| 295 | |
| 296 length = 1; | |
| 297 a0 = 1; | |
| 298 f(1); | |
| 299 | |
| 300 length = 2; | |
| 301 a0 = 1; | |
| 302 a1 = 2; | |
| 303 f(1, 2); | |
| 304 | |
| 305 length = 3; | |
| 306 a0 = 1; | |
| 307 a1 = 2; | |
| 308 a2 = 3; | |
| 309 f(1, 2, 3); | |
| 310 })(); | |
| 311 | |
| 312 | |
| 313 (function TestStrictArguments() { | |
| 314 var length, a0, a1, a2, nt; | |
| 315 function f(x) { | |
| 316 'use strict'; | |
| 317 assertEquals(length, arguments.length); | |
| 318 assertEquals(a0, arguments[0]); | |
| 319 assertEquals(a1, arguments[1]); | |
| 320 assertEquals(a2, arguments[2]); | |
| 321 assertEquals(nt, new.target); | |
| 322 | |
| 323 if (length > 0) { | |
| 324 x = 42; | |
| 325 assertEquals(a0, arguments[0]); | |
| 326 | |
| 327 arguments[0] = 33; | |
| 328 assertEquals(33, arguments[0]); | |
| 329 } | |
| 330 } | |
| 331 | |
| 332 nt = f; | |
| 333 length = 0; | |
| 334 new f(); | |
| 335 | |
| 336 length = 1; | |
| 337 a0 = 1; | |
| 338 new f(1); | |
| 339 | |
| 340 length = 2; | |
| 341 a0 = 1; | |
| 342 a1 = 2; | |
| 343 new f(1, 2); | |
| 344 | |
| 345 length = 3; | |
| 346 a0 = 1; | |
| 347 a1 = 2; | |
| 348 a2 = 3; | |
| 349 new f(1, 2, 3); | |
| 350 | |
| 351 nt = undefined; | |
| 352 a0 = a1 = a2 = undefined; | |
| 353 length = 0; | |
| 354 f(); | |
| 355 | |
| 356 length = 1; | |
| 357 a0 = 1; | |
| 358 f(1); | |
| 359 | |
| 360 length = 2; | |
| 361 a0 = 1; | |
| 362 a1 = 2; | |
| 363 f(1, 2); | |
| 364 | |
| 365 length = 3; | |
| 366 a0 = 1; | |
| 367 a1 = 2; | |
| 368 a2 = 3; | |
| 369 f(1, 2, 3); | |
| 370 })(); | |
| 371 | |
| 372 | |
| 373 (function TestOtherScopes() { | |
| 374 function f1() { return eval("'use strict'; new.target") } | |
| 375 assertSame(f1, new f1); | |
| 376 function f2() { with ({}) return new.target } | |
| 377 assertSame(f2, new f2); | |
| 378 function f3({a}) { return new.target } | |
| 379 assertSame(f3, new f3({})); | |
| 380 function f4(...a) { return new.target } | |
| 381 assertSame(f4, new f4); | |
| 382 function f5() { 'use strict'; { let x; return new.target } } | |
| 383 assertSame(f5, new f5); | |
| 384 function f6() { with ({'new.target': 42}) return new.target } | |
| 385 assertSame(f6, new f6); | |
| 386 })(); | |
| 387 | |
| 388 | |
| 389 (function TestEarlyErrors() { | |
| 390 assertThrows(function() { Function("new.target = 42"); }, ReferenceError); | |
| 391 assertThrows(function() { Function("var foo = 1; new.target = foo = 42"); }, R
eferenceError); | |
| 392 assertThrows(function() { Function("var foo = 1; foo = new.target = 42"); }, R
eferenceError); | |
| 393 assertThrows(function() { Function("new.target--"); }, ReferenceError); | |
| 394 assertThrows(function() { Function("--new.target"); }, ReferenceError); | |
| 395 assertThrows(function() { Function("(new.target)++"); }, ReferenceError); | |
| 396 assertThrows(function() { Function("++(new.target)"); }, ReferenceError); | |
| 397 assertThrows(function() { Function("for (new.target of {});"); }, SyntaxError)
; | |
| 398 })(); | |
| OLD | NEW |