OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 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 // Test generator states. | 5 // Test generator states. |
6 | 6 |
7 function Foo() {} | 7 function Foo() {} |
8 function Bar() {} | 8 function Bar() {} |
9 | 9 |
10 function assertIteratorResult(value, done, result) { | 10 function assertIteratorResult(value, done, result) { |
11 assertEquals({ value: value, done: done}, result); | 11 assertEquals({ value: value, done: done}, result); |
12 } | 12 } |
13 | 13 |
14 function assertIteratorIsClosed(iter) { | 14 function assertIteratorIsClosed(iter) { |
15 assertIteratorResult(undefined, true, iter.next()); | 15 assertIteratorResult(undefined, true, iter.next()); |
16 // Next and throw on a closed iterator. | 16 // Next and throw on a closed iterator. |
17 assertDoesNotThrow(function() { iter.next(); }); | 17 assertDoesNotThrow(function() { iter.next(); }); |
18 assertThrows(function() { iter.throw(new Bar); }, Bar); | 18 assertThrows(function() { iter.throw(new Bar); }, Bar); |
19 } | 19 } |
20 | 20 |
21 var iter; | 21 var iter; |
22 function* nextGenerator() { yield iter.next(); } | 22 function* nextGenerator() { yield iter.next(); } |
23 function* throwGenerator() { yield iter.throw(new Bar); } | 23 function* throwGenerator() { yield iter.throw(new Bar); } |
24 | 24 |
25 // Throw on a suspendedStart iterator. | 25 // Throw on a suspendedStart iterator. |
26 iter = nextGenerator(); | 26 iter = nextGenerator(); |
27 assertThrows(function() { iter.throw(new Foo) }, Foo) | 27 assertThrows(function() { iter.throw(new Foo) }, Foo) |
| 28 assertIteratorIsClosed(iter); |
28 assertThrows(function() { iter.throw(new Foo) }, Foo) | 29 assertThrows(function() { iter.throw(new Foo) }, Foo) |
29 assertIteratorIsClosed(iter); | 30 assertIteratorIsClosed(iter); |
30 | 31 |
31 // The same. | 32 // The same. |
32 iter = throwGenerator(); | 33 iter = throwGenerator(); |
33 assertThrows(function() { iter.throw(new Foo) }, Foo) | 34 assertThrows(function() { iter.throw(new Foo) }, Foo) |
34 assertThrows(function() { iter.throw(new Foo) }, Foo) | 35 assertThrows(function() { iter.throw(new Foo) }, Foo) |
35 assertIteratorIsClosed(iter); | 36 assertIteratorIsClosed(iter); |
36 | 37 |
37 // Next on an executing iterator raises a TypeError. | 38 // Next on an executing iterator raises a TypeError. |
(...skipping 20 matching lines...) Expand all Loading... |
58 yield 2; | 59 yield 2; |
59 } catch (e) { | 60 } catch (e) { |
60 yield 3; | 61 yield 3; |
61 } | 62 } |
62 } | 63 } |
63 yield 4; | 64 yield 4; |
64 })(); | 65 })(); |
65 assertIteratorResult(3, false, iter.next()); | 66 assertIteratorResult(3, false, iter.next()); |
66 assertIteratorResult(4, false, iter.next()); | 67 assertIteratorResult(4, false, iter.next()); |
67 assertIteratorIsClosed(iter); | 68 assertIteratorIsClosed(iter); |
| 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 } |
OLD | NEW |