Skip to content

Commit 360ce52

Browse files
indutnyry
authored andcommitted
debugger: watch, unwatch, watchers
Fixes #1800.
1 parent 4e43afd commit 360ce52

File tree

2 files changed

+94
-4
lines changed

2 files changed

+94
-4
lines changed

lib/_debugger.js

+70-3
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,9 @@ var commands = [
644644
'clearBreakpoint (cb)',
645645
],
646646
[
647+
'watch',
648+
'unwatch',
649+
'watchers',
647650
'repl',
648651
'restart',
649652
'kill',
@@ -784,6 +787,7 @@ function Interface(stdin, stdout, args) {
784787
control: []
785788
};
786789
this.breakpoints = [];
790+
this._watchers = [];
787791

788792
// Run script automatically
789793
this.pause();
@@ -863,6 +867,8 @@ Interface.prototype.error = function(text) {
863867

864868
// Debugger's `break` event handler
865869
Interface.prototype.handleBreak = function(r) {
870+
var self = this;
871+
866872
this.pause();
867873

868874
// Save execution context's data
@@ -875,10 +881,15 @@ Interface.prototype.handleBreak = function(r) {
875881
// Print break data
876882
this.print(SourceInfo(r));
877883

878-
// And list source
879-
this.list(2);
884+
// Show watchers' values
885+
this.watchers(true, function (err) {
886+
if (err) return self.error(err);
880887

881-
this.resume(true);
888+
// And list source
889+
self.list(2);
890+
891+
self.resume(true);
892+
});
882893
};
883894

884895

@@ -1209,6 +1220,62 @@ Interface.prototype.step = Interface.stepGenerator('in', 1);
12091220
Interface.prototype.out = Interface.stepGenerator('out', 1);
12101221

12111222

1223+
// Watch
1224+
Interface.prototype.watch = function(expr) {
1225+
this._watchers.push(expr);
1226+
};
1227+
1228+
// Unwatch
1229+
Interface.prototype.unwatch = function(expr) {
1230+
var index = this._watchers.indexOf(expr);
1231+
1232+
// Unwatch by expression
1233+
// or
1234+
// Unwatch by watcher number
1235+
this._watchers.splice(index !== -1 ? index : +expr, 1);
1236+
};
1237+
1238+
// List watchers
1239+
Interface.prototype.watchers = function() {
1240+
var self = this,
1241+
verbose = arguments[0] || false,
1242+
callback = arguments[1] || function() {},
1243+
waiting = this._watchers.length,
1244+
values = [];
1245+
1246+
this.pause();
1247+
1248+
if (!waiting) {
1249+
this.resume();
1250+
1251+
return callback();
1252+
}
1253+
1254+
this._watchers.forEach(function(watcher, i) {
1255+
self.debugEval(watcher, null, null, function(err, value) {
1256+
values[i] = err ? '<error>' : value;
1257+
wait();
1258+
});
1259+
});
1260+
1261+
function wait() {
1262+
if (--waiting === 0) {
1263+
if (verbose) self.print('Watchers:');
1264+
1265+
self._watchers.forEach(function(watcher, i) {
1266+
self.print(leftPad(i, ' ') + ': ' + watcher + ' = ' +
1267+
JSON.stringify(values[i]));
1268+
});
1269+
1270+
if (verbose) self.print('');
1271+
1272+
self.resume();
1273+
1274+
callback(null);
1275+
}
1276+
}
1277+
};
1278+
12121279
// Add breakpoint
12131280
Interface.prototype.setBreakpoint = function(script, line,
12141281
condition, silent) {

test/simple/test-debugger-repl.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ child.on('line', function(line) {
4646
assert.ok(expected.length > 0, 'Got unexpected line: ' + line);
4747

4848
var expectedLine = expected[0].lines.shift();
49-
assert.ok(line.match(expectedLine) !== null);
49+
assert.ok(line.match(expectedLine) !== null, expectedLine);
5050

5151
if (expected[0].lines.length === 0) {
5252
var callback = expected[0].callback;
@@ -59,6 +59,15 @@ function addTest(input, output) {
5959
function next() {
6060
if (expected.length > 0) {
6161
child.stdin.write(expected[0].input + '\n');
62+
63+
if (!expected[0].lines) {
64+
process.nextTick(function() {
65+
var callback = expected[0].callback;
66+
expected.shift();
67+
68+
callback && callback();
69+
});
70+
}
6271
} else {
6372
finish();
6473
}
@@ -80,12 +89,26 @@ addTest('n', [
8089
/11/, /12/, /13/, /14/, /15/
8190
]);
8291

92+
// Watch
93+
addTest('watch("\'x\'")');
94+
8395
// Continue
8496
addTest('c', [
8597
/break in .*:7/,
98+
/Watchers/,
99+
/0:\s+'x' = "x"/,
100+
/()/,
86101
/5/, /6/, /7/, /8/, /9/
87102
]);
88103

104+
// Show watchers
105+
addTest('watchers', [
106+
/0:\s+'x' = "x"/
107+
]);
108+
109+
// Unwatch
110+
addTest('unwatch("\'x\'")');
111+
89112
// Step out
90113
addTest('o', [
91114
/break in .*:14/,

0 commit comments

Comments
 (0)