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