js/src/tests/ecma_6/Generators/delegating-yield-2.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Test yield* with iter.throw and monkeypatching.
michael@0 2
michael@0 3 function* g1() { return (yield 1); }
michael@0 4 function* g2() { try { yield 1; } catch (e) { yield e; } }
michael@0 5 function* delegate(iter) { return yield* iter; }
michael@0 6 var GeneratorObjectPrototype = Object.getPrototypeOf(g1).prototype;
michael@0 7 var GeneratorObjectPrototype_throw = GeneratorObjectPrototype.throw;
michael@0 8
michael@0 9 // An uncaught delegated throw.
michael@0 10 var inner = g1();
michael@0 11 var outer = delegate(inner);
michael@0 12 assertIteratorNext(outer, 1);
michael@0 13 assertThrowsValue(function () { outer.throw(42) }, 42);
michael@0 14 assertThrowsValue(function () { outer.throw(42) }, 42);
michael@0 15
michael@0 16 // A caught delegated throw.
michael@0 17 inner = g2();
michael@0 18 outer = delegate(inner);
michael@0 19 assertIteratorNext(outer, 1);
michael@0 20 assertIteratorResult(outer.throw(42), 42, false);
michael@0 21 assertThrowsValue(function () { outer.throw(42) }, 42);
michael@0 22 assertThrowsValue(function () { outer.throw(42) }, 42);
michael@0 23
michael@0 24 // What would be an uncaught delegated throw, but with a monkeypatched iterator.
michael@0 25 inner = g1();
michael@0 26 outer = delegate(inner);
michael@0 27 assertIteratorNext(outer, 1);
michael@0 28 inner.throw = function(e) { return e*2; };
michael@0 29 assertEq(84, outer.throw(42));
michael@0 30 assertIteratorDone(outer, undefined);
michael@0 31
michael@0 32 // Monkeypatching inner.next.
michael@0 33 inner = g1();
michael@0 34 outer = delegate(inner);
michael@0 35 inner.next = function() { return { value: 13, done: true } };
michael@0 36 assertIteratorDone(outer, 13);
michael@0 37
michael@0 38 // What would be a caught delegated throw, but with a monkeypunched prototype.
michael@0 39 inner = g2();
michael@0 40 outer = delegate(inner);
michael@0 41 assertIteratorNext(outer, 1);
michael@0 42 delete GeneratorObjectPrototype.throw;
michael@0 43 var outer_throw_42 = GeneratorObjectPrototype_throw.bind(outer, 42);
michael@0 44 assertThrowsValue(outer_throw_42, 42);
michael@0 45 assertThrowsValue(outer_throw_42, 42);
michael@0 46
michael@0 47 // Monkeypunch a different throw handler.
michael@0 48 inner = g2();
michael@0 49 outer = delegate(inner);
michael@0 50 outer_throw_42 = GeneratorObjectPrototype_throw.bind(outer, 42);
michael@0 51 assertIteratorNext(outer, 1);
michael@0 52 GeneratorObjectPrototype.throw = function(e) { return e*2; }
michael@0 53 assertEq(84, outer_throw_42());
michael@0 54 assertEq(84, outer_throw_42());
michael@0 55 // This continues indefinitely.
michael@0 56 assertEq(84, outer_throw_42());
michael@0 57 assertIteratorDone(outer, undefined);
michael@0 58
michael@0 59 // The same, but restoring the original pre-monkey throw.
michael@0 60 inner = g2();
michael@0 61 outer = delegate(inner);
michael@0 62 outer_throw_42 = GeneratorObjectPrototype_throw.bind(outer, 42);
michael@0 63 assertIteratorNext(outer, 1);
michael@0 64 assertEq(84, outer_throw_42());
michael@0 65 assertEq(84, outer_throw_42());
michael@0 66 GeneratorObjectPrototype.throw = GeneratorObjectPrototype_throw;
michael@0 67 assertIteratorResult(outer_throw_42(), 42, false);
michael@0 68 assertIteratorDone(outer, undefined);
michael@0 69
michael@0 70 if (typeof reportCompare == "function")
michael@0 71 reportCompare(true, true);

mercurial