Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// This function:
function foo() {
var x = 5;
var y = 6;
debugger;
return x + y;
}
// Would be compiled to this function. Yes, it's a large transform,
// but it works surprisingly well even if the output JS is very large.
// Also, I hand-wrote the following code (to simplify it) so there
// might be minor errors. Take it as specific pseudo-code.
function foo() {
var $__next = 0, x, y;
try {
if(VM.doRestore) {
var $__frame = VM.popFrame();
$__next = $__frame.next;
var $__child = $__frame.child;
if($__child) {
$__frame.state.$__t1 = $__child.fn.call($__child.thisPtr);
}
x = $__frame.state.x;
y = $__frame.state.y;
}
while(1) {
if (VM.breakpoints[1][$__next] !== undefined)
throw new $ContinuationExc();
switch($__next) {
case 0:
x = 5;
$__next = 1;
break;
case 1:
y = 6;
$__next = 2;
break;
case 2:
$__next = 3;
throw new $ContinuationExc();
case 3:
return x + y;
}
}
}
catch(e) {
if(!(e instanceof $ContinuationExc))
e = new $ContinuationExc({ error: e })
e.pushFrame(new $Frame($__next, { x: x, y: y }));
throw e;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment