Debian Codegraph @

rhino/testsrc/tests/js1_8/genexps/regress-349326.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 var gTestfile = 'regress-349326.js';
7 //-----------------------------------------------------------------------------
8 var BUGNUMBER = 349326;
9 var summary = 'closing generators';
10 var actual = 'PASS';
11 var expect = 'PASS';
12
13
14 //-----------------------------------------------------------------------------
15 test();
16 //-----------------------------------------------------------------------------
17
18 function test()
19 {
20 enterFunc ('test');
21 printBugNumber(BUGNUMBER);
22 printStatus (summary);
23
24 let closed;
25
26 function gen()
27 {
28 try {
29 yield 1;
30 yield 2;
31 } finally {
32 closed = true;
33 }
34 }
35
36 // Test that return closes the generator
37 function test_return()
38 {
39 for (let i in gen()) {
40 if (i != 1)
41 throw "unexpected generator value";
42 return 10;
43 }
44 }
45
46 closed = false;
47 test_return();
48 if (closed !== true)
49 throw "return does not close generator";
50
51 // test that break closes generator
52
53 closed = false;
54 for (let i in gen()) {
55 if (i != 1)
56 throw "unexpected generator value";
57 break;
58 }
59 if (closed !== true)
60 throw "break does not close generator";
61
62 label: {
63 for (;;) {
64 closed = false;
65 for (let i in gen()) {
66 if (i != 1)
67 throw "unexpected generator value";
68 break label;
69 }
70 }
71 }
72
73 if (closed !== true)
74 throw "break does not close generator";
75
76 // test that an exception closes generator
77
78 function function_that_throws()
79 {
80 throw function_that_throws;
81 }
82
83 try {
84 closed = false;
85 for (let i in gen()) {
86 if (i != 1)
87 throw "unexpected generator value";
88 function_that_throws();
89 }
90 } catch (e) {
91 if (e !== function_that_throws)
92 throw e;
93 }
94
95 if (closed !== true)
96 throw "exception does not close generator";
97
98 // Check consistency of finally execution in presence of generators
99
100 let gen2_was_closed = false;
101 let gen3_was_closed = false;
102 let finally_was_executed = false;
103
104 function gen2() {
105 try {
106 yield 2;
107 } finally {
108 if (gen2_was_closed || !finally_was_executed || !gen3_was_closed)
109 throw "bad oder of gen2 finally execution"
110 gen2_was_closed = true;
111 throw gen2;
112 }
113 }
114
115 function gen3() {
116 try {
117 yield 3;
118 } finally {
119 if (gen2_was_closed || finally_was_executed || gen3_was_closed)
120 throw "bad oder of gen3 finally execution"
121 gen3_was_closed = true;
122 }
123 }
124
125 label2: {
126 try {
127 for (let i in gen2()) {
128 try {
129 for (let j in gen3()) {
130 break label2;
131 }
132 } finally {
133 if (gen2_was_closed || finally_was_executed || !gen3_was_closed)
134 throw "bad oder of try-finally execution";
135 finally_was_executed = true;
136 }
137 }
138 throw "gen2 finally should throw";
139 } catch (e) {
140 if (e != gen2) throw e;
141 }
142 }
143
144 print("OK");
145
146 reportCompare(expect, actual, summary);
147
148 exitFunc ('test');
149 }