debugger: setBreakpoint('fn()')
authorFedor Indutny <fedor.indutny@gmail.com>
Tue, 27 Sep 2011 06:25:31 +0000 (13:25 +0700)
committerRyan Dahl <ry@tinyclouds.org>
Tue, 27 Sep 2011 07:26:16 +0000 (00:26 -0700)
Fixes #1777

lib/_debugger.js
test/simple/test-debugger-repl.js

index 8d16a89..c380391 100644 (file)
@@ -1217,29 +1217,40 @@ Interface.prototype.setBreakpoint = function(script, line,
     line = this.client.currentSourceLine + 1;
   }
 
-  if (script != +script && !this.client.scripts[script]) {
-    Object.keys(this.client.scripts).forEach(function(id) {
-      if (self.client.scripts[id].name.indexOf(script) !== -1) {
-        if (scriptId) {
-          ambiguous = true;
-        }
-        scriptId = id;
-      }
-    });
+  if (/\(\)$/.test(script)) {
+    // setBreakpoint('functionname()');
+    var req = {
+      type: 'function',
+      target: script.replace(/\(\)$/, ''),
+      condition: condition
+    };
   } else {
-    scriptId = script;
-  }
+    // setBreakpoint('scriptname')
+    if (script != +script && !this.client.scripts[script]) {
+      var scripts = this.client.scripts;
+      Object.keys(scripts).forEach(function(id) {
+        if (scripts[id] && scripts[id].name.indexOf(script) !== -1) {
+          if (scriptId) {
+            ambiguous = true;
+          }
+          scriptId = id;
+        }
+      });
+    } else {
+      scriptId = script;
+    }
 
-  if (!scriptId) return this.error('Script : ' + script + ' not found');
-  if (ambiguous) return this.error('Script name is ambiguous');
-  if (line <= 0) return this.error('Line should be a positive value');
+    if (!scriptId) return this.error('Script : ' + script + ' not found');
+    if (ambiguous) return this.error('Script name is ambiguous');
+    if (line <= 0) return this.error('Line should be a positive value');
 
-  var req = {
-    type: 'scriptId',
-    target: scriptId,
-    line: line - 1,
-    condition: condition
-  };
+    var req = {
+      type: 'scriptId',
+      target: scriptId,
+      line: line - 1,
+      condition: condition
+    };
+  }
 
   self.pause();
   self.client.setBreakpoint(req, function(res) {
@@ -1247,17 +1258,27 @@ Interface.prototype.setBreakpoint = function(script, line,
       if (!silent) {
         self.list(5);
       }
-      self.client.breakpoints.push({
-        id: res.body.breakpoint,
-        scriptId: scriptId,
-        script: (self.client.scripts[scriptId] || {}).name,
-        line: line,
-        condition: condition
-      });
+
+      // Try load scriptId and line from response
+      if (!scriptId) {
+        scriptId = res.body.script_id;
+        line = res.body.line;
+      }
+
+      // If we finally have one - remember this breakpoint
+      if (scriptId) {
+        self.client.breakpoints.push({
+          id: res.body.breakpoint,
+          scriptId: scriptId,
+          script: (self.client.scripts[scriptId] || {}).name,
+          line: line,
+          condition: condition
+        });
+      }
 
     } else {
       if (!silent) {
-        self.print(req.message || 'error!');
+        self.print(res.message || 'error!');
       }
     }
     self.resume();
index 58e2405..e551fc6 100644 (file)
@@ -43,7 +43,6 @@ child.stderr.pipe(process.stdout);
 var expected = [];
 
 child.on('line', function(line) {
-  console.log(JSON.stringify(line));
   assert.ok(expected.length > 0, 'Got unexpected line: ' + line);
 
   var expectedLine = expected[0].lines.shift();
@@ -60,8 +59,6 @@ function addTest(input, output) {
   function next() {
     if (expected.length > 0) {
       child.stdin.write(expected[0].input + '\n');
-      console.log('---');
-      console.log('>>', expected[0].input);
     } else {
       finish();
     }
@@ -121,6 +118,31 @@ addTest('c', [
   "\b  9 };"
 ]);
 
+// Set breakpoint by function name
+addTest('sb("setInterval()", "!(setInterval.flag++)")', [
+  "debug> \b  2 debugger;",
+  "\b  3 debugger;",
+  "\b  4 function a(x) {",
+  "\b  5   var i = 10;",
+  "\b  6   while (--i != 0);",
+  "\b  7   debugger;",
+  "\b  8   return i;",
+  "\b  9 };",
+  "\b 10 function b() {",
+  "\b 11   return ['hello', 'world'].join(' ');",
+  "\b 12 };"
+]);
+
+// Continue
+addTest('c', [
+  "debug> debug> debug> debug> \bbreak in node.js:150",
+  "\b*148 ",
+  "\b 149     global.setInterval = function() {",
+  "\b 150       var t = NativeModule.require('timers');",
+  "\b 151       return t.setInterval.apply(this, arguments);",
+  "\b 152     };"
+]);
+
 // Continue
 addTest('c, bt', [
   "debug> \bCan't request backtrace now"