Skip to content

Commit f4124e1

Browse files
indutnyry
authored andcommitted
debugger: setBreakpoint('fn()')
Fixes #1777
1 parent fa2eaea commit f4124e1

File tree

2 files changed

+74
-31
lines changed

2 files changed

+74
-31
lines changed

lib/_debugger.js

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,47 +1217,68 @@ Interface.prototype.setBreakpoint = function(script, line,
12171217
line = this.client.currentSourceLine + 1;
12181218
}
12191219

1220-
if (script != +script && !this.client.scripts[script]) {
1221-
Object.keys(this.client.scripts).forEach(function(id) {
1222-
if (self.client.scripts[id].name.indexOf(script) !== -1) {
1223-
if (scriptId) {
1224-
ambiguous = true;
1225-
}
1226-
scriptId = id;
1227-
}
1228-
});
1220+
if (/\(\)$/.test(script)) {
1221+
// setBreakpoint('functionname()');
1222+
var req = {
1223+
type: 'function',
1224+
target: script.replace(/\(\)$/, ''),
1225+
condition: condition
1226+
};
12291227
} else {
1230-
scriptId = script;
1231-
}
1228+
// setBreakpoint('scriptname')
1229+
if (script != +script && !this.client.scripts[script]) {
1230+
var scripts = this.client.scripts;
1231+
Object.keys(scripts).forEach(function(id) {
1232+
if (scripts[id] && scripts[id].name.indexOf(script) !== -1) {
1233+
if (scriptId) {
1234+
ambiguous = true;
1235+
}
1236+
scriptId = id;
1237+
}
1238+
});
1239+
} else {
1240+
scriptId = script;
1241+
}
12321242

1233-
if (!scriptId) return this.error('Script : ' + script + ' not found');
1234-
if (ambiguous) return this.error('Script name is ambiguous');
1235-
if (line <= 0) return this.error('Line should be a positive value');
1243+
if (!scriptId) return this.error('Script : ' + script + ' not found');
1244+
if (ambiguous) return this.error('Script name is ambiguous');
1245+
if (line <= 0) return this.error('Line should be a positive value');
12361246

1237-
var req = {
1238-
type: 'scriptId',
1239-
target: scriptId,
1240-
line: line - 1,
1241-
condition: condition
1242-
};
1247+
var req = {
1248+
type: 'scriptId',
1249+
target: scriptId,
1250+
line: line - 1,
1251+
condition: condition
1252+
};
1253+
}
12431254

12441255
self.pause();
12451256
self.client.setBreakpoint(req, function(res) {
12461257
if (res.success) {
12471258
if (!silent) {
12481259
self.list(5);
12491260
}
1250-
self.client.breakpoints.push({
1251-
id: res.body.breakpoint,
1252-
scriptId: scriptId,
1253-
script: (self.client.scripts[scriptId] || {}).name,
1254-
line: line,
1255-
condition: condition
1256-
});
1261+
1262+
// Try load scriptId and line from response
1263+
if (!scriptId) {
1264+
scriptId = res.body.script_id;
1265+
line = res.body.line;
1266+
}
1267+
1268+
// If we finally have one - remember this breakpoint
1269+
if (scriptId) {
1270+
self.client.breakpoints.push({
1271+
id: res.body.breakpoint,
1272+
scriptId: scriptId,
1273+
script: (self.client.scripts[scriptId] || {}).name,
1274+
line: line,
1275+
condition: condition
1276+
});
1277+
}
12571278

12581279
} else {
12591280
if (!silent) {
1260-
self.print(req.message || 'error!');
1281+
self.print(res.message || 'error!');
12611282
}
12621283
}
12631284
self.resume();

test/simple/test-debugger-repl.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ child.stderr.pipe(process.stdout);
4343
var expected = [];
4444

4545
child.on('line', function(line) {
46-
console.log(JSON.stringify(line));
4746
assert.ok(expected.length > 0, 'Got unexpected line: ' + line);
4847

4948
var expectedLine = expected[0].lines.shift();
@@ -60,8 +59,6 @@ function addTest(input, output) {
6059
function next() {
6160
if (expected.length > 0) {
6261
child.stdin.write(expected[0].input + '\n');
63-
console.log('---');
64-
console.log('>>', expected[0].input);
6562
} else {
6663
finish();
6764
}
@@ -121,6 +118,31 @@ addTest('c', [
121118
"\b 9 };"
122119
]);
123120

121+
// Set breakpoint by function name
122+
addTest('sb("setInterval()", "!(setInterval.flag++)")', [
123+
"debug> \b 2 debugger;",
124+
"\b 3 debugger;",
125+
"\b 4 function a(x) {",
126+
"\b 5 var i = 10;",
127+
"\b 6 while (--i != 0);",
128+
"\b 7 debugger;",
129+
"\b 8 return i;",
130+
"\b 9 };",
131+
"\b 10 function b() {",
132+
"\b 11 return ['hello', 'world'].join(' ');",
133+
"\b 12 };"
134+
]);
135+
136+
// Continue
137+
addTest('c', [
138+
"debug> debug> debug> debug> \bbreak in node.js:150",
139+
"\b*148 ",
140+
"\b 149 global.setInterval = function() {",
141+
"\b 150 var t = NativeModule.require('timers');",
142+
"\b 151 return t.setInterval.apply(this, arguments);",
143+
"\b 152 };"
144+
]);
145+
124146
// Continue
125147
addTest('c, bt', [
126148
"debug> \bCan't request backtrace now"

0 commit comments

Comments
 (0)