|
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 //----------------------------------------------------------------------------- |
|
7 var BUGNUMBER = 349326; |
|
8 var summary = 'closing generators'; |
|
9 var actual = 'PASS'; |
|
10 var expect = 'PASS'; |
|
11 |
|
12 |
|
13 //----------------------------------------------------------------------------- |
|
14 test(); |
|
15 //----------------------------------------------------------------------------- |
|
16 |
|
17 function test() |
|
18 { |
|
19 enterFunc ('test'); |
|
20 printBugNumber(BUGNUMBER); |
|
21 printStatus (summary); |
|
22 |
|
23 let closed; |
|
24 |
|
25 function gen() |
|
26 { |
|
27 try { |
|
28 yield 1; |
|
29 yield 2; |
|
30 } finally { |
|
31 closed = true; |
|
32 } |
|
33 } |
|
34 |
|
35 // Test that return closes the generator |
|
36 function test_return() |
|
37 { |
|
38 for (let i in gen()) { |
|
39 if (i != 1) |
|
40 throw "unexpected generator value"; |
|
41 return 10; |
|
42 } |
|
43 } |
|
44 |
|
45 closed = false; |
|
46 test_return(); |
|
47 if (closed !== true) |
|
48 throw "return does not close generator"; |
|
49 |
|
50 // test that break closes generator |
|
51 |
|
52 closed = false; |
|
53 for (let i in gen()) { |
|
54 if (i != 1) |
|
55 throw "unexpected generator value"; |
|
56 break; |
|
57 } |
|
58 if (closed !== true) |
|
59 throw "break does not close generator"; |
|
60 |
|
61 label: { |
|
62 for (;;) { |
|
63 closed = false; |
|
64 for (let i in gen()) { |
|
65 if (i != 1) |
|
66 throw "unexpected generator value"; |
|
67 break label; |
|
68 } |
|
69 } |
|
70 } |
|
71 |
|
72 if (closed !== true) |
|
73 throw "break does not close generator"; |
|
74 |
|
75 // test that an exception closes generator |
|
76 |
|
77 function function_that_throws() |
|
78 { |
|
79 throw function_that_throws; |
|
80 } |
|
81 |
|
82 try { |
|
83 closed = false; |
|
84 for (let i in gen()) { |
|
85 if (i != 1) |
|
86 throw "unexpected generator value"; |
|
87 function_that_throws(); |
|
88 } |
|
89 } catch (e) { |
|
90 if (e !== function_that_throws) |
|
91 throw e; |
|
92 } |
|
93 |
|
94 if (closed !== true) |
|
95 throw "exception does not close generator"; |
|
96 |
|
97 // Check consistency of finally execution in presence of generators |
|
98 |
|
99 let gen2_was_closed = false; |
|
100 let gen3_was_closed = false; |
|
101 let finally_was_executed = false; |
|
102 |
|
103 function gen2() { |
|
104 try { |
|
105 yield 2; |
|
106 } finally { |
|
107 if (gen2_was_closed || !finally_was_executed || !gen3_was_closed) |
|
108 throw "bad oder of gen2 finally execution" |
|
109 gen2_was_closed = true; |
|
110 throw gen2; |
|
111 } |
|
112 } |
|
113 |
|
114 function gen3() { |
|
115 try { |
|
116 yield 3; |
|
117 } finally { |
|
118 if (gen2_was_closed || finally_was_executed || gen3_was_closed) |
|
119 throw "bad oder of gen3 finally execution" |
|
120 gen3_was_closed = true; |
|
121 } |
|
122 } |
|
123 |
|
124 label2: { |
|
125 try { |
|
126 for (let i in gen2()) { |
|
127 try { |
|
128 for (let j in gen3()) { |
|
129 break label2; |
|
130 } |
|
131 } finally { |
|
132 if (gen2_was_closed || finally_was_executed || !gen3_was_closed) |
|
133 throw "bad oder of try-finally execution"; |
|
134 finally_was_executed = true; |
|
135 } |
|
136 } |
|
137 throw "gen2 finally should throw"; |
|
138 } catch (e) { |
|
139 if (e != gen2) throw e; |
|
140 } |
|
141 } |
|
142 |
|
143 print("OK"); |
|
144 |
|
145 reportCompare(expect, actual, summary); |
|
146 |
|
147 exitFunc ('test'); |
|
148 } |