Debugger: better maintance of script list
authorRyan Dahl <ry@tinyclouds.org>
Thu, 30 Dec 2010 18:02:42 +0000 (10:02 -0800)
committerRyan Dahl <ry@tinyclouds.org>
Thu, 30 Dec 2010 18:02:42 +0000 (10:02 -0800)
lib/_debugger.js

index c401066efa57e6709e5e1f9f1cbe9ad2d7da084b..a364910597805dd9b087029b61d64b71c184cbc6 100644 (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) {