|
1 // This file was written by Andy Wingo <wingo@igalia.com> and originally |
|
2 // contributed to V8 as generators-objects.js, available here: |
|
3 // |
|
4 // http://code.google.com/p/v8/source/browse/branches/bleeding_edge/test/mjsunit/harmony/generators-objects.js |
|
5 |
|
6 // Test aspects of the generator runtime. |
|
7 |
|
8 |
|
9 var GeneratorFunction = (function*(){yield 1;}).constructor; |
|
10 |
|
11 |
|
12 function TestGeneratorResultPrototype() { |
|
13 function* g() { yield 1; } |
|
14 var iter = g(); |
|
15 assertIteratorNext(iter, 1); |
|
16 assertIteratorDone(iter, undefined); |
|
17 assertIteratorDone(iter, undefined); |
|
18 } |
|
19 TestGeneratorResultPrototype(); |
|
20 |
|
21 function TestGenerator(g, expected_values_for_next, |
|
22 send_val, expected_values_for_send) { |
|
23 function testNext(thunk) { |
|
24 var iter = thunk(); |
|
25 for (var i = 0; i < expected_values_for_next.length; i++) { |
|
26 assertIteratorResult(iter.next(), expected_values_for_next[i], |
|
27 i == expected_values_for_next.length - 1); |
|
28 } |
|
29 assertIteratorDone(iter, undefined); |
|
30 } |
|
31 function testSend(thunk) { |
|
32 var iter = thunk(); |
|
33 for (var i = 0; i < expected_values_for_send.length; i++) { |
|
34 assertIteratorResult(i ? iter.next(send_val) : iter.next(), |
|
35 expected_values_for_send[i], |
|
36 i == expected_values_for_send.length - 1); |
|
37 } |
|
38 assertIteratorDone(iter, undefined); |
|
39 } |
|
40 function testThrow(thunk) { |
|
41 for (var i = 0; i < expected_values_for_next.length; i++) { |
|
42 var iter = thunk(); |
|
43 for (var j = 0; j < i; j++) { |
|
44 assertIteratorResult(iter.next(), |
|
45 expected_values_for_next[j], |
|
46 j == expected_values_for_next.length - 1); |
|
47 } |
|
48 var Sentinel = function () {} |
|
49 assertThrowsInstanceOf(function () { iter.throw(new Sentinel); }, Sentinel); |
|
50 assertIteratorDone(iter, undefined); |
|
51 } |
|
52 } |
|
53 |
|
54 testNext(g); |
|
55 testSend(g); |
|
56 testThrow(g); |
|
57 |
|
58 testNext(function*() { return yield* g(); }); |
|
59 testSend(function*() { return yield* g(); }); |
|
60 testThrow(function*() { return yield* g(); }); |
|
61 |
|
62 if (g instanceof GeneratorFunction) { |
|
63 testNext(function() { return new g(); }); |
|
64 testSend(function() { return new g(); }); |
|
65 testThrow(function() { return new g(); }); |
|
66 } |
|
67 } |
|
68 |
|
69 TestGenerator(function* g1() { }, |
|
70 [undefined], |
|
71 "foo", |
|
72 [undefined]); |
|
73 |
|
74 TestGenerator(function* g2() { yield 1; }, |
|
75 [1, undefined], |
|
76 "foo", |
|
77 [1, undefined]); |
|
78 |
|
79 TestGenerator(function* g3() { yield 1; yield 2; }, |
|
80 [1, 2, undefined], |
|
81 "foo", |
|
82 [1, 2, undefined]); |
|
83 |
|
84 TestGenerator(function* g4() { yield 1; yield 2; return 3; }, |
|
85 [1, 2, 3], |
|
86 "foo", |
|
87 [1, 2, 3]); |
|
88 |
|
89 TestGenerator(function* g5() { return 1; }, |
|
90 [1], |
|
91 "foo", |
|
92 [1]); |
|
93 |
|
94 TestGenerator(function* g6() { var x = yield 1; return x; }, |
|
95 [1, undefined], |
|
96 "foo", |
|
97 [1, "foo"]); |
|
98 |
|
99 TestGenerator(function* g7() { var x = yield 1; yield 2; return x; }, |
|
100 [1, 2, undefined], |
|
101 "foo", |
|
102 [1, 2, "foo"]); |
|
103 |
|
104 TestGenerator(function* g8() { for (var x = 0; x < 4; x++) { yield x; } }, |
|
105 [0, 1, 2, 3, undefined], |
|
106 "foo", |
|
107 [0, 1, 2, 3, undefined]); |
|
108 |
|
109 // Generator with arguments. |
|
110 TestGenerator( |
|
111 function g9() { |
|
112 return (function*(a, b, c, d) { |
|
113 yield a; yield b; yield c; yield d; |
|
114 })("fee", "fi", "fo", "fum"); |
|
115 }, |
|
116 ["fee", "fi", "fo", "fum", undefined], |
|
117 "foo", |
|
118 ["fee", "fi", "fo", "fum", undefined]); |
|
119 |
|
120 // Too few arguments. |
|
121 TestGenerator( |
|
122 function g10() { |
|
123 return (function*(a, b, c, d) { |
|
124 yield a; yield b; yield c; yield d; |
|
125 })("fee", "fi"); |
|
126 }, |
|
127 ["fee", "fi", undefined, undefined, undefined], |
|
128 "foo", |
|
129 ["fee", "fi", undefined, undefined, undefined]); |
|
130 |
|
131 // Too many arguments. |
|
132 TestGenerator( |
|
133 function g11() { |
|
134 return (function*(a, b, c, d) { |
|
135 yield a; yield b; yield c; yield d; |
|
136 })("fee", "fi", "fo", "fum", "I smell the blood of an Englishman"); |
|
137 }, |
|
138 ["fee", "fi", "fo", "fum", undefined], |
|
139 "foo", |
|
140 ["fee", "fi", "fo", "fum", undefined]); |
|
141 |
|
142 // The arguments object. |
|
143 TestGenerator( |
|
144 function g12() { |
|
145 return (function*(a, b, c, d) { |
|
146 for (var i = 0; i < arguments.length; i++) { |
|
147 yield arguments[i]; |
|
148 } |
|
149 })("fee", "fi", "fo", "fum", "I smell the blood of an Englishman"); |
|
150 }, |
|
151 ["fee", "fi", "fo", "fum", "I smell the blood of an Englishman", |
|
152 undefined], |
|
153 "foo", |
|
154 ["fee", "fi", "fo", "fum", "I smell the blood of an Englishman", |
|
155 undefined]); |
|
156 |
|
157 // Access to captured free variables. |
|
158 TestGenerator( |
|
159 function g13() { |
|
160 return (function(a, b, c, d) { |
|
161 return (function*() { |
|
162 yield a; yield b; yield c; yield d; |
|
163 })(); |
|
164 })("fee", "fi", "fo", "fum"); |
|
165 }, |
|
166 ["fee", "fi", "fo", "fum", undefined], |
|
167 "foo", |
|
168 ["fee", "fi", "fo", "fum", undefined]); |
|
169 |
|
170 // Abusing the arguments object. |
|
171 TestGenerator( |
|
172 function g14() { |
|
173 return (function*(a, b, c, d) { |
|
174 arguments[0] = "Be he live"; |
|
175 arguments[1] = "or be he dead"; |
|
176 arguments[2] = "I'll grind his bones"; |
|
177 arguments[3] = "to make my bread"; |
|
178 yield a; yield b; yield c; yield d; |
|
179 })("fee", "fi", "fo", "fum"); |
|
180 }, |
|
181 ["Be he live", "or be he dead", "I'll grind his bones", "to make my bread", |
|
182 undefined], |
|
183 "foo", |
|
184 ["Be he live", "or be he dead", "I'll grind his bones", "to make my bread", |
|
185 undefined]); |
|
186 |
|
187 // Abusing the arguments object: strict mode. |
|
188 TestGenerator( |
|
189 function g15() { |
|
190 return (function*(a, b, c, d) { |
|
191 "use strict"; |
|
192 arguments[0] = "Be he live"; |
|
193 arguments[1] = "or be he dead"; |
|
194 arguments[2] = "I'll grind his bones"; |
|
195 arguments[3] = "to make my bread"; |
|
196 yield a; yield b; yield c; yield d; |
|
197 })("fee", "fi", "fo", "fum"); |
|
198 }, |
|
199 ["fee", "fi", "fo", "fum", undefined], |
|
200 "foo", |
|
201 ["fee", "fi", "fo", "fum", undefined]); |
|
202 |
|
203 // GC. |
|
204 if (typeof gc == 'function') { |
|
205 TestGenerator(function* g16() { yield "baz"; gc(); yield "qux"; }, |
|
206 ["baz", "qux", undefined], |
|
207 "foo", |
|
208 ["baz", "qux", undefined]); |
|
209 } |
|
210 |
|
211 // Receivers. |
|
212 TestGenerator( |
|
213 function g17() { |
|
214 function* g() { yield this.x; yield this.y; } |
|
215 var o = { start: g, x: 1, y: 2 }; |
|
216 return o.start(); |
|
217 }, |
|
218 [1, 2, undefined], |
|
219 "foo", |
|
220 [1, 2, undefined]); |
|
221 |
|
222 // FIXME: Capture the generator object as "this" in new g(). Bug 907742. |
|
223 // TestGenerator( |
|
224 // function g18() { |
|
225 // function* g() { yield this.x; yield this.y; } |
|
226 // var iter = new g; |
|
227 // iter.x = 1; |
|
228 // iter.y = 2; |
|
229 // return iter; |
|
230 // }, |
|
231 // [1, 2, undefined], |
|
232 // "foo", |
|
233 // [1, 2, undefined]); |
|
234 |
|
235 TestGenerator( |
|
236 function* g19() { |
|
237 var x = 1; |
|
238 yield x; |
|
239 with({x:2}) { yield x; } |
|
240 yield x; |
|
241 }, |
|
242 [1, 2, 1, undefined], |
|
243 "foo", |
|
244 [1, 2, 1, undefined]); |
|
245 |
|
246 TestGenerator( |
|
247 function* g20() { yield (1 + (yield 2) + 3); }, |
|
248 [2, NaN, undefined], |
|
249 "foo", |
|
250 [2, "1foo3", undefined]); |
|
251 |
|
252 TestGenerator( |
|
253 function* g21() { return (1 + (yield 2) + 3); }, |
|
254 [2, NaN], |
|
255 "foo", |
|
256 [2, "1foo3"]); |
|
257 |
|
258 TestGenerator( |
|
259 function* g22() { yield (1 + (yield 2) + 3); yield (4 + (yield 5) + 6); }, |
|
260 [2, NaN, 5, NaN, undefined], |
|
261 "foo", |
|
262 [2, "1foo3", 5, "4foo6", undefined]); |
|
263 |
|
264 TestGenerator( |
|
265 function* g23() { |
|
266 return (yield (1 + (yield 2) + 3)) + (yield (4 + (yield 5) + 6)); |
|
267 }, |
|
268 [2, NaN, 5, NaN, NaN], |
|
269 "foo", |
|
270 [2, "1foo3", 5, "4foo6", "foofoo"]); |
|
271 |
|
272 // Rewind a try context with and without operands on the stack. |
|
273 TestGenerator( |
|
274 function* g24() { |
|
275 try { |
|
276 return (yield (1 + (yield 2) + 3)) + (yield (4 + (yield 5) + 6)); |
|
277 } catch (e) { |
|
278 throw e; |
|
279 } |
|
280 }, |
|
281 [2, NaN, 5, NaN, NaN], |
|
282 "foo", |
|
283 [2, "1foo3", 5, "4foo6", "foofoo"]); |
|
284 |
|
285 // Yielding in a catch context, with and without operands on the stack. |
|
286 TestGenerator( |
|
287 function* g25() { |
|
288 try { |
|
289 throw (yield (1 + (yield 2) + 3)) |
|
290 } catch (e) { |
|
291 if (typeof e == 'object') throw e; |
|
292 return e + (yield (4 + (yield 5) + 6)); |
|
293 } |
|
294 }, |
|
295 [2, NaN, 5, NaN, NaN], |
|
296 "foo", |
|
297 [2, "1foo3", 5, "4foo6", "foofoo"]); |
|
298 |
|
299 // Generator function instances. |
|
300 TestGenerator(GeneratorFunction(), |
|
301 [undefined], |
|
302 "foo", |
|
303 [undefined]); |
|
304 |
|
305 TestGenerator(new GeneratorFunction(), |
|
306 [undefined], |
|
307 "foo", |
|
308 [undefined]); |
|
309 |
|
310 TestGenerator(GeneratorFunction('yield 1;'), |
|
311 [1, undefined], |
|
312 "foo", |
|
313 [1, undefined]); |
|
314 |
|
315 TestGenerator( |
|
316 function() { return GeneratorFunction('x', 'y', 'yield x + y;')(1, 2) }, |
|
317 [3, undefined], |
|
318 "foo", |
|
319 [3, undefined]); |
|
320 |
|
321 // Access to this with formal arguments. |
|
322 TestGenerator( |
|
323 function () { |
|
324 return ({ x: 42, g: function* (a) { yield this.x } }).g(0); |
|
325 }, |
|
326 [42, undefined], |
|
327 "foo", |
|
328 [42, undefined]); |
|
329 |
|
330 function TestTryCatch(instantiate) { |
|
331 function* g() { yield 1; try { yield 2; } catch (e) { yield e; } yield 3; } |
|
332 function Sentinel() {} |
|
333 |
|
334 function Test1(iter) { |
|
335 assertIteratorNext(iter, 1); |
|
336 assertIteratorNext(iter, 2); |
|
337 assertIteratorNext(iter, 3); |
|
338 assertIteratorDone(iter, undefined); |
|
339 assertIteratorDone(iter, undefined); |
|
340 } |
|
341 Test1(instantiate(g)); |
|
342 |
|
343 function Test2(iter) { |
|
344 assertThrowsInstanceOf(function() { iter.throw(new Sentinel); }, Sentinel); |
|
345 assertIteratorDone(iter, undefined); |
|
346 } |
|
347 Test2(instantiate(g)); |
|
348 |
|
349 function Test3(iter) { |
|
350 assertIteratorNext(iter, 1); |
|
351 assertThrowsInstanceOf(function() { iter.throw(new Sentinel); }, Sentinel); |
|
352 assertIteratorDone(iter, undefined); |
|
353 } |
|
354 Test3(instantiate(g)); |
|
355 |
|
356 function Test4(iter) { |
|
357 assertIteratorNext(iter, 1); |
|
358 assertIteratorNext(iter, 2); |
|
359 var exn = new Sentinel; |
|
360 assertIteratorResult(iter.throw(exn), exn, false); |
|
361 assertIteratorNext(iter, 3); |
|
362 assertIteratorDone(iter, undefined); |
|
363 assertIteratorDone(iter, undefined); |
|
364 } |
|
365 Test4(instantiate(g)); |
|
366 |
|
367 function Test5(iter) { |
|
368 assertIteratorNext(iter, 1); |
|
369 assertIteratorNext(iter, 2); |
|
370 var exn = new Sentinel; |
|
371 assertIteratorResult(iter.throw(exn), exn, false); |
|
372 assertIteratorNext(iter, 3); |
|
373 assertThrowsInstanceOf(function() { iter.throw(new Sentinel); }, Sentinel); |
|
374 assertIteratorDone(iter, undefined); |
|
375 |
|
376 } |
|
377 Test5(instantiate(g)); |
|
378 |
|
379 function Test6(iter) { |
|
380 assertIteratorNext(iter, 1); |
|
381 assertIteratorNext(iter, 2); |
|
382 var exn = new Sentinel; |
|
383 assertIteratorResult(iter.throw(exn), exn, false); |
|
384 assertThrowsInstanceOf(function() { iter.throw(new Sentinel); }, Sentinel); |
|
385 assertIteratorDone(iter, undefined); |
|
386 } |
|
387 Test6(instantiate(g)); |
|
388 } |
|
389 TestTryCatch(function (g) { return g(); }); |
|
390 TestTryCatch(function* (g) { return yield* g(); }); |
|
391 |
|
392 function TestTryFinally(instantiate) { |
|
393 function* g() { yield 1; try { yield 2; } finally { yield 3; } yield 4; } |
|
394 function Sentinel() {} |
|
395 function Sentinel2() {} |
|
396 |
|
397 function Test1(iter) { |
|
398 assertIteratorNext(iter, 1); |
|
399 assertIteratorNext(iter, 2); |
|
400 assertIteratorNext(iter, 3); |
|
401 assertIteratorNext(iter, 4); |
|
402 assertIteratorDone(iter, undefined); |
|
403 assertIteratorDone(iter, undefined); |
|
404 } |
|
405 Test1(instantiate(g)); |
|
406 |
|
407 function Test2(iter) { |
|
408 assertThrowsInstanceOf(function() { iter.throw(new Sentinel); }, Sentinel); |
|
409 assertIteratorDone(iter, undefined); |
|
410 } |
|
411 Test2(instantiate(g)); |
|
412 |
|
413 function Test3(iter) { |
|
414 assertIteratorNext(iter, 1); |
|
415 assertThrowsInstanceOf(function() { iter.throw(new Sentinel); }, Sentinel); |
|
416 assertIteratorDone(iter, undefined); |
|
417 } |
|
418 Test3(instantiate(g)); |
|
419 |
|
420 function Test4(iter) { |
|
421 assertIteratorNext(iter, 1); |
|
422 assertIteratorNext(iter, 2); |
|
423 assertIteratorResult(iter.throw(new Sentinel), 3, false); |
|
424 assertThrowsInstanceOf(function() { iter.next(); }, Sentinel); |
|
425 assertIteratorDone(iter, undefined); |
|
426 |
|
427 } |
|
428 Test4(instantiate(g)); |
|
429 |
|
430 function Test5(iter) { |
|
431 assertIteratorNext(iter, 1); |
|
432 assertIteratorNext(iter, 2); |
|
433 assertIteratorResult(iter.throw(new Sentinel), 3, false); |
|
434 assertThrowsInstanceOf(function() { iter.throw(new Sentinel2); }, Sentinel2); |
|
435 assertIteratorDone(iter, undefined); |
|
436 } |
|
437 Test5(instantiate(g)); |
|
438 |
|
439 function Test6(iter) { |
|
440 assertIteratorNext(iter, 1); |
|
441 assertIteratorNext(iter, 2); |
|
442 assertIteratorNext(iter, 3); |
|
443 assertThrowsInstanceOf(function() { iter.throw(new Sentinel); }, Sentinel); |
|
444 assertIteratorDone(iter, undefined); |
|
445 } |
|
446 Test6(instantiate(g)); |
|
447 |
|
448 function Test7(iter) { |
|
449 assertIteratorNext(iter, 1); |
|
450 assertIteratorNext(iter, 2); |
|
451 assertIteratorNext(iter, 3); |
|
452 assertIteratorNext(iter, 4); |
|
453 assertThrowsInstanceOf(function() { iter.throw(new Sentinel); }, Sentinel); |
|
454 assertIteratorDone(iter, undefined); |
|
455 } |
|
456 Test7(instantiate(g)); |
|
457 } |
|
458 TestTryFinally(function (g) { return g(); }); |
|
459 TestTryFinally(function* (g) { return yield* g(); }); |
|
460 |
|
461 function TestNestedTry(instantiate) { |
|
462 function* g() { |
|
463 try { |
|
464 yield 1; |
|
465 try { yield 2; } catch (e) { yield e; } |
|
466 yield 3; |
|
467 } finally { |
|
468 yield 4; |
|
469 } |
|
470 yield 5; |
|
471 } |
|
472 function Sentinel() {} |
|
473 function Sentinel2() {} |
|
474 |
|
475 function Test1(iter) { |
|
476 assertIteratorNext(iter, 1); |
|
477 assertIteratorNext(iter, 2); |
|
478 assertIteratorNext(iter, 3); |
|
479 assertIteratorNext(iter, 4); |
|
480 assertIteratorNext(iter, 5); |
|
481 assertIteratorDone(iter, undefined); |
|
482 assertIteratorDone(iter, undefined); |
|
483 } |
|
484 Test1(instantiate(g)); |
|
485 |
|
486 function Test2(iter) { |
|
487 assertThrowsInstanceOf(function() { iter.throw(new Sentinel); }, Sentinel); |
|
488 assertIteratorDone(iter, undefined); |
|
489 } |
|
490 Test2(instantiate(g)); |
|
491 |
|
492 function Test3(iter) { |
|
493 assertIteratorNext(iter, 1); |
|
494 assertIteratorResult(iter.throw(new Sentinel), 4, false); |
|
495 assertThrowsInstanceOf(function() { iter.next(); }, Sentinel); |
|
496 assertIteratorDone(iter, undefined); |
|
497 } |
|
498 Test3(instantiate(g)); |
|
499 |
|
500 function Test4(iter) { |
|
501 assertIteratorNext(iter, 1); |
|
502 assertIteratorResult(iter.throw(new Sentinel), 4, false); |
|
503 assertThrowsInstanceOf(function() { iter.throw(new Sentinel2); }, Sentinel2); |
|
504 assertIteratorDone(iter, undefined); |
|
505 } |
|
506 Test4(instantiate(g)); |
|
507 |
|
508 function Test5(iter) { |
|
509 assertIteratorNext(iter, 1); |
|
510 assertIteratorNext(iter, 2); |
|
511 var exn = new Sentinel; |
|
512 assertIteratorResult(iter.throw(exn), exn, false); |
|
513 assertIteratorNext(iter, 3); |
|
514 assertIteratorNext(iter, 4); |
|
515 assertIteratorNext(iter, 5); |
|
516 assertIteratorDone(iter, undefined); |
|
517 assertIteratorDone(iter, undefined); |
|
518 |
|
519 } |
|
520 Test5(instantiate(g)); |
|
521 |
|
522 function Test6(iter) { |
|
523 assertIteratorNext(iter, 1); |
|
524 assertIteratorNext(iter, 2); |
|
525 var exn = new Sentinel; |
|
526 assertIteratorResult(iter.throw(exn), exn, false); |
|
527 assertIteratorResult(iter.throw(new Sentinel2), 4, false); |
|
528 assertThrowsInstanceOf(function() { iter.next(); }, Sentinel2); |
|
529 assertIteratorDone(iter, undefined); |
|
530 } |
|
531 Test6(instantiate(g)); |
|
532 |
|
533 function Test7(iter) { |
|
534 assertIteratorNext(iter, 1); |
|
535 assertIteratorNext(iter, 2); |
|
536 var exn = new Sentinel; |
|
537 assertIteratorResult(iter.throw(exn), exn, false); |
|
538 assertIteratorNext(iter, 3); |
|
539 assertIteratorResult(iter.throw(new Sentinel2), 4, false); |
|
540 assertThrowsInstanceOf(function() { iter.next(); }, Sentinel2); |
|
541 assertIteratorDone(iter, undefined); |
|
542 |
|
543 } |
|
544 Test7(instantiate(g)); |
|
545 |
|
546 // That's probably enough. |
|
547 } |
|
548 TestNestedTry(function (g) { return g(); }); |
|
549 TestNestedTry(function* (g) { return yield* g(); }); |
|
550 |
|
551 function TestRecursion() { |
|
552 function TestNextRecursion() { |
|
553 function* g() { yield iter.next(); } |
|
554 var iter = g(); |
|
555 return iter.next(); |
|
556 } |
|
557 function TestSendRecursion() { |
|
558 function* g() { yield iter.next(42); } |
|
559 var iter = g(); |
|
560 return iter.next(); |
|
561 } |
|
562 function TestThrowRecursion() { |
|
563 function* g() { yield iter.throw(1); } |
|
564 var iter = g(); |
|
565 return iter.next(); |
|
566 } |
|
567 assertThrowsInstanceOf(TestNextRecursion, TypeError); |
|
568 assertThrowsInstanceOf(TestSendRecursion, TypeError); |
|
569 assertThrowsInstanceOf(TestThrowRecursion, TypeError); |
|
570 } |
|
571 TestRecursion(); |
|
572 |
|
573 if (typeof reportCompare == "function") |
|
574 reportCompare(true, true); |