@@ -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
865869Interface . 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);
12091220Interface . 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
12131280Interface . prototype . setBreakpoint = function ( script , line ,
12141281 condition , silent ) {
0 commit comments