From 360ce526fd5391a955005b27f327bda83acac6dd Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Fri, 30 Sep 2011 01:18:35 +0700 Subject: [PATCH] debugger: watch, unwatch, watchers Fixes #1800. --- lib/_debugger.js | 73 +++++++++++++++++++++++++++++++++++++-- test/simple/test-debugger-repl.js | 25 +++++++++++++- 2 files changed, 94 insertions(+), 4 deletions(-) diff --git a/lib/_debugger.js b/lib/_debugger.js index b90d8fb..8fa09e7 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -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 ? '' : 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) { diff --git a/test/simple/test-debugger-repl.js b/test/simple/test-debugger-repl.js index 25d3cee..6152fd4 100644 --- a/test/simple/test-debugger-repl.js +++ b/test/simple/test-debugger-repl.js @@ -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/, -- 2.7.4