js 实现单步调试

Tags
javascript
Created
May 30, 2016 2:17 AM

// 生成器版https://www.h5jun.com/post/building-an-in.html

例:http://unwinder.test.h5jun.com/

首先实现状态机:

function foo() {

var x = 5;

var y = 6;

return x + y;

}

将函数 foo 变换成状态机,看起来像下面这样:

function foo() {

var $__next = 0, x, y;

while(1) {

switch($__next) {

case 0:

x = 5;

$__next = 1;

break;

case 1:

y = 6;

$__next = 2;

break;

case 2:

return x + y;

}

}

}

保存状态:

debugger 语句被编译成一个抛出 continuation exception 异常的语句。这个异常被 catch 语句捕获,我们在其中处理异常然后再将它重新抛出。

包括调用栈中所以状态

最后恢复执行

实时断点:

将代码的行列位置对应到状态机特定的 case 语句,使得我们能够在代码的任意位置暂停运行,并设置一个断点在那个位置。

SuperMade with Super