Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1 | // Copyright 2014 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 | // Test generator states. |
| 6 | |
| 7 | function Foo() {} |
| 8 | function Bar() {} |
| 9 | |
| 10 | function assertIteratorResult(value, done, result) { |
| 11 | assertEquals({ value: value, done: done}, result); |
| 12 | } |
| 13 | |
| 14 | function assertIteratorIsClosed(iter) { |
| 15 | assertIteratorResult(undefined, true, iter.next()); |
| 16 | // Next and throw on a closed iterator. |
| 17 | assertDoesNotThrow(function() { iter.next(); }); |
| 18 | assertThrows(function() { iter.throw(new Bar); }, Bar); |
| 19 | } |
| 20 | |
| 21 | var iter; |
| 22 | function* nextGenerator() { yield iter.next(); } |
| 23 | function* throwGenerator() { yield iter.throw(new Bar); } |
| 24 | |
| 25 | // Throw on a suspendedStart iterator. |
| 26 | iter = nextGenerator(); |
| 27 | assertThrows(function() { iter.throw(new Foo) }, Foo) |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 28 | assertIteratorIsClosed(iter); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 29 | assertThrows(function() { iter.throw(new Foo) }, Foo) |
| 30 | assertIteratorIsClosed(iter); |
| 31 | |
| 32 | // The same. |
| 33 | iter = throwGenerator(); |
| 34 | assertThrows(function() { iter.throw(new Foo) }, Foo) |
| 35 | assertThrows(function() { iter.throw(new Foo) }, Foo) |
| 36 | assertIteratorIsClosed(iter); |
| 37 | |
| 38 | // Next on an executing iterator raises a TypeError. |
| 39 | iter = nextGenerator(); |
| 40 | assertThrows(function() { iter.next() }, TypeError) |
| 41 | assertIteratorIsClosed(iter); |
| 42 | |
| 43 | // Throw on an executing iterator raises a TypeError. |
| 44 | iter = throwGenerator(); |
| 45 | assertThrows(function() { iter.next() }, TypeError) |
| 46 | assertIteratorIsClosed(iter); |
| 47 | |
| 48 | // Next on an executing iterator doesn't change the state of the |
| 49 | // generator. |
| 50 | iter = (function* () { |
| 51 | try { |
| 52 | iter.next(); |
| 53 | yield 1; |
| 54 | } catch (e) { |
| 55 | try { |
| 56 | // This next() should raise the same exception, because the first |
| 57 | // next() left the iter in the executing state. |
| 58 | iter.next(); |
| 59 | yield 2; |
| 60 | } catch (e) { |
| 61 | yield 3; |
| 62 | } |
| 63 | } |
| 64 | yield 4; |
| 65 | })(); |
| 66 | assertIteratorResult(3, false, iter.next()); |
| 67 | assertIteratorResult(4, false, iter.next()); |
| 68 | assertIteratorIsClosed(iter); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 69 | |
| 70 | |
| 71 | // A return that doesn't close. |
| 72 | { |
| 73 | let g = function*() { try {return 42} finally {yield 43} }; |
| 74 | |
| 75 | let x = g(); |
| 76 | assertEquals({value: 43, done: false}, x.next()); |
| 77 | assertEquals({value: 42, done: true}, x.next()); |
| 78 | } |
| 79 | { |
| 80 | let x; |
| 81 | let g = function*() { try {return 42} finally {x.throw(666)} }; |
| 82 | |
| 83 | x = g(); |
| 84 | assertThrows(() => x.next(), TypeError); // Still executing. |
| 85 | } |
| 86 | { |
| 87 | let x; |
| 88 | let g = function*() { |
| 89 | try {return 42} finally {try {x.throw(666)} catch(e) {}} |
| 90 | }; |
| 91 | |
| 92 | x = g(); |
| 93 | assertEquals({value: 42, done: true}, x.next()); |
| 94 | } |