Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 1 | // Copyright 2016 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 | // Flags: --harmony-iterator-close |
| 6 | |
| 7 | function* g() { yield 42; return 88 }; |
| 8 | |
| 9 | |
| 10 | // Return method is "undefined". |
| 11 | { |
| 12 | g.prototype.return = null; |
| 13 | |
| 14 | assertEquals(undefined, (() => { |
| 15 | for (let x of g()) { break; } |
| 16 | })()); |
| 17 | |
| 18 | assertEquals(undefined, (() => { |
| 19 | for (x of g()) { break; } |
| 20 | })()); |
| 21 | |
| 22 | assertThrowsEquals(() => { |
| 23 | for (let x of g()) { throw 42; } |
| 24 | }, 42); |
| 25 | |
| 26 | assertThrowsEquals(() => { |
| 27 | for (x of g()) { throw 42; } |
| 28 | }, 42); |
| 29 | |
| 30 | assertEquals(42, (() => { |
| 31 | for (let x of g()) { return 42; } |
| 32 | })()); |
| 33 | |
| 34 | assertEquals(42, (() => { |
| 35 | for (x of g()) { return 42; } |
| 36 | })()); |
| 37 | |
| 38 | assertEquals(42, eval('for (let x of g()) { x; }')); |
| 39 | |
| 40 | assertEquals(42, eval('for (let x of g()) { x; }')); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | // Return method is not callable. |
| 45 | { |
| 46 | g.prototype.return = 666; |
| 47 | |
| 48 | assertThrows(() => { |
| 49 | for (let x of g()) { break; } |
| 50 | }, TypeError); |
| 51 | |
| 52 | assertThrows(() => { |
| 53 | for (x of g()) { break; } |
| 54 | }, TypeError); |
| 55 | |
| 56 | assertThrows(() => { |
| 57 | for (let x of g()) { throw 666; } |
| 58 | }, TypeError); |
| 59 | |
| 60 | assertThrows(() => { |
| 61 | for (x of g()) { throw 666; } |
| 62 | }, TypeError); |
| 63 | |
| 64 | assertThrows(() => { |
| 65 | for (let x of g()) { return 666; } |
| 66 | }, TypeError); |
| 67 | |
| 68 | assertThrows(() => { |
| 69 | for (x of g()) { return 666; } |
| 70 | }, TypeError); |
| 71 | |
| 72 | assertEquals(42, eval('for (let x of g()) { x; }')); |
| 73 | |
| 74 | assertEquals(42, eval('for (let x of g()) { x; }')); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | // Return method does not return an object. |
| 79 | { |
| 80 | g.prototype.return = () => 666; |
| 81 | |
| 82 | assertThrows(() => { |
| 83 | for (let x of g()) { break; } |
| 84 | }, TypeError); |
| 85 | |
| 86 | assertThrows(() => { |
| 87 | for (x of g()) { break; } |
| 88 | }, TypeError); |
| 89 | |
| 90 | assertThrows(() => { |
| 91 | for (let x of g()) { throw 666; } |
| 92 | }, TypeError); |
| 93 | |
| 94 | assertThrows(() => { |
| 95 | for (x of g()) { throw 666; } |
| 96 | }, TypeError); |
| 97 | |
| 98 | assertThrows(() => { |
| 99 | for (let x of g()) { return 666; } |
| 100 | }, TypeError); |
| 101 | |
| 102 | assertThrows(() => { |
| 103 | for (x of g()) { return 666; } |
| 104 | }, TypeError); |
| 105 | |
| 106 | assertEquals(42, eval('for (let x of g()) { x; }')); |
| 107 | |
| 108 | assertEquals(42, eval('for (x of g()) { x; }')); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | // Return method returns an object. |
| 113 | { |
| 114 | let log = []; |
| 115 | g.prototype.return = (...args) => { log.push(args); return {} }; |
| 116 | |
| 117 | log = []; |
| 118 | for (let x of g()) { break; } |
| 119 | assertEquals([[]], log); |
| 120 | |
| 121 | log = []; |
| 122 | for (x of g()) { break; } |
| 123 | assertEquals([[]], log); |
| 124 | |
| 125 | log = []; |
| 126 | assertThrowsEquals(() => { |
| 127 | for (let x of g()) { throw 42; } |
| 128 | }, 42); |
| 129 | assertEquals([[]], log); |
| 130 | |
| 131 | log = []; |
| 132 | assertThrowsEquals(() => { |
| 133 | for (x of g()) { throw 42; } |
| 134 | }, 42); |
| 135 | assertEquals([[]], log); |
| 136 | |
| 137 | log = []; |
| 138 | assertEquals(42, (() => { |
| 139 | for (let x of g()) { return 42; } |
| 140 | })()); |
| 141 | assertEquals([[]], log); |
| 142 | |
| 143 | log = []; |
| 144 | assertEquals(42, (() => { |
| 145 | for (x of g()) { return 42; } |
| 146 | })()); |
| 147 | assertEquals([[]], log); |
| 148 | |
| 149 | log = []; |
| 150 | assertEquals(42, eval('for (let x of g()) { x; }')); |
| 151 | assertEquals([], log); |
| 152 | |
| 153 | log = []; |
| 154 | assertEquals(42, eval('for (x of g()) { x; }')); |
| 155 | assertEquals([], log); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | // Return method throws. |
| 160 | { |
| 161 | let log = []; |
| 162 | g.prototype.return = (...args) => { log.push(args); throw 23 }; |
| 163 | |
| 164 | log = []; |
| 165 | assertThrowsEquals(() => { |
| 166 | for (let x of g()) { break; } |
| 167 | }, 23); |
| 168 | assertEquals([[]], log); |
| 169 | |
| 170 | log = []; |
| 171 | assertThrowsEquals(() => { |
| 172 | for (x of g()) { break; } |
| 173 | }, 23); |
| 174 | assertEquals([[]], log); |
| 175 | |
| 176 | log = []; |
| 177 | assertThrowsEquals(() => { |
| 178 | for (let x of g()) { throw 42; } |
| 179 | }, 42); |
| 180 | assertEquals([[]], log); |
| 181 | |
| 182 | log = []; |
| 183 | assertThrowsEquals(() => { |
| 184 | for (x of g()) { throw 42; } |
| 185 | }, 42); |
| 186 | assertEquals([[]], log); |
| 187 | |
| 188 | log = []; |
| 189 | assertThrowsEquals(() => { |
| 190 | for (let x of g()) { return 42; } |
| 191 | }, 23); |
| 192 | assertEquals([[]], log); |
| 193 | |
| 194 | log = []; |
| 195 | assertThrowsEquals(() => { |
| 196 | for (x of g()) { return 42; } |
| 197 | }, 23); |
| 198 | assertEquals([[]], log); |
| 199 | |
| 200 | log = []; |
| 201 | assertEquals(42, eval('for (let x of g()) { x; }')); |
| 202 | assertEquals([], log); |
| 203 | |
| 204 | log = []; |
| 205 | assertEquals(42, eval('for (x of g()) { x; }')); |
| 206 | assertEquals([], log); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | // Next method throws. |
| 211 | { |
| 212 | g.prototype.next = () => { throw 666; }; |
| 213 | g.prototype.return = () => { assertUnreachable() }; |
| 214 | |
| 215 | assertThrowsEquals(() => { |
| 216 | for (let x of g()) {} |
| 217 | }, 666); |
| 218 | |
| 219 | assertThrowsEquals(() => { |
| 220 | for (x of g()) {} |
| 221 | }, 666); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | // Nested loops. |
| 226 | { |
| 227 | function* g1() { yield 1; yield 2; throw 3; } |
| 228 | function* g2() { yield -1; yield -2; throw -3; } |
| 229 | |
| 230 | assertDoesNotThrow(() => { |
| 231 | for (let x of g1()) { |
| 232 | for (let y of g2()) { |
| 233 | if (y == -2) break; |
| 234 | } |
| 235 | if (x == 2) break; |
| 236 | } |
| 237 | }, -3); |
| 238 | |
| 239 | assertThrowsEquals(() => { |
| 240 | for (let x of g1()) { |
| 241 | for (let y of g2()) { |
| 242 | } |
| 243 | } |
| 244 | }, -3); |
| 245 | |
| 246 | assertThrowsEquals(() => { |
| 247 | for (let x of g1()) { |
| 248 | for (let y of g2()) { |
| 249 | if (y == -2) break; |
| 250 | } |
| 251 | } |
| 252 | }, 3); |
| 253 | |
| 254 | assertDoesNotThrow(() => { |
| 255 | l: for (let x of g1()) { |
| 256 | for (let y of g2()) { |
| 257 | if (y == -2) break l; |
| 258 | } |
| 259 | } |
| 260 | }); |
| 261 | |
| 262 | assertThrowsEquals(() => { |
| 263 | for (let x of g1()) { |
| 264 | for (let y of g2()) { |
| 265 | throw 4; |
| 266 | } |
| 267 | } |
| 268 | }, 4); |
| 269 | |
| 270 | assertThrowsEquals(() => { |
| 271 | for (let x of g1()) { |
| 272 | for (let y of g2()) { |
| 273 | if (y == -2) throw 4; |
| 274 | } |
| 275 | } |
| 276 | }, 4); |
| 277 | |
| 278 | let log = []; |
| 279 | g1.prototype.return = () => { log.push(1); throw 5 }; |
| 280 | g2.prototype.return = () => { log.push(2); throw -5 }; |
| 281 | |
| 282 | log = []; |
| 283 | assertThrowsEquals(() => { |
| 284 | for (let x of g1()) { |
| 285 | for (let y of g2()) { |
| 286 | if (y == -2) break; |
| 287 | } |
| 288 | if (x == 2) break; |
| 289 | } |
| 290 | }, -5); |
| 291 | assertEquals([2, 1], log); |
| 292 | |
| 293 | log = []; |
| 294 | assertThrowsEquals(() => { |
| 295 | for (let x of g1()) { |
| 296 | for (let y of g2()) { |
| 297 | } |
| 298 | } |
| 299 | }, -3); |
| 300 | assertEquals([1], log); |
| 301 | |
| 302 | log = []; |
| 303 | assertThrowsEquals(() => { |
| 304 | for (let x of g1()) { |
| 305 | for (let y of g2()) { |
| 306 | if (y == -2) break; |
| 307 | } |
| 308 | } |
| 309 | }, -5); |
| 310 | assertEquals([2, 1], log); |
| 311 | |
| 312 | log = []; |
| 313 | assertThrowsEquals(() => { |
| 314 | l: for (let x of g1()) { |
| 315 | for (let y of g2()) { |
| 316 | if (y == -2) break l; |
| 317 | } |
| 318 | } |
| 319 | }, -5); |
| 320 | assertEquals([2, 1], log); |
| 321 | |
| 322 | log = []; |
| 323 | assertThrowsEquals(() => { |
| 324 | for (let x of g1()) { |
| 325 | for (let y of g2()) { |
| 326 | throw 4; |
| 327 | } |
| 328 | } |
| 329 | }, 4); |
| 330 | assertEquals([2, 1], log); |
| 331 | |
| 332 | log = []; |
| 333 | assertThrowsEquals(() => { |
| 334 | for (let x of g1()) { |
| 335 | for (let y of g2()) { |
| 336 | if (y == -2) throw 4; |
| 337 | } |
| 338 | } |
| 339 | }, 4); |
| 340 | assertEquals([2, 1], log); |
| 341 | |
| 342 | log = []; |
| 343 | assertThrowsEquals(() => { |
| 344 | for (let x of g1()) { |
| 345 | try { |
| 346 | for (let y of g2()) { |
| 347 | } |
| 348 | } catch (_) {} |
| 349 | } |
| 350 | }, 3); |
| 351 | assertEquals([], log); |
| 352 | |
| 353 | log = []; |
| 354 | assertThrowsEquals(() => { |
| 355 | for (let x of g1()) { |
| 356 | try { |
| 357 | for (let y of g2()) { |
| 358 | } |
| 359 | } catch (_) {} |
| 360 | if (x == 2) break; |
| 361 | } |
| 362 | }, 5); |
| 363 | assertEquals([1], log); |
| 364 | } |