Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Add try/catch helper to fix #148 #150

Merged
merged 1 commit into from
Jan 13, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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