blob: 0a2173a919fb9d24eadb1cc90dd6f29641d67822 [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// 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
7function Foo() {}
8function Bar() {}
9
10function assertIteratorResult(value, done, result) {
11 assertEquals({ value: value, done: done}, result);
12}
13
14function 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
21var iter;
22function* nextGenerator() { yield iter.next(); }
23function* throwGenerator() { yield iter.throw(new Bar); }
24
25// Throw on a suspendedStart iterator.
26iter = nextGenerator();
27assertThrows(function() { iter.throw(new Foo) }, Foo)
28assertThrows(function() { iter.throw(new Foo) }, Foo)
29assertIteratorIsClosed(iter);
30
31// The same.
32iter = throwGenerator();
33assertThrows(function() { iter.throw(new Foo) }, Foo)
34assertThrows(function() { iter.throw(new Foo) }, Foo)
35assertIteratorIsClosed(iter);
36
37// Next on an executing iterator raises a TypeError.
38iter = nextGenerator();
39assertThrows(function() { iter.next() }, TypeError)
40assertIteratorIsClosed(iter);
41
42// Throw on an executing iterator raises a TypeError.
43iter = throwGenerator();
44assertThrows(function() { iter.next() }, TypeError)
45assertIteratorIsClosed(iter);
46
47// Next on an executing iterator doesn't change the state of the
48// generator.
49iter = (function* () {
50 try {
51 iter.next();
52 yield 1;
53 } catch (e) {
54 try {
55 // This next() should raise the same exception, because the first
56 // next() left the iter in the executing state.
57 iter.next();
58 yield 2;
59 } catch (e) {
60 yield 3;
61 }
62 }
63 yield 4;
64})();
65assertIteratorResult(3, false, iter.next());
66assertIteratorResult(4, false, iter.next());
67assertIteratorIsClosed(iter);