Skip to content

Commit

Permalink
Merge pull request #150 from maxnordlund/issue-148-try-catch-helper
Browse files Browse the repository at this point in the history
Add try/catch helper to fix #148.
  • Loading branch information
benjamn committed Jan 13, 2015
2 parents 01651f2 + f1062ad commit 222d491
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
return new Generator(innerFn, outerFn, self || null, tryList || []);
}
runtime.wrap = wrap;

// Try/catch helper to minemize deoptimizations
var _try = function(block) {
try {
block();
return undefined;
} catch (err) {
return err;
}
};

var GenStateSuspendedStart = "suspendedStart";
var GenStateSuspendedYield = "suspendedYield";
Expand Down Expand Up @@ -70,10 +80,12 @@
var callThrow = step.bind(generator["throw"]);

function step(arg) {
try {
var info = this(arg);
var value = info.value;
} catch (error) {
var info, value, self = this, error = _try(function() {
info = self(arg);
value = info.value;
});

if (error) {
return reject(error);
}

Expand Down Expand Up @@ -107,16 +119,18 @@
while (true) {
var delegate = context.delegate;
if (delegate) {
try {
var info = delegate.iterator[method](arg);
var info, uncaught = _try(function() {
info = delegate.iterator[method](arg);

// Delegate generator ran and handled its own exceptions so
// regardless of what the method was, we continue as if it is
// "next" with an undefined arg.
method = "next";
arg = undefined;

} catch (uncaught) {
})

if (uncaught) {
context.delegate = null;

// Like returning generator.throw(uncaught), but without the
Expand Down Expand Up @@ -172,9 +186,11 @@

state = GenStateExecuting;

try {
var value = innerFn.call(self, context);
var value, thrown = _try(function() {
value = innerFn.call(self, context);
})

if (!thrown) {
// If an exception is thrown from innerFn, we leave state ===
// GenStateExecuting and loop back for another invocation.
state = context.done
Expand All @@ -196,7 +212,7 @@
return info;
}

} catch (thrown) {
} else {
state = GenStateCompleted;

if (method === "next") {
Expand Down

0 comments on commit 222d491

Please sign in to comment.