OLD | NEW |
---|---|
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Flags: --harmony-generators --expose-gc | 28 // Flags: --harmony-generators --expose-gc --harmony-symbols |
rossberg
2014/08/05 11:58:02
Note: --harmony-symbols should be unnecessary now.
| |
29 // Flags: --harmony-iteration | |
29 | 30 |
30 // Test generator iteration. | 31 // Test generator iteration. |
31 | 32 |
32 var GeneratorFunction = (function*(){yield 1;}).__proto__.constructor; | 33 var GeneratorFunction = (function*(){yield 1;}).__proto__.constructor; |
33 | 34 |
34 function assertIteratorResult(value, done, result) { | 35 function assertIteratorResult(value, done, result) { |
35 assertEquals({ value: value, done: done}, result); | 36 assertEquals({ value: value, done: done}, result); |
36 } | 37 } |
37 | 38 |
38 function assertIteratorIsClosed(iter) { | 39 function assertIteratorIsClosed(iter) { |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
348 TestGenerator( | 349 TestGenerator( |
349 function* g27() { | 350 function* g27() { |
350 yield | 351 yield |
351 3 | 352 3 |
352 return | 353 return |
353 }, | 354 }, |
354 [undefined, undefined], | 355 [undefined, undefined], |
355 "foo", | 356 "foo", |
356 [undefined, undefined]); | 357 [undefined, undefined]); |
357 | 358 |
359 // TODO(wingo): We should use TestGenerator for these, except that | |
360 // currently yield* will unconditionally propagate a throw() to the | |
361 // delegate iterator, which fails for these iterators that don't have | |
362 // throw(). See http://code.google.com/p/v8/issues/detail?id=3484. | |
363 (function() { | |
364 function* g28() { | |
365 yield* [1, 2, 3]; | |
366 } | |
367 var iter = g28(); | |
368 assertIteratorResult(1, false, iter.next()); | |
369 assertIteratorResult(2, false, iter.next()); | |
370 assertIteratorResult(3, false, iter.next()); | |
371 assertIteratorResult(undefined, true, iter.next()); | |
372 })(); | |
373 | |
374 (function() { | |
375 function* g29() { | |
376 yield* "abc"; | |
377 } | |
378 var iter = g29(); | |
379 assertIteratorResult("a", false, iter.next()); | |
380 assertIteratorResult("b", false, iter.next()); | |
381 assertIteratorResult("c", false, iter.next()); | |
382 assertIteratorResult(undefined, true, iter.next()); | |
383 })(); | |
384 | |
358 // Generator function instances. | 385 // Generator function instances. |
359 TestGenerator(GeneratorFunction(), | 386 TestGenerator(GeneratorFunction(), |
360 [undefined], | 387 [undefined], |
361 "foo", | 388 "foo", |
362 [undefined]); | 389 [undefined]); |
363 | 390 |
364 TestGenerator(new GeneratorFunction(), | 391 TestGenerator(new GeneratorFunction(), |
365 [undefined], | 392 [undefined], |
366 "foo", | 393 "foo", |
367 [undefined]); | 394 [undefined]); |
(...skipping 18 matching lines...) Expand all Loading... | |
386 "foo", | 413 "foo", |
387 [42, undefined]); | 414 [42, undefined]); |
388 | 415 |
389 // Test that yield* re-yields received results without re-boxing. | 416 // Test that yield* re-yields received results without re-boxing. |
390 function TestDelegatingYield() { | 417 function TestDelegatingYield() { |
391 function results(results) { | 418 function results(results) { |
392 var i = 0; | 419 var i = 0; |
393 function next() { | 420 function next() { |
394 return results[i++]; | 421 return results[i++]; |
395 } | 422 } |
396 return { next: next } | 423 var iter = { next: next }; |
424 var ret = {}; | |
425 ret[Symbol.iterator] = function() { return iter; }; | |
426 return ret; | |
397 } | 427 } |
398 function* yield_results(expected) { | 428 function* yield_results(expected) { |
399 return yield* results(expected); | 429 return yield* results(expected); |
400 } | 430 } |
401 function collect_results(iter) { | 431 function collect_results(iterable) { |
432 var iter = iterable[Symbol.iterator](); | |
402 var ret = []; | 433 var ret = []; |
403 var result; | 434 var result; |
404 do { | 435 do { |
405 result = iter.next(); | 436 result = iter.next(); |
406 ret.push(result); | 437 ret.push(result); |
407 } while (!result.done); | 438 } while (!result.done); |
408 return ret; | 439 return ret; |
409 } | 440 } |
410 // We have to put a full result for the end, because the return will re-box. | 441 // We have to put a full result for the end, because the return will re-box. |
411 var expected = [{value: 1}, 13, "foo", {value: 34, done: true}]; | 442 var expected = [{value: 1}, 13, "foo", {value: 34, done: true}]; |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
659 function TestThrowRecursion() { | 690 function TestThrowRecursion() { |
660 function* g() { yield iter.throw(1); } | 691 function* g() { yield iter.throw(1); } |
661 var iter = g(); | 692 var iter = g(); |
662 return iter.next(); | 693 return iter.next(); |
663 } | 694 } |
664 assertThrows(TestNextRecursion, Error); | 695 assertThrows(TestNextRecursion, Error); |
665 assertThrows(TestSendRecursion, Error); | 696 assertThrows(TestSendRecursion, Error); |
666 assertThrows(TestThrowRecursion, Error); | 697 assertThrows(TestThrowRecursion, Error); |
667 } | 698 } |
668 TestRecursion(); | 699 TestRecursion(); |
OLD | NEW |