diff --git a/lib/_debugger.js b/lib/_debugger.js index 83a119d994f..3fd61238f02 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -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();