1// Copyright JS Foundation and other contributors, http://js.foundation 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15function createIterable(arr, methods = {}) { 16 let iterable = function *() { 17 let idx = 0; 18 while (idx < arr.length) { 19 yield arr[idx]; 20 idx++; 21 } 22 }(); 23 iterable['return'] = methods['return']; 24 iterable['throw'] = methods['throw']; 25 26 return iterable; 27}; 28 29function close1() { 30 var closed = false; 31 var iter = createIterable([1, 2, 3], { 32 'return': function() { closed = true; return {}; } 33 }); 34 for (var it of iter) break; 35 return closed; 36} 37 38assert(close1()); 39 40function close2() { 41 var closed = false; 42 var iter = createIterable([1, 2, 3], { 43 'return': function() { closed = true; return {}; } 44 }); 45 try { 46 for (var it of iter) throw 0; 47 assert(false); 48 } catch(e){ 49 assert(e === 0); 50 } 51 return closed; 52} 53 54assert(close2()); 55 56function close3() { 57 var closed = false; 58 var iter = createIterable([1, 2, 3], { 59 'return': function() { closed = true; return {}; } 60 }); 61 for (var it of iter) continue; 62 return closed; 63} 64 65assert(!close3()); 66 67function close4() { 68 var closed = false; 69 var iter = createIterable([1, 2, 3], { 70 'return': function() { closed = true; throw 6; } 71 }); 72 try { 73 for (var it of iter) throw 5; 74 assert(false); 75 } catch(e) { 76 assert(e === 5); 77 } 78 return closed; 79} 80 81assert(close4()); 82 83function close5() { 84 var closed_called = 0; 85 var iter = createIterable([1, 2, 3], { 86 'return': function() { closed_called++; throw 6; } 87 }); 88 try { 89 for (var it of iter) { 90 for (var it of iter) { 91 throw 5; 92 } 93 assert(false); 94 } 95 assert(false); 96 } catch(e) { 97 assert(e === 5); 98 } 99 return closed_called === 2; 100} 101 102assert(close5()); 103 104function close6() { 105 var closed = false; 106 var iter = createIterable([1, 2, 3], { 107 'return': function() { closed = true; return {}; } 108 }); 109 for (var it of iter) {}; 110 111 return closed; 112} 113 114assert(!close6()); 115 116var close7_result = false; 117function close7() { 118 var iter = createIterable([1, 2, 3], { 119 'return': function() { close7_result = true; throw "5"; } 120 }); 121 122 for (var it of iter) { 123 return "foo"; 124 } 125} 126 127try { 128 close7(); 129 assert(false); 130} catch (e) { 131 assert(close7_result); 132 assert(e === "5"); 133} 134 135function close8() { 136 var iter = createIterable([1, 2, 3], { 137 'return': function() { close8_result = true; throw "5"; } 138 }); 139 140 for (var it of iter) { 141 throw "foo"; 142 } 143} 144 145var close8_result = false; 146try { 147 close8(); 148 assert(false); 149} catch (e) { 150 assert(e === "foo"); 151 assert(close8_result); 152} 153 154function close9() { 155 var closed = false; 156 var iter = createIterable([1, 2, 3], { 157 'return': function() { closed = true; throw "5"; } 158 }); 159 160 try { 161 for (var it of iter) { 162 break; 163 } 164 } finally { 165 assert(closed); 166 throw "foo" 167 } 168} 169 170try { 171 close9(); 172 assert(false); 173} catch (e) { 174 assert(e === "foo"); 175} 176 177function close10() { 178 var closed = false; 179 var iter = createIterable([1, 2, 3], { 180 'return': function() { closed = true; return {}; } 181 }); 182 183 try { 184 for (var it of iter) { 185 return "foo"; 186 } 187 } finally { 188 assert(closed); 189 throw "bar"; 190 } 191} 192 193try { 194 close10(); 195 assert(false); 196} catch (e) { 197 assert(e === "bar"); 198} 199 200function close11() { 201 var closed = false; 202 var iter = createIterable([1, 2, 3], { 203 'return': function() { closed = true; throw "5"; } 204 }); 205 206 try { 207 for (var it of iter) { 208 return "foo"; 209 } 210 } finally { 211 assert(closed); 212 throw "bar"; 213 } 214} 215 216try { 217 close11(); 218 assert(false); 219} catch (e) { 220 assert(e === "bar"); 221} 222 223function close12() { 224 var closed = false; 225 var iter = createIterable([1, 2, 3], { 226 'return': function() { closed = true; throw "5"; } 227 }); 228 229 try { 230 for (var it of iter) { 231 throw "foo"; 232 } 233 } finally { 234 assert(closed); 235 throw "bar"; 236 } 237} 238 239try { 240 close12(); 241 assert(false); 242} catch (e) { 243 assert(e === "bar"); 244} 245