0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

debugger: implement 'list'

This commit is contained in:
Ryan Dahl 2011-01-08 19:09:54 -08:00
parent 5342e3e925
commit e1f4b3f009

View File

@ -277,6 +277,20 @@ Client.prototype.listbreakpoints = function(cb) {
});
};
Client.prototype.reqSource = function(from, to, cb) {
var req = {
command: 'source',
fromLine: from,
toLine: to
};
this.req(req, function(res) {
if (cb) cb(res.body);
});
};
// client.next(1, cb);
Client.prototype.step = function(action, count, cb) {
var req = {
@ -293,7 +307,7 @@ Client.prototype.step = function(action, count, cb) {
var helpMessage = 'Commands: run, kill, print, step, next, ' +
'continue, scripts, backtrace, version, quit';
'continue, list, scripts, backtrace, version, quit';
function SourceUnderline(sourceText, position) {
if (!sourceText) return;
@ -468,6 +482,31 @@ Interface.prototype.handleBreak = function(r) {
};
function intChars(n) {
// TODO dumb:
if (n < 50) {
return 2;
} else if (n < 950) {
return 3;
} else if (n < 9950) {
return 4;
} else {
return 5;
}
}
function leftPad(n) {
var s = n.toString();
var nchars = intChars(n);
var nspaces = nchars - s.length;
for (var i = 0; i < nspaces; i++) {
s = ' ' + s;
}
return s;
}
Interface.prototype.handleCommand = function(cmd) {
var self = this;
@ -522,6 +561,44 @@ Interface.prototype.handleCommand = function(cmd) {
term.prompt();
});
} else if ('l' == cmd || 'list' == cmd) {
if (!client) {
self.printNotConnected();
return;
}
var from = client.currentSourceLine - 5;
var to = client.currentSourceLine + 5;
client.reqSource(from, to, function(res) {
var lines = res.source.split('\n');
for (var i = 0; i < lines.length; i++) {
var lineno = res.fromLine + i + 1;
if (lineno < from || lineno > to) continue;
if (lineno == 1) {
// The first line needs to have the module wrapper filtered out of
// it.
var wrapper = require('module').wrapper[0];
lines[i] = lines[i].slice(wrapper.length);
}
if (lineno == 1 + client.currentSourceLine) {
var nchars = intChars(lineno);
var pointer = '';
for (var j = 0; j < nchars - 1; j++) {
pointer += '=';
}
pointer += '>';
console.log(pointer + ' ' + lines[i]);
} else {
console.log(leftPad(lineno) + ' ' + lines[i]);
}
}
term.prompt();
});
} else if (/^backtrace/.test(cmd) || /^bt/.test(cmd)) {
if (!client) {
self.printNotConnected();