debugger: watch, unwatch, watchers

Fixes #1800.
This commit is contained in:
Fedor Indutny
2011-09-30 01:18:35 +07:00
committed by Ryan Dahl
parent 4e43afd973
commit 360ce526fd
2 changed files with 94 additions and 4 deletions

View File

@@ -644,6 +644,9 @@ var commands = [
'clearBreakpoint (cb)',
],
[
'watch',
'unwatch',
'watchers',
'repl',
'restart',
'kill',
@@ -784,6 +787,7 @@ function Interface(stdin, stdout, args) {
control: []
};
this.breakpoints = [];
this._watchers = [];
// Run script automatically
this.pause();
@@ -863,6 +867,8 @@ Interface.prototype.error = function(text) {
// Debugger's `break` event handler
Interface.prototype.handleBreak = function(r) {
var self = this;
this.pause();
// Save execution context's data
@@ -875,10 +881,15 @@ Interface.prototype.handleBreak = function(r) {
// Print break data
this.print(SourceInfo(r));
// And list source
this.list(2);
// Show watchers' values
this.watchers(true, function (err) {
if (err) return self.error(err);
this.resume(true);
// And list source
self.list(2);
self.resume(true);
});
};
@@ -1209,6 +1220,62 @@ Interface.prototype.step = Interface.stepGenerator('in', 1);
Interface.prototype.out = Interface.stepGenerator('out', 1);
// Watch
Interface.prototype.watch = function(expr) {
this._watchers.push(expr);
};
// Unwatch
Interface.prototype.unwatch = function(expr) {
var index = this._watchers.indexOf(expr);
// Unwatch by expression
// or
// Unwatch by watcher number
this._watchers.splice(index !== -1 ? index : +expr, 1);
};
// List watchers
Interface.prototype.watchers = function() {
var self = this,
verbose = arguments[0] || false,
callback = arguments[1] || function() {},
waiting = this._watchers.length,
values = [];
this.pause();
if (!waiting) {
this.resume();
return callback();
}
this._watchers.forEach(function(watcher, i) {
self.debugEval(watcher, null, null, function(err, value) {
values[i] = err ? '<error>' : value;
wait();
});
});
function wait() {
if (--waiting === 0) {
if (verbose) self.print('Watchers:');
self._watchers.forEach(function(watcher, i) {
self.print(leftPad(i, ' ') + ': ' + watcher + ' = ' +
JSON.stringify(values[i]));
});
if (verbose) self.print('');
self.resume();
callback(null);
}
}
};
// Add breakpoint
Interface.prototype.setBreakpoint = function(script, line,
condition, silent) {

View File

@@ -46,7 +46,7 @@ child.on('line', function(line) {
assert.ok(expected.length > 0, 'Got unexpected line: ' + line);
var expectedLine = expected[0].lines.shift();
assert.ok(line.match(expectedLine) !== null);
assert.ok(line.match(expectedLine) !== null, expectedLine);
if (expected[0].lines.length === 0) {
var callback = expected[0].callback;
@@ -59,6 +59,15 @@ function addTest(input, output) {
function next() {
if (expected.length > 0) {
child.stdin.write(expected[0].input + '\n');
if (!expected[0].lines) {
process.nextTick(function() {
var callback = expected[0].callback;
expected.shift();
callback && callback();
});
}
} else {
finish();
}
@@ -80,12 +89,26 @@ addTest('n', [
/11/, /12/, /13/, /14/, /15/
]);
// Watch
addTest('watch("\'x\'")');
// Continue
addTest('c', [
/break in .*:7/,
/Watchers/,
/0:\s+'x' = "x"/,
/()/,
/5/, /6/, /7/, /8/, /9/
]);
// Show watchers
addTest('watchers', [
/0:\s+'x' = "x"/
]);
// Unwatch
addTest('unwatch("\'x\'")');
// Step out
addTest('o', [
/break in .*:14/,