我正在尝试使用基于Web(Javascript)的JS解释器和调试器来进行教学。我在nodejs服务器上运行该程序。只需在"node file.js"
上运行命令child_process.exec()
即可完成解释器部分。
有人能建议一种实现调试器的好方法吗?如果用户设置断点,程序将一直运行到该点,并显示已经声明或更改的变量值,直到该执行点为止。
我尝试使用uglify-js模块执行此操作。我运行UglifyJS.minify(<file.js>[,{options}])
来提取代码中使用的变量并在控制台中回显它们。
我已经为此目的提出了以下代码,但我正在寻找一些可用的开源解决方案。有没有人知道任何可用于此目的的图书馆?
文件: parser.js
var UglifyJS = require("uglify-js");
var exec = require('child_process').exec;
var fs = require('fs');
var allcode = new Object();
allcode.code = "";
allcode.vars = "";
var output = new Object();
output.data = "";
//THE BREAKPOINT MENTIONED BY USER
var tillLine = 3;
var i = 0;
var done = 0;
var filesuffix = "temp301";
var fileout = "output.log";
var child = Array();
var notAllowed = Array("console","log","eval");
fs.writeFileSync("./"+fileout, "");
fs.readFileSync('./demo.js').toString().split('\n').every(function (line) {
allcode.code += line + "\n";
output.data[i] = "";
allcode.vars = "";
fs.writeFileSync("./"+filesuffix+".js", allcode.code);
i++;
//ADD CODE ONLY UPTO THE MENTIONED LINE
if(i>tillLine)
{
return false;
}
else
{
return true;
}
});
//GET THE LIST OF USED VARIABLES
var minified = UglifyJS.minify("./"+filesuffix+".js",{outSourceMap: "out.js.map"});
var map = JSON.parse(minified.map);
map.names.forEach(function(variable){
if(notAllowed.indexOf(variable) == -1)
{
allcode.vars += "console.log('Value of "+variable+" is ' + "+variable+");\n";
}
});
fs.appendFileSync("./"+filesuffix+".js", allcode.vars);
//EXECUTE THE DEBUGGING CODE
child = exec("nodejs " + "./"+filesuffix+".js");
done = 1;
child.stdout.on('data', function(data) {
output.data += data;
});
child.stderr.on('data', function(data) {
output.err += data;
});
child.on('close', function(code) {
fs.writeFileSync("./"+fileout, JSON.stringify(output,null,'\t'));
console.log(output.data);
done = 0;
});
文件: demo.js
var x = 9;
var a = "The sum is ";
var y = 10;
var z = y*y + x;
console.log(a + z);
输出:
admin@tomcat-PC:~/Desktop/node_app$ nodejs parser.js
Value of x is 9
Value of a is The sum is
Value of y is 10
Value of z is 109
答案 0 :(得分:0)
就在我的头脑中,我尝试在用户代码中插入一个函数调用,在预期的调试操作点。
该函数将枚举并打印本地上下文变量以及任何其他有用信息
我会尝试在所选行分号后附加插入的代码,以便不修改下面的行号。