From 8e96b8ab9bd559914c18b0bdf3b291862c961f94 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 22 Dec 2010 00:48:22 -0800 Subject: [PATCH] keep track of current frame. eval works for global scope --- lib/_debugger.js | 51 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/lib/_debugger.js b/lib/_debugger.js index 5c0dd555010..0232f312baa 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -110,7 +110,7 @@ Protocol.prototype.serialize = function(req) { }; - +var NO_FRAME = -1; function Client() { net.Stream.call(this); @@ -118,6 +118,9 @@ function Client() { this._reqCallbacks = []; var socket = this; + this.currentFrame = NO_FRAME; + this.currentSourceLine = -1; + // Note that 'Protocol' requires strings instead of Buffers. socket.setEncoding('utf8'); socket.on('data', function(d) { @@ -142,7 +145,7 @@ Client.prototype._onResponse = function(res) { this._reqCallbacks.splice(i, 1); cb(res.body); } else { - console.error("unhandled res: %j", res.body); + this.emit('unhandledResponse', res.body); } }; @@ -161,7 +164,30 @@ Client.prototype.reqVersion = function(cb) { }; -var helpMessage = "Commands: version, eval, help, quit"; +Client.prototype.reqEval = function(expression, cb) { + var req = { + command: 'evaluate', + arguments: { expression: expression } + }; + + if (this.currentFrame == NO_FRAME) req.arguments.global = true; + + this.req(req, function (res) { + if (cb) cb(res.body); + }); +}; + + +// reqBacktrace(cb) +// TODO: from, to, bottom +Client.prototype.reqBacktrace = function(cb) { + this.req({ command: 'backtrace' } , function (res) { + if (cb) cb(res.body); + }); +}; + + +var helpMessage = "Commands: backtrace, version, eval, help, quit"; function startInterface() { @@ -194,13 +220,14 @@ function startInterface() { i.prompt(); }); - } else if (/^eval/.test(cmd)) { - var req = { - command: 'evaluate', - arguments: { 'expression': cmd.slice(5) } - }; + } else if ('backtrace' == cmd || 'bt' == cmd) { + c.reqBacktrace(function (bt) { + console.log(bt); + i.prompt(); + }); - c.req(req, function (res) { + } else if (/^eval/.test(cmd)) { + c.reqEval(cmd.slice(5), function (res) { console.log(res); i.prompt(); }); @@ -214,6 +241,12 @@ function startInterface() { } }); + c.on('unhandledResponse', function (res) { + console.log("\r\nunhandled res:"); + console.log(res.body); + i.prompt(); + }); + i.on('close', function() { stdin.destroy(); });