Browse Source

Debugger: better maintance of script list

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
f484cbf4c7
  1. 73
      lib/_debugger.js

73
lib/_debugger.js

@ -190,6 +190,8 @@ function Client() {
this.currentFrame = 0; this.currentFrame = 0;
this.currentSourceLine = -1; this.currentSourceLine = -1;
this.currentSource = null; this.currentSource = null;
this.handles = {};
this.scripts = {};
// Note that 'Protocol' requires strings instead of Buffers. // Note that 'Protocol' requires strings instead of Buffers.
socket.setEncoding('utf8'); socket.setEncoding('utf8');
@ -203,22 +205,53 @@ inherits(Client, net.Stream);
exports.Client = Client; exports.Client = Client;
Client.prototype._addHandle = function(desc) {
if (typeof desc != 'object' || !desc.handle) throw new Error("bad type");
this.handles[desc.id] = desc;
if (desc.type == 'script') {
this._addScript(desc);
}
};
Client.prototype._addScript = function(desc) {
this.scripts[desc.id] = desc;
};
Client.prototype._removeScript = function(desc) {
this.scripts[desc.id] = undefined;
};
Client.prototype._onResponse = function(res) { Client.prototype._onResponse = function(res) {
for (var i = 0; i < this._reqCallbacks.length; i++) { for (var i = 0; i < this._reqCallbacks.length; i++) {
var cb = this._reqCallbacks[i]; var cb = this._reqCallbacks[i];
if (this._reqCallbacks[i].request_seq == cb.request_seq) break; if (this._reqCallbacks[i].request_seq == cb.request_seq) break;
} }
var self = this;
if (res.headers.Type == 'connect') { if (res.headers.Type == 'connect') {
// do nothing // Request a list of scripts for our own storage.
this.emit('ready'); self.reqScripts();
self.emit('ready');
} else if (res.body && res.body.event == 'break') { } else if (res.body && res.body.event == 'break') {
this.emit('break', res.body); this.emit('break', res.body);
} else if (res.body && res.body.event == 'afterCompile') { } else if (res.body && res.body.event == 'afterCompile') {
this.emit('afterCompile', res.body); this._addHandle(res.body.body.script);
} else if (res.body && res.body.event == 'scriptCollected') {
// ???
this._removeScript(res.body.body.script);
} else if (cb) { } else if (cb) {
this._reqCallbacks.splice(i, 1); this._reqCallbacks.splice(i, 1);
cb(res.body); cb(res.body);
} else { } else {
this.emit('unhandledResponse', res.body); this.emit('unhandledResponse', res.body);
} }
@ -283,8 +316,12 @@ Client.prototype.reqBacktrace = function(cb) {
// text: 'node.js (lines: 562)' } // text: 'node.js (lines: 562)' }
// //
Client.prototype.reqScripts = function(cb) { Client.prototype.reqScripts = function(cb) {
var self = this;
this.req({ command: 'scripts' } , function (res) { this.req({ command: 'scripts' } , function (res) {
if (cb) cb(res.body); for (var i = 0; i < res.body.length; i++) {
self._addHandle(res.body[i]);
}
if (cb) cb();
}); });
}; };
@ -376,6 +413,18 @@ function restartQuestion (cb) {
}); });
} }
function printScripts () {
var text = '';
for (var id in c.scripts) {
var script = c.scripts[id];
if (typeof script == 'object' && script.name) {
text += script.name == c.currentScript ? '* ' : ' ';
text += script.name + '\n';
}
}
process.stdout.write(text);
}
function startInterface() { function startInterface() {
@ -397,7 +446,7 @@ function startInterface() {
if (quitTried) return; if (quitTried) return;
quitTried = true; quitTried = true;
term.close(); term.close();
console.log("debug done\n"); console.log("\ndebug done\n");
if (c.writable) { if (c.writable) {
c.reqContinue(function (res) { c.reqContinue(function (res) {
process.exit(0); process.exit(0);
@ -470,20 +519,8 @@ function startInterface() {
}); });
} else if (cmd == 'scripts' || cmd == 'scripts full') { } else if (cmd == 'scripts' || cmd == 'scripts full') {
c.reqScripts(function (res) { printScripts();
if (/full/.test(cmd)) {
console.log(res);
} else {
var text = '';
for (var i = 0; i < res.length; i++) {
text += res[i].name == c.currentScript ? '* ' : ' ';
text += res[i].name + '\n';
}
process.stdout.write(text);
}
term.prompt(); term.prompt();
});
} else if (/^continue/.test(cmd) || /^c/.test(cmd)) { } else if (/^continue/.test(cmd) || /^c/.test(cmd)) {
c.reqContinue(function (res) { c.reqContinue(function (res) {

Loading…
Cancel
Save