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-classes --harmony-new-target --harmony-reflect |
| 6 // Flags: --harmony-rest-parameters --harmony-arrow-functions |
| 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 /* |
| 160 // TODO(arv): Reflect.construct does not work correctly with a third argument. |
| 161 (function TestReflectConstruct() { |
| 162 var calls = 0; |
| 163 function f(expected) { |
| 164 calls++; |
| 165 assertEquals(expected, new.target); |
| 166 } |
| 167 |
| 168 var o = Reflect.construct(f, [f]); |
| 169 assertEquals(Object.getPrototypeOf(o), f.prototype); |
| 170 o = Reflect.construct(f, [f, 'extra']); |
| 171 assertEquals(Object.getPrototypeOf(o), f.prototype); |
| 172 assertEquals(2, calls); |
| 173 |
| 174 calls = 0; |
| 175 o = Reflect.construct(f, [f], f); |
| 176 assertEquals(Object.getPrototypeOf(o), f.prototype); |
| 177 o = Reflect.construct(f, [f, 'extra'], f); |
| 178 assertEquals(Object.getPrototypeOf(o), f.prototype); |
| 179 assertEquals(2, calls); |
| 180 |
| 181 |
| 182 function g() {} |
| 183 calls = 0; |
| 184 o = Reflect.construct(f, [g], g); |
| 185 assertEquals(Object.getPrototypeOf(o), g.prototype); |
| 186 o = Reflect.construct(f, [g, 'extra'], g); |
| 187 assertEquals(Object.getPrototypeOf(o), g.prototype); |
| 188 assertEquals(2, calls); |
| 189 })(); |
| 190 */ |
| 191 |
| 192 |
| 193 (function TestRestParametersFunction() { |
| 194 function f(...rest) { |
| 195 assertEquals(rest[0], new.target); |
| 196 } |
| 197 |
| 198 assertInstanceof(new f(f), f); |
| 199 assertInstanceof(new f(f, 'extra'), f); |
| 200 })(); |
| 201 |
| 202 |
| 203 (function TestRestParametersClass() { |
| 204 'use strict'; |
| 205 |
| 206 class Base { |
| 207 constructor(...rest) { |
| 208 assertEquals(rest[0], new.target); |
| 209 } |
| 210 } |
| 211 |
| 212 assertInstanceof(new Base(Base), Base); |
| 213 assertInstanceof(new Base(Base, 'extra'), Base); |
| 214 |
| 215 class Derived extends Base {} |
| 216 |
| 217 assertInstanceof(new Derived(Derived), Derived); |
| 218 assertInstanceof(new Derived(Derived, 'extra'), Derived); |
| 219 })(); |
| 220 |
| 221 |
| 222 (function TestArrowFunction() { |
| 223 function f(expected) { |
| 224 (() => { |
| 225 assertEquals(expected, new.target); |
| 226 })(); |
| 227 } |
| 228 |
| 229 assertInstanceof(new f(f), f); |
| 230 assertInstanceof(new f(f, 'extra'), f); |
| 231 })(); |
| 232 |
| 233 |
| 234 (function TestRestParametersClass() { |
| 235 'use strict'; |
| 236 |
| 237 class Base { |
| 238 constructor(expected) { |
| 239 (() => { |
| 240 assertEquals(expected, new.target); |
| 241 })(); |
| 242 } |
| 243 } |
| 244 |
| 245 assertInstanceof(new Base(Base), Base); |
| 246 assertInstanceof(new Base(Base, 'extra'), Base); |
| 247 |
| 248 class Derived extends Base {} |
| 249 |
| 250 assertInstanceof(new Derived(Derived), Derived); |
| 251 assertInstanceof(new Derived(Derived, 'extra'), Derived); |
| 252 })(); |
| 253 |
| 254 |
| 255 (function TestSloppyArguments() { |
| 256 var length, a0, a1, a2, nt; |
| 257 function f(x) { |
| 258 assertEquals(length, arguments.length); |
| 259 assertEquals(a0, arguments[0]); |
| 260 assertEquals(a1, arguments[1]); |
| 261 assertEquals(a2, arguments[2]); |
| 262 assertEquals(nt, new.target); |
| 263 |
| 264 if (length > 0) { |
| 265 x = 42; |
| 266 assertEquals(42, x); |
| 267 assertEquals(42, arguments[0]); |
| 268 |
| 269 arguments[0] = 33; |
| 270 assertEquals(33, x); |
| 271 assertEquals(33, arguments[0]); |
| 272 } |
| 273 } |
| 274 |
| 275 nt = f; |
| 276 length = 0; |
| 277 new f(); |
| 278 |
| 279 length = 1; |
| 280 a0 = 1; |
| 281 new f(1); |
| 282 |
| 283 length = 2; |
| 284 a0 = 1; |
| 285 a1 = 2; |
| 286 new f(1, 2); |
| 287 |
| 288 length = 3; |
| 289 a0 = 1; |
| 290 a1 = 2; |
| 291 a2 = 3; |
| 292 new f(1, 2, 3); |
| 293 |
| 294 nt = undefined; |
| 295 a0 = a1 = a2 = undefined; |
| 296 length = 0; |
| 297 f(); |
| 298 |
| 299 length = 1; |
| 300 a0 = 1; |
| 301 f(1); |
| 302 |
| 303 length = 2; |
| 304 a0 = 1; |
| 305 a1 = 2; |
| 306 f(1, 2); |
| 307 |
| 308 length = 3; |
| 309 a0 = 1; |
| 310 a1 = 2; |
| 311 a2 = 3; |
| 312 f(1, 2, 3); |
| 313 })(); |
| 314 |
| 315 |
| 316 (function TestStrictArguments() { |
| 317 var length, a0, a1, a2, nt; |
| 318 function f(x) { |
| 319 'use strict'; |
| 320 assertEquals(length, arguments.length); |
| 321 assertEquals(a0, arguments[0]); |
| 322 assertEquals(a1, arguments[1]); |
| 323 assertEquals(a2, arguments[2]); |
| 324 assertEquals(nt, new.target); |
| 325 |
| 326 if (length > 0) { |
| 327 x = 42; |
| 328 assertEquals(a0, arguments[0]); |
| 329 |
| 330 arguments[0] = 33; |
| 331 assertEquals(33, arguments[0]); |
| 332 } |
| 333 } |
| 334 |
| 335 nt = f; |
| 336 length = 0; |
| 337 new f(); |
| 338 |
| 339 length = 1; |
| 340 a0 = 1; |
| 341 new f(1); |
| 342 |
| 343 length = 2; |
| 344 a0 = 1; |
| 345 a1 = 2; |
| 346 new f(1, 2); |
| 347 |
| 348 length = 3; |
| 349 a0 = 1; |
| 350 a1 = 2; |
| 351 a2 = 3; |
| 352 new f(1, 2, 3); |
| 353 |
| 354 nt = undefined; |
| 355 a0 = a1 = a2 = undefined; |
| 356 length = 0; |
| 357 f(); |
| 358 |
| 359 length = 1; |
| 360 a0 = 1; |
| 361 f(1); |
| 362 |
| 363 length = 2; |
| 364 a0 = 1; |
| 365 a1 = 2; |
| 366 f(1, 2); |
| 367 |
| 368 length = 3; |
| 369 a0 = 1; |
| 370 a1 = 2; |
| 371 a2 = 3; |
| 372 f(1, 2, 3); |
| 373 })(); |
OLD | NEW |