Skip to content

Commit

Permalink
Merge pull request kriskowal#366 from kriskowal/no-spider
Browse files Browse the repository at this point in the history
Withdraw support of SpiderMonkey generators
  • Loading branch information
kriskowal committed Oct 20, 2013
2 parents 87c8607 + 9b48dd1 commit ae90d83
Showing 1 changed file with 8 additions and 91 deletions.
99 changes: 8 additions & 91 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,49 +262,10 @@ var object_keys = Object.keys || function (object) {
return keys;
};

var object_toString = uncurryThis(Object.prototype.toString);

function isObject(value) {
return value === Object(value);
}

// generator related shims

// FIXME: Remove this function once ES6 generators are in SpiderMonkey.
function isStopIteration(exception) {
return (
object_toString(exception) === "[object StopIteration]" ||
exception instanceof QReturnValue
);
}

// FIXME: Remove this helper and Q.return once ES6 generators are in
// SpiderMonkey.
var QReturnValue;
if (typeof ReturnValue !== "undefined") {
QReturnValue = ReturnValue;
} else {
QReturnValue = function (value) {
this.value = value;
};
}

// Until V8 3.19 / Chromium 29 is released, SpiderMonkey is the only
// engine that has a deployed base of browsers that support generators.
// However, SM's generators use the Python-inspired semantics of
// outdated ES6 drafts. We would like to support ES6, but we'd also
// like to make it possible to use generators in deployed browsers, so
// we also support Python-style generators. At some point we can remove
// this block.
var hasES6Generators;
try {
/* jshint evil: true, nonew: false */
new Function("(function* (){ yield 1; })");
hasES6Generators = true;
} catch (e) {
hasES6Generators = false;
}

// long stack traces

var STACK_JUMP_SEPARATOR = "From previous event:";
Expand Down Expand Up @@ -1184,29 +1145,15 @@ function async(makeGenerator) {
// when verb is "throw", arg is an exception
function continuer(verb, arg) {
var result;
if (hasES6Generators) {
try {
result = generator[verb](arg);
} catch (exception) {
return reject(exception);
}
if (result.done) {
return result.value;
} else {
return when(result.value, callback, errback);
}
try {
result = generator[verb](arg);
} catch (exception) {
return reject(exception);
}
if (result.done) {
return result.value;
} else {
// FIXME: Remove this case when SM does ES6 generators.
try {
result = generator[verb](arg);
} catch (exception) {
if (isStopIteration(exception)) {
return exception.value;
} else {
return reject(exception);
}
}
return when(result, callback, errback);
return when(result.value, callback, errback);
}
}
var generator = makeGenerator.apply(this, arguments);
Expand All @@ -1228,36 +1175,6 @@ function spawn(makeGenerator) {
Q.done(Q.async(makeGenerator)());
}

// FIXME: Remove this interface once ES6 generators are in SpiderMonkey.
/**
* Throws a ReturnValue exception to stop an asynchronous generator.
*
* This interface is a stop-gap measure to support generator return
* values in older Firefox/SpiderMonkey. In browsers that support ES6
* generators like Chromium 29, just use "return" in your generator
* functions.
*
* @param value the return value for the surrounding generator
* @throws ReturnValue exception with the value.
* @example
* // ES6 style
* Q.async(function* () {
* var foo = yield getFooPromise();
* var bar = yield getBarPromise();
* return foo + bar;
* })
* // Older SpiderMonkey style
* Q.async(function () {
* var foo = yield getFooPromise();
* var bar = yield getBarPromise();
* Q.return(foo + bar);
* })
*/
Q["return"] = _return;
function _return(value) {
throw new QReturnValue(value);
}

/**
* The promised function decorator ensures that any promise arguments
* are settled and passed as values (`this` is also settled and passed
Expand Down

0 comments on commit ae90d83

Please sign in to comment.