Skip to content

Commit

Permalink
Add try/catch helper to fix #148
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnordlund committed Dec 7, 2014
1 parent 06b4ba0 commit 6abfb9b
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 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 @@ -66,10 +76,12 @@
var callThrow = step.bind(generator["throw"]);

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

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

Expand Down Expand Up @@ -101,16 +113,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 @@ -166,9 +180,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 @@ -190,7 +206,7 @@
return info;
}

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

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

0 comments on commit 6abfb9b

Please sign in to comment.