Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
debugger: watch, unwatch, watchers
Fixes #1800.
  • Loading branch information
indutny authored and ry committed Sep 30, 2011
1 parent 4e43afd commit 360ce52
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
73 changes: 70 additions & 3 deletions lib/_debugger.js
Expand Up @@ -644,6 +644,9 @@ var commands = [
'clearBreakpoint (cb)',
],
[
'watch',
'unwatch',
'watchers',
'repl',
'restart',
'kill',
Expand Down Expand Up @@ -784,6 +787,7 @@ function Interface(stdin, stdout, args) {
control: []
};
this.breakpoints = [];
this._watchers = [];

// Run script automatically
this.pause();
Expand Down Expand Up @@ -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
Expand All @@ -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);
});
};


Expand Down Expand Up @@ -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) {
Expand Down
25 changes: 24 additions & 1 deletion test/simple/test-debugger-repl.js
Expand Up @@ -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;
Expand All @@ -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();
}
Expand All @@ -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/,
Expand Down

0 comments on commit 360ce52

Please sign in to comment.