From f4124e18cb64c01975fae41e133a8716775647fd Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Tue, 27 Sep 2011 13:25:31 +0700 Subject: [PATCH] debugger: setBreakpoint('fn()') Fixes #1777 --- lib/_debugger.js | 79 +++++++++++++++++++------------ test/simple/test-debugger-repl.js | 28 +++++++++-- 2 files changed, 75 insertions(+), 32 deletions(-) diff --git a/lib/_debugger.js b/lib/_debugger.js index 8d16a89418..c3803911dc 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -1217,47 +1217,68 @@ 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'); + + var req = { + type: 'scriptId', + target: scriptId, + line: line - 1, + condition: condition + }; } - 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 - }; - self.pause(); self.client.setBreakpoint(req, function(res) { if (res.success) { 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(); diff --git a/test/simple/test-debugger-repl.js b/test/simple/test-debugger-repl.js index 58e240528e..e551fc61b0 100644 --- a/test/simple/test-debugger-repl.js +++ b/test/simple/test-debugger-repl.js @@ -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"