From 7df8a05129c230ee0345c20c4416fac0279697a8 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 13 Jan 2011 11:57:28 -0800 Subject: [PATCH] debugger: primative object inspection --- lib/_debugger.js | 91 ++++++++++++++++++++++++++--- test/simple/test-debugger-client.js | 5 +- 2 files changed, 86 insertions(+), 10 deletions(-) diff --git a/lib/_debugger.js b/lib/_debugger.js index 554cdaa097..717026369c 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -210,6 +210,22 @@ Client.prototype.reqVersion = function(cb) { }; +Client.prototype.reqLookup = function(refs, cb) { + // TODO: We have a cache of handle's we've already seen in this.handles + // This can be used if we're careful. + var req = { + command: 'lookup', + arguments: { + handles: refs + } + }; + + this.req(req, function(res) { + if (cb) cb(res); + }); +}; + + Client.prototype.reqEval = function(expression, cb) { var self = this; var req = { @@ -225,9 +241,10 @@ Client.prototype.reqEval = function(expression, cb) { } this.req(req, function(res) { - console.error('reqEval res ', res.body); - self._addHandle(res.body); - if (cb) cb(res.body); + if (res.success) { + self._addHandle(res.body); + } + if (cb) cb(res); }); }; @@ -307,6 +324,60 @@ Client.prototype.step = function(action, count, cb) { }; +Client.prototype.mirrorObject = function(handle, cb) { + var self = this; + + if (handle.type == 'object') { + // The handle looks something like this: + // { handle: 8, + // type: 'object', + // className: 'Object', + // constructorFunction: { ref: 9 }, + // protoObject: { ref: 4 }, + // prototypeObject: { ref: 2 }, + // properties: [ { name: 'hello', propertyType: 1, ref: 10 } ], + // text: '#' } + + // For now ignore the className and constructor and prototype. + // TJ's method of object inspection would probably be good for this: + // https://groups.google.com/forum/?pli=1#!topic/nodejs-dev/4gkWBOimiOg + + var propertyRefs = handle.properties.map(function(p) { + return p.ref; + }); + + this.reqLookup(propertyRefs, function(res) { + if (!res.success) { + console.error("problem with reqLookup"); + if (cb) cb(handle); + return; + } + + var mirror = {}; + for (var i = 0; i < handle.properties.length; i++) { + var value = res.body[handle.properties[i].ref]; + self._addHandle(value); + mirror[handle.properties[i].name] = value.text; + } + + if (cb) cb(mirror); + }); + + } else if (handle.type == 'string') { + process.nextTick(function() { + cb(handle.text); + }); + + } else { + process.nextTick(function() { + cb(handle); + }); + } +}; + + + + var commands = [ 'backtrace', 'continue', @@ -724,12 +795,16 @@ Interface.prototype.handleCommand = function(cmd) { } else { cmd = cmd.slice(i); client.reqEval(cmd, function(res) { - if (res) { - console.log(res.text); - } else { - console.log(res); + if (!res.success) { + console.log(res.message); + term.prompt(); + return; } - term.prompt(); + + client.mirrorObject(res.body, function(mirror) { + console.log(mirror); + term.prompt(); + }); }); } diff --git a/test/simple/test-debugger-client.js b/test/simple/test-debugger-client.js index 03c6159b91..3890bf10be 100644 --- a/test/simple/test-debugger-client.js +++ b/test/simple/test-debugger-client.js @@ -104,9 +104,10 @@ addTest(function (client, done) { addTest(function (client, done) { console.error("eval 2+2"); client.reqEval("2+2", function (res) { + assert.ok(res.success); console.error(res); - assert.equal('4', res.text); - assert.equal(4, res.value); + assert.equal('4', res.body.text); + assert.equal(4, res.body.value); done(); }); });