1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_6/Generators/delegating-yield-2.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,71 @@ 1.4 +// Test yield* with iter.throw and monkeypatching. 1.5 + 1.6 +function* g1() { return (yield 1); } 1.7 +function* g2() { try { yield 1; } catch (e) { yield e; } } 1.8 +function* delegate(iter) { return yield* iter; } 1.9 +var GeneratorObjectPrototype = Object.getPrototypeOf(g1).prototype; 1.10 +var GeneratorObjectPrototype_throw = GeneratorObjectPrototype.throw; 1.11 + 1.12 +// An uncaught delegated throw. 1.13 +var inner = g1(); 1.14 +var outer = delegate(inner); 1.15 +assertIteratorNext(outer, 1); 1.16 +assertThrowsValue(function () { outer.throw(42) }, 42); 1.17 +assertThrowsValue(function () { outer.throw(42) }, 42); 1.18 + 1.19 +// A caught delegated throw. 1.20 +inner = g2(); 1.21 +outer = delegate(inner); 1.22 +assertIteratorNext(outer, 1); 1.23 +assertIteratorResult(outer.throw(42), 42, false); 1.24 +assertThrowsValue(function () { outer.throw(42) }, 42); 1.25 +assertThrowsValue(function () { outer.throw(42) }, 42); 1.26 + 1.27 +// What would be an uncaught delegated throw, but with a monkeypatched iterator. 1.28 +inner = g1(); 1.29 +outer = delegate(inner); 1.30 +assertIteratorNext(outer, 1); 1.31 +inner.throw = function(e) { return e*2; }; 1.32 +assertEq(84, outer.throw(42)); 1.33 +assertIteratorDone(outer, undefined); 1.34 + 1.35 +// Monkeypatching inner.next. 1.36 +inner = g1(); 1.37 +outer = delegate(inner); 1.38 +inner.next = function() { return { value: 13, done: true } }; 1.39 +assertIteratorDone(outer, 13); 1.40 + 1.41 +// What would be a caught delegated throw, but with a monkeypunched prototype. 1.42 +inner = g2(); 1.43 +outer = delegate(inner); 1.44 +assertIteratorNext(outer, 1); 1.45 +delete GeneratorObjectPrototype.throw; 1.46 +var outer_throw_42 = GeneratorObjectPrototype_throw.bind(outer, 42); 1.47 +assertThrowsValue(outer_throw_42, 42); 1.48 +assertThrowsValue(outer_throw_42, 42); 1.49 + 1.50 +// Monkeypunch a different throw handler. 1.51 +inner = g2(); 1.52 +outer = delegate(inner); 1.53 +outer_throw_42 = GeneratorObjectPrototype_throw.bind(outer, 42); 1.54 +assertIteratorNext(outer, 1); 1.55 +GeneratorObjectPrototype.throw = function(e) { return e*2; } 1.56 +assertEq(84, outer_throw_42()); 1.57 +assertEq(84, outer_throw_42()); 1.58 +// This continues indefinitely. 1.59 +assertEq(84, outer_throw_42()); 1.60 +assertIteratorDone(outer, undefined); 1.61 + 1.62 +// The same, but restoring the original pre-monkey throw. 1.63 +inner = g2(); 1.64 +outer = delegate(inner); 1.65 +outer_throw_42 = GeneratorObjectPrototype_throw.bind(outer, 42); 1.66 +assertIteratorNext(outer, 1); 1.67 +assertEq(84, outer_throw_42()); 1.68 +assertEq(84, outer_throw_42()); 1.69 +GeneratorObjectPrototype.throw = GeneratorObjectPrototype_throw; 1.70 +assertIteratorResult(outer_throw_42(), 42, false); 1.71 +assertIteratorDone(outer, undefined); 1.72 + 1.73 +if (typeof reportCompare == "function") 1.74 + reportCompare(true, true);