| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 | 390 |
| 391 // Access to this with formal arguments. | 391 // Access to this with formal arguments. |
| 392 TestGenerator( | 392 TestGenerator( |
| 393 function () { | 393 function () { |
| 394 return ({ x: 42, g: function* (a) { yield this.x } }).g(0); | 394 return ({ x: 42, g: function* (a) { yield this.x } }).g(0); |
| 395 }, | 395 }, |
| 396 [42, undefined], | 396 [42, undefined], |
| 397 "foo", | 397 "foo", |
| 398 [42, undefined]); | 398 [42, undefined]); |
| 399 | 399 |
| 400 // Test that yield* re-yields received results without re-boxing. | 400 // Test that yield* validates iterator results. |
| 401 function TestDelegatingYield() { | 401 function TestDelegatingYield(junk) { |
| 402 function results(results) { | 402 var iterator = {next: () => junk}; |
| 403 var i = 0; | 403 var iterable = {[Symbol.iterator]: () => iterator}; |
| 404 function next() { | 404 function* g() { return yield* iterable }; |
| 405 return results[i++]; | 405 assertThrows(() => g().next(), TypeError); |
| 406 } | |
| 407 var iter = { next: next }; | |
| 408 var ret = {}; | |
| 409 ret[Symbol.iterator] = function() { return iter; }; | |
| 410 return ret; | |
| 411 } | |
| 412 function* yield_results(expected) { | |
| 413 return yield* results(expected); | |
| 414 } | |
| 415 function collect_results(iterable) { | |
| 416 var iter = iterable[Symbol.iterator](); | |
| 417 var ret = []; | |
| 418 var result; | |
| 419 do { | |
| 420 result = iter.next(); | |
| 421 ret.push(result); | |
| 422 } while (!result.done); | |
| 423 return ret; | |
| 424 } | |
| 425 // We have to put a full result for the end, because the return will re-box. | |
| 426 var expected = [{value: 1}, 13, "foo", {value: 34, done: true}]; | |
| 427 | |
| 428 // Sanity check. | |
| 429 assertEquals(expected, collect_results(results(expected))); | |
| 430 assertEquals(expected, collect_results(yield_results(expected))); | |
| 431 } | 406 } |
| 432 TestDelegatingYield(); | 407 TestDelegatingYield(); |
| 408 TestDelegatingYield(null); |
| 409 TestDelegatingYield(42); |
| 410 TestDelegatingYield(true); |
| 433 | 411 |
| 434 function TestTryCatch(instantiate) { | 412 function TestTryCatch(instantiate) { |
| 435 function* g() { yield 1; try { yield 2; } catch (e) { yield e; } yield 3; } | 413 function* g() { yield 1; try { yield 2; } catch (e) { yield e; } yield 3; } |
| 436 function Sentinel() {} | 414 function Sentinel() {} |
| 437 | 415 |
| 438 function Test1(iter) { | 416 function Test1(iter) { |
| 439 assertIteratorResult(1, false, iter.next()); | 417 assertIteratorResult(1, false, iter.next()); |
| 440 assertIteratorResult(2, false, iter.next()); | 418 assertIteratorResult(2, false, iter.next()); |
| 441 assertIteratorResult(3, false, iter.next()); | 419 assertIteratorResult(3, false, iter.next()); |
| 442 assertIteratorIsClosed(iter); | 420 assertIteratorIsClosed(iter); |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 function TestThrowRecursion() { | 652 function TestThrowRecursion() { |
| 675 function* g() { yield iter.throw(1); } | 653 function* g() { yield iter.throw(1); } |
| 676 var iter = g(); | 654 var iter = g(); |
| 677 return iter.next(); | 655 return iter.next(); |
| 678 } | 656 } |
| 679 assertThrows(TestNextRecursion, Error); | 657 assertThrows(TestNextRecursion, Error); |
| 680 assertThrows(TestSendRecursion, Error); | 658 assertThrows(TestSendRecursion, Error); |
| 681 assertThrows(TestThrowRecursion, Error); | 659 assertThrows(TestThrowRecursion, Error); |
| 682 } | 660 } |
| 683 TestRecursion(); | 661 TestRecursion(); |
| 662 |
| 663 |
| 664 // Test yield* on non-iterable objects. |
| 665 function* g(junk) { return yield* junk } |
| 666 var non_iterables = [ |
| 667 42, |
| 668 {[Symbol.iterator]: 42}, |
| 669 {[Symbol.iterator]: () => 42}, |
| 670 {[Symbol.iterator]: () => ({next: 42})}, |
| 671 ]; |
| 672 for (let junk of non_iterables) { |
| 673 assertThrows(() => g(junk).next(), TypeError); |
| 674 } |
| OLD | NEW |