OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 (function testSpreadCallsStrict() { | 5 (function testSpreadCallsStrict() { |
6 "use strict" | 6 "use strict" |
7 function countArgs() { return arguments.length; } | 7 function countArgs() { return arguments.length; } |
8 | 8 |
9 // Test this argument | 9 // Test this argument |
10 function returnThis() { return this; } | 10 function returnThis() { return this; } |
11 assertEquals(void 0, returnThis(..."test")); | 11 assertEquals(void 0, returnThis(..."test")); |
12 | 12 |
13 // Test argument counting with different iterables | 13 // Test argument counting with different iterables |
14 assertEquals(0, countArgs(..."")); | 14 assertEquals(0, countArgs(..."")); |
15 assertEquals(4, countArgs(..."test")); | 15 assertEquals(4, countArgs(..."test")); |
16 assertEquals(4, countArgs(..."tes", ..."t")); | 16 assertEquals(4, countArgs(..."tes", ..."t")); |
17 assertEquals(4, countArgs("t", ..."es", "t")); | 17 assertEquals(4, countArgs("t", ..."es", "t")); |
18 assertEquals(4, countArgs("tes", ..."t!!")); | 18 assertEquals(4, countArgs("tes", ..."t!!")); |
19 | 19 |
20 assertEquals(1, countArgs(...[1])); | 20 assertEquals(1, countArgs(...[1])); |
21 assertEquals(2, countArgs(...[1, 2])); | 21 assertEquals(2, countArgs(...[1, 2])); |
22 assertEquals(3, countArgs(...[1, 2, 3])); | 22 assertEquals(3, countArgs(...[1, 2, 3])); |
23 assertEquals(4, countArgs(...[1, 2, 3, 4])); | 23 assertEquals(4, countArgs(...[1, 2, 3, 4])); |
24 assertEquals(5, countArgs(...[1, 2, 3, 4, 5])); | 24 assertEquals(5, countArgs(...[1, 2, 3, 4, 5])); |
25 assertEquals(6, countArgs(...[1, 2, 3, 4, 5, 6])); | 25 assertEquals(6, countArgs(...[1, 2, 3, 4, 5, 6])); |
26 | 26 |
| 27 assertEquals(1, countArgs(...[1.1])); |
| 28 assertEquals(2, countArgs(...[1.1, 2.2])); |
| 29 assertEquals(3, countArgs(...[1.1, 2.2, 3.3])); |
| 30 assertEquals(4, countArgs(...[1.1, 2.2, 3.3, 4.4])); |
| 31 assertEquals(5, countArgs(...[1.1, 2.2, 3.3, 4.4, 5.5])); |
| 32 assertEquals(6, countArgs(...[1.1, 2.2, 3.3, 4.4, 5.5, 6.6])); |
| 33 |
27 assertEquals(1, countArgs(...new Set([1]))); | 34 assertEquals(1, countArgs(...new Set([1]))); |
28 assertEquals(2, countArgs(...new Set([1, 2]))); | 35 assertEquals(2, countArgs(...new Set([1, 2]))); |
29 assertEquals(3, countArgs(...new Set([1, 2, 3]))); | 36 assertEquals(3, countArgs(...new Set([1, 2, 3]))); |
30 assertEquals(4, countArgs(...new Set([1, 2, 3, 4]))); | 37 assertEquals(4, countArgs(...new Set([1, 2, 3, 4]))); |
31 assertEquals(5, countArgs(...new Set([1, 2, 3, 4, 5]))); | 38 assertEquals(5, countArgs(...new Set([1, 2, 3, 4, 5]))); |
32 assertEquals(6, countArgs(...new Set([1, 2, 3, 4, 5, 6]))); | 39 assertEquals(6, countArgs(...new Set([1, 2, 3, 4, 5, 6]))); |
33 | 40 |
34 assertEquals(3, countArgs(...(function*(){ yield 1; yield 2; yield 3; })())); | 41 assertEquals(3, countArgs(...(function*(){ yield 1; yield 2; yield 3; })())); |
35 | 42 |
36 // Test values | 43 // Test values |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 result = fn(a(), ...b(), ...c(), d(), e()); | 346 result = fn(a(), ...b(), ...c(), d(), e()); |
340 assertEquals([undefined, 0, 1, undefined, undefined], result); | 347 assertEquals([undefined, 0, 1, undefined, undefined], result); |
341 assertEquals("ABXYC1C2DE", log); | 348 assertEquals("ABXYC1C2DE", log); |
342 | 349 |
343 log = ""; | 350 log = ""; |
344 result = fn(a(), ...b(), ...c(), d(), e(), ...b(), ...c()); | 351 result = fn(a(), ...b(), ...c(), d(), e(), ...b(), ...c()); |
345 assertEquals([undefined, 0, 1, undefined, undefined, 0, 1], result); | 352 assertEquals([undefined, 0, 1, undefined, undefined, 0, 1], result); |
346 assertEquals("ABXYC1C2DEBXYC1C2", log); | 353 assertEquals("ABXYC1C2DEBXYC1C2", log); |
347 })(); | 354 })(); |
348 | 355 |
| 356 (function testArrayPrototypeHoleGetterModifiesIteratorPrototypeNext() { |
| 357 function sum() { |
| 358 var sum = arguments[0]; |
| 359 for (var i = 1; i < arguments.length; ++i) { |
| 360 sum += arguments[i]; |
| 361 } |
| 362 return sum; |
| 363 } |
| 364 var a = [1, 2]; |
| 365 a[3] = 4; |
| 366 var called = 0; |
| 367 |
| 368 Object.defineProperty(Array.prototype, 2, { |
| 369 get: function() { |
| 370 var ai = a[Symbol.iterator](); |
| 371 var original_next = ai.__proto__["next"]; |
| 372 Object.defineProperty(ai.__proto__, "next", { |
| 373 get: function() { |
| 374 called++; |
| 375 return original_next; |
| 376 } |
| 377 }); |
| 378 return 3; |
| 379 }, |
| 380 configurable: true |
| 381 }); |
| 382 |
| 383 assertEquals(10, sum(...a)); |
| 384 assertEquals(2, called); |
| 385 |
| 386 Object.defineProperty(Array.prototype, 2, {}); |
| 387 })(); |
| 388 |
| 389 (function testArrayHasOtherPrototype() { |
| 390 function countArgs() { return arguments.length; } |
| 391 var a = [1, 2, 3]; |
| 392 var b = {}; |
| 393 Object.defineProperty(b, Symbol.iterator, { |
| 394 value: function*() { |
| 395 yield 4; |
| 396 }, |
| 397 configurable: true |
| 398 }); |
| 399 |
| 400 Object.setPrototypeOf(a, b); |
| 401 |
| 402 assertEquals(1, countArgs(...a)); |
| 403 })(); |
| 404 |
| 405 (function testArrayIteratorPrototypeGetter() { |
| 406 function countArgs() { return arguments.length; } |
| 407 var a = [1, 2, 3]; |
| 408 var ai = a[Symbol.iterator](); |
| 409 var called = 0; |
| 410 |
| 411 var original_next = ai.__proto__["next"]; |
| 412 |
| 413 Object.defineProperty(ai.__proto__, "next", { |
| 414 get: function() { |
| 415 called++; |
| 416 return original_next; |
| 417 } |
| 418 }); |
| 419 |
| 420 countArgs(...a); |
| 421 |
| 422 // should be called 4 times; 3 for the values, 1 for the final |
| 423 // {value: undefined, done: true} pair |
| 424 assertEquals(4, called); |
| 425 })(); |
| 426 |
| 427 (function testArrayIteratorPrototypeModified() { |
| 428 function countArgs() { return arguments.length; } |
| 429 var a = [1,2,3]; |
| 430 var ai = a[Symbol.iterator](); |
| 431 Object.defineProperty(ai.__proto__, "next", { |
| 432 value: function() { |
| 433 return {value: undefined, done: true}; |
| 434 }, |
| 435 configurable: true |
| 436 }); |
| 437 |
| 438 assertEquals(0, countArgs(...a)); |
| 439 |
| 440 })(); |
349 | 441 |
350 (function testCustomArrayPrototypeIterator() { | 442 (function testCustomArrayPrototypeIterator() { |
351 var origIterator = | 443 var origIterator = |
352 Object.getOwnPropertyDescriptor(Array.prototype, Symbol.iterator); | 444 Object.getOwnPropertyDescriptor(Array.prototype, Symbol.iterator); |
353 Object.defineProperty(Array.prototype, Symbol.iterator, { | 445 Object.defineProperty(Array.prototype, Symbol.iterator, { |
354 value: function*() { | 446 value: function*() { |
355 yield 1; | 447 yield 1; |
356 yield 2; | 448 yield 2; |
357 yield 3; | 449 yield 3; |
358 }, | 450 }, |
359 configurable: true | 451 configurable: true |
360 }); | 452 }); |
361 function returnCountStrict() { 'use strict'; return arguments.length; } | 453 function returnCountStrict() { 'use strict'; return arguments.length; } |
362 function returnCountSloppy() { return arguments.length; } | 454 function returnCountSloppy() { return arguments.length; } |
363 | 455 |
364 assertEquals(3, returnCountStrict(...[1])); | 456 assertEquals(3, returnCountStrict(...[1])); |
365 assertEquals(4, returnCountStrict(1, ...[2])); | 457 assertEquals(4, returnCountStrict(1, ...[2])); |
366 assertEquals(5, returnCountStrict(1, ...[2], 3)); | 458 assertEquals(5, returnCountStrict(1, ...[2], 3)); |
367 assertEquals(3, returnCountSloppy(...[1])); | 459 assertEquals(3, returnCountSloppy(...[1])); |
368 assertEquals(4, returnCountSloppy(1, ...[2])); | 460 assertEquals(4, returnCountSloppy(1, ...[2])); |
369 assertEquals(5, returnCountSloppy(1, ...[2], 3)); | 461 assertEquals(5, returnCountSloppy(1, ...[2], 3)); |
370 | 462 |
371 Object.defineProperty(Array.prototype, Symbol.iterator, origIterator); | 463 Object.defineProperty(Array.prototype, Symbol.iterator, origIterator); |
372 })(); | 464 })(); |
| 465 |
| 466 (function testGetPropertyIteratorCalledExactlyOnce() { |
| 467 function countArgs() { return arguments.length; } |
| 468 var a = [1, 2, 3]; |
| 469 var called = 0; |
| 470 |
| 471 Object.defineProperty(Array.prototype, Symbol.iterator, { |
| 472 value: function*() { |
| 473 yield 1; |
| 474 yield 2; |
| 475 }, |
| 476 configurable: true |
| 477 }); |
| 478 |
| 479 var it = a[Symbol.iterator]; |
| 480 Object.defineProperty(a, Symbol.iterator, { |
| 481 get: function() { |
| 482 called++; |
| 483 return it; |
| 484 } |
| 485 }); |
| 486 |
| 487 countArgs(...a); |
| 488 |
| 489 assertEquals(1, called); |
| 490 })(); |
OLD | NEW |