Debugger: better maintance of script list

pull/22966/head
Ryan Dahl 2010-12-30 10:02:42 -08:00
parent bb400d5697
commit f484cbf4c7
1 changed files with 56 additions and 19 deletions

View File

@ -190,6 +190,8 @@ function Client() {
this.currentFrame = 0;
this.currentSourceLine = -1;
this.currentSource = null;
this.handles = {};
this.scripts = {};
// Note that 'Protocol' requires strings instead of Buffers.
socket.setEncoding('utf8');
@ -203,22 +205,53 @@ inherits(Client, net.Stream);
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) {
for (var i = 0; i < this._reqCallbacks.length; i++) {
var cb = this._reqCallbacks[i];
if (this._reqCallbacks[i].request_seq == cb.request_seq) break;
}
var self = this;
if (res.headers.Type == 'connect') {
// do nothing
this.emit('ready');
// Request a list of scripts for our own storage.
self.reqScripts();
self.emit('ready');
} else if (res.body && res.body.event == 'break') {
this.emit('break', res.body);
} 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) {
this._reqCallbacks.splice(i, 1);
cb(res.body);
} else {
this.emit('unhandledResponse', res.body);
}
@ -283,8 +316,12 @@ Client.prototype.reqBacktrace = function(cb) {
// text: 'node.js (lines: 562)' }
//
Client.prototype.reqScripts = function(cb) {
var self = this;
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() {
@ -397,7 +446,7 @@ function startInterface() {
if (quitTried) return;
quitTried = true;
term.close();
console.log("debug done\n");
console.log("\ndebug done\n");
if (c.writable) {
c.reqContinue(function (res) {
process.exit(0);
@ -470,20 +519,8 @@ function startInterface() {
});
} else if (cmd == 'scripts' || cmd == 'scripts full') {
c.reqScripts(function (res) {
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();
});
printScripts();
term.prompt();
} else if (/^continue/.test(cmd) || /^c/.test(cmd)) {
c.reqContinue(function (res) {