regenerator
Advanced tools
Comparing version 0.3.6 to 0.3.7
@@ -19,3 +19,3 @@ { | ||
], | ||
"version": "0.3.6", | ||
"version": "0.3.7", | ||
"homepage": "http://github.com/facebook/regenerator", | ||
@@ -22,0 +22,0 @@ "repository": { |
@@ -43,6 +43,2 @@ /** | ||
// It's too hard to know if a delegate generator is newborn or not, so | ||
// we don't throw a TypeError when a value is accidentally sent. | ||
var permitNewbornSend = false; | ||
wrapGenerator.mark = function(genFun) { | ||
@@ -80,9 +76,5 @@ genFun.constructor = GeneratorFunction; | ||
if (delegate) { | ||
var pns = permitNewbornSend; | ||
permitNewbornSend = true; | ||
try { | ||
var info = delegate.generator[method](arg); | ||
permitNewbornSend = pns; | ||
} catch (uncaught) { | ||
permitNewbornSend = pns; | ||
context.delegate = null; | ||
@@ -110,4 +102,3 @@ | ||
if (state === GenStateSuspendedStart && | ||
typeof arg !== "undefined" && | ||
!permitNewbornSend) { | ||
typeof arg !== "undefined") { | ||
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresume | ||
@@ -145,4 +136,14 @@ throw new TypeError( | ||
var info = { value: value, done: context.done }; | ||
if (info.value !== ContinueSentinel) { | ||
var info = { | ||
value: value, | ||
done: context.done | ||
}; | ||
if (value === ContinueSentinel) { | ||
if (context.delegate && method === "next") { | ||
// Deliberately forget the last sent value so that we don't | ||
// accidentally pass it on to the delegate. | ||
arg = void 0; | ||
} | ||
} else { | ||
return info; | ||
@@ -149,0 +150,0 @@ } |
@@ -10,2 +10,2 @@ /** | ||
*/ | ||
(function(t,e){function n(t,e){return new r(t,e||null)}function r(t,e){function n(n,r){if(f===h)throw new Error("Generator is already running");if(f===s)throw new Error("Generator has already finished");for(;;){var o=i.delegate;if(o){var p=u;u=!0;try{var d=o.generator[n](r);u=p}catch(y){u=p,i.delegate=null,n="throw",r=y;continue}if(!d.done)return f=c,d;i[o.resultName]=d.value,i.next=o.nextLoc,i.delegate=null}if("next"===n){if(f===a&&"undefined"!=typeof r&&!u)throw new TypeError("attempt to send "+JSON.stringify(r)+" to newborn generator");f===c?i.sent=r:delete i.sent}else if("throw"===n){if(f===a)throw f=s,r;i.dispatchException(r)}f=h;try{var w=t.call(e,i);f=i.done?s:c;var d={value:w,done:i.done};if(d.value!==l)return d}catch(v){"next"===n?i.dispatchException(v):r=v}}}var r=this,i=new o,f=a;r.next=n.bind(r,"next"),r.throw=n.bind(r,"throw")}function o(){this.reset()}var i=Object.prototype.hasOwnProperty;if(!t.wrapGenerator){t.wrapGenerator=n,"undefined"!=typeof exports&&(exports.wrapGenerator=n);var a="suspendedStart",c="suspendedYield",h="executing",s="completed",l={},u=!1;n.mark=function(t){return t.constructor=e,t},"GeneratorFunction"!==e.name&&(e.name="GeneratorFunction"),n.isGeneratorFunction=function(t){var n=t&&t.constructor;return n?e.name===n.name:!1},r.prototype.toString=function(){return"[object Generator]"},o.prototype={constructor:o,reset:function(){this.next=0,this.sent=void 0,this.tryStack=[],this.done=!1,this.delegate=null;for(var t,e=0;i.call(this,t="t"+e)||20>e;++e)this[t]=null},stop:function(){if(this.done=!0,i.call(this,"thrown")){var t=this.thrown;throw delete this.thrown,t}return this.rval},keys:function(t){return Object.keys(t).reverse()},pushTry:function(t,e,n){e&&this.tryStack.push({finallyLoc:e,finallyTempVar:n}),t&&this.tryStack.push({catchLoc:t})},popCatch:function(t){var e=this.tryStack.length-1,n=this.tryStack[e];n&&n.catchLoc===t&&(this.tryStack.length=e)},popFinally:function(t){var e=this.tryStack.length-1,n=this.tryStack[e];n&&i.call(n,"finallyLoc")||(n=this.tryStack[--e]),n&&n.finallyLoc===t&&(this.tryStack.length=e)},dispatchException:function(t){var e=[],n=!1;if(this.done)throw t;this.thrown=t,this.next="end";for(var r=this.tryStack.length-1;r>=0;--r){var o=this.tryStack[r];if(o.catchLoc){this.next=o.catchLoc,n=!0;break}o.finallyLoc&&(e.push(o),n=!0)}for(;o=e.pop();)this[o.finallyTempVar]=this.next,this.next=o.finallyLoc},delegateYield:function(t,e,n){return this.delegate={generator:t,resultName:e,nextLoc:n},l}}}}).apply(this,Function("return [this, function GeneratorFunction(){}]")()); | ||
(function(t,e){function n(t,e){return new r(t,e||null)}function r(t,e){function n(n,r){if(u===h)throw new Error("Generator is already running");if(u===s)throw new Error("Generator has already finished");for(;;){var o=i.delegate;if(o){try{var f=o.generator[n](r)}catch(p){i.delegate=null,n="throw",r=p;continue}if(!f.done)return u=c,f;i[o.resultName]=f.value,i.next=o.nextLoc,i.delegate=null}if("next"===n){if(u===a&&"undefined"!=typeof r)throw new TypeError("attempt to send "+JSON.stringify(r)+" to newborn generator");u===c?i.sent=r:delete i.sent}else if("throw"===n){if(u===a)throw u=s,r;i.dispatchException(r)}u=h;try{var d=t.call(e,i);u=i.done?s:c;var f={value:d,done:i.done};if(d!==l)return f;i.delegate&&"next"===n&&(r=void 0)}catch(y){"next"===n?i.dispatchException(y):r=y}}}var r=this,i=new o,u=a;r.next=n.bind(r,"next"),r.throw=n.bind(r,"throw")}function o(){this.reset()}var i=Object.prototype.hasOwnProperty;if(!t.wrapGenerator){t.wrapGenerator=n,"undefined"!=typeof exports&&(exports.wrapGenerator=n);var a="suspendedStart",c="suspendedYield",h="executing",s="completed",l={};n.mark=function(t){return t.constructor=e,t},"GeneratorFunction"!==e.name&&(e.name="GeneratorFunction"),n.isGeneratorFunction=function(t){var n=t&&t.constructor;return n?e.name===n.name:!1},r.prototype.toString=function(){return"[object Generator]"},o.prototype={constructor:o,reset:function(){this.next=0,this.sent=void 0,this.tryStack=[],this.done=!1,this.delegate=null;for(var t,e=0;i.call(this,t="t"+e)||20>e;++e)this[t]=null},stop:function(){if(this.done=!0,i.call(this,"thrown")){var t=this.thrown;throw delete this.thrown,t}return this.rval},keys:function(t){return Object.keys(t).reverse()},pushTry:function(t,e,n){e&&this.tryStack.push({finallyLoc:e,finallyTempVar:n}),t&&this.tryStack.push({catchLoc:t})},popCatch:function(t){var e=this.tryStack.length-1,n=this.tryStack[e];n&&n.catchLoc===t&&(this.tryStack.length=e)},popFinally:function(t){var e=this.tryStack.length-1,n=this.tryStack[e];n&&i.call(n,"finallyLoc")||(n=this.tryStack[--e]),n&&n.finallyLoc===t&&(this.tryStack.length=e)},dispatchException:function(t){var e=[],n=!1;if(this.done)throw t;this.thrown=t,this.next="end";for(var r=this.tryStack.length-1;r>=0;--r){var o=this.tryStack[r];if(o.catchLoc){this.next=o.catchLoc,n=!0;break}o.finallyLoc&&(e.push(o),n=!0)}for(;o=e.pop();)this[o.finallyTempVar]=this.next,this.next=o.finallyLoc},delegateYield:function(t,e,n){return this.delegate={generator:t,resultName:e,nextLoc:n},l}}}}).apply(this,Function("return [this, function GeneratorFunction(){}]")()); |
@@ -74,2 +74,3 @@ /** | ||
"--reporter", "spec", | ||
"--require", "runtime/dev", | ||
"./test/tests.es6.js" | ||
@@ -103,2 +104,3 @@ ]); | ||
"--reporter", "spec", | ||
"--require", "runtime/dev", | ||
"./test/tests.es5.js" | ||
@@ -105,0 +107,0 @@ ]); |
@@ -12,3 +12,2 @@ /** | ||
var assert = require("assert"); | ||
require("../runtime/dev"); | ||
@@ -1153,3 +1152,3 @@ function check(g, yields, returnValue) { | ||
// I would really like to be able to pass 3 to gen.next here, but V8 | ||
// I would really like to be able to pass 3 to g.next here, but V8 | ||
// ignores values sent to newborn generators, and SpiderMonkey throws | ||
@@ -1167,2 +1166,106 @@ // a TypeError. | ||
}); | ||
it("should support the ignore-initial-yield wrapper idiom", function() { | ||
var markers = []; | ||
function *inner() { | ||
markers.push(0); | ||
var sent1 = yield 1; | ||
markers.push(2); | ||
var sent2 = yield 2; | ||
markers.push(3); | ||
return [sent1, sent2]; | ||
} | ||
function wrapper(delegate) { | ||
var gen = (function*() { | ||
// This yield is the "initial yield" whose argument we ignore. | ||
var sent = yield "ignored", info; | ||
markers.push(1); | ||
while (!(info = delegate.next(sent)).done) { | ||
sent = yield info.value; | ||
} | ||
markers.push(4); | ||
return info.value; | ||
})(); | ||
// Ensure that gen is not newborn and that the next invocation of | ||
// gen.next(value) can send value to the initial yield expression. | ||
gen.next(); | ||
return gen; | ||
} | ||
var n = inner(); | ||
assert.deepEqual(n.next(), { | ||
value: 1, | ||
done: false | ||
}); | ||
var g = wrapper(n); | ||
// Unlike in the previous spec, it's fine to pass 3 to g.next here, | ||
// because g is not newborn, because g.next was already called once | ||
// before g was returned from the wrapper function. | ||
assert.deepEqual(g.next(3), { | ||
value: 2, | ||
done: false | ||
}); | ||
assert.deepEqual(g.next(4), { | ||
value: [3, 4], | ||
done: true | ||
}); | ||
// Ensure we encountered the marker points in the expected order. | ||
assert.deepEqual(markers, [0, 1, 2, 3, 4]); | ||
}); | ||
it("should allow chaining newborn and non-newborn generators", function() { | ||
function *range(n) { | ||
for (var i = 0; i < n; ++i) { | ||
yield i; | ||
} | ||
} | ||
function *chain(a, b) { | ||
yield* a; | ||
yield* b; | ||
} | ||
check(chain(range(3), range(5)), [0, 1, 2, 0, 1, 2, 3, 4]); | ||
function *y3(x) { | ||
return yield yield yield x; | ||
} | ||
function *y5(x) { | ||
return yield yield yield yield yield x; | ||
} | ||
check( | ||
chain(y3("foo"), y5("bar")), | ||
["foo", 1, 2, "bar", 4, 5, 6, 7] | ||
); | ||
var g3 = y3("three"); | ||
assert.deepEqual(g3.next(), { | ||
value: "three", | ||
done: false | ||
}); | ||
var g5 = y5("five"); | ||
assert.deepEqual(g5.next(), { | ||
value: "five", | ||
done: false | ||
}); | ||
var undef; // A little easier to read than void 0. | ||
check(chain(g3, g5), [undef, 1, undef, 3, 4, 5]); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
100263
2906