@@ -644,6 +644,9 @@ var commands = [
644
644
'clearBreakpoint (cb)' ,
645
645
] ,
646
646
[
647
+ 'watch' ,
648
+ 'unwatch' ,
649
+ 'watchers' ,
647
650
'repl' ,
648
651
'restart' ,
649
652
'kill' ,
@@ -784,6 +787,7 @@ function Interface(stdin, stdout, args) {
784
787
control : [ ]
785
788
} ;
786
789
this . breakpoints = [ ] ;
790
+ this . _watchers = [ ] ;
787
791
788
792
// Run script automatically
789
793
this . pause ( ) ;
@@ -863,6 +867,8 @@ Interface.prototype.error = function(text) {
863
867
864
868
// Debugger's `break` event handler
865
869
Interface . prototype . handleBreak = function ( r ) {
870
+ var self = this ;
871
+
866
872
this . pause ( ) ;
867
873
868
874
// Save execution context's data
@@ -875,10 +881,15 @@ Interface.prototype.handleBreak = function(r) {
875
881
// Print break data
876
882
this . print ( SourceInfo ( r ) ) ;
877
883
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 ) ;
880
887
881
- this . resume ( true ) ;
888
+ // And list source
889
+ self . list ( 2 ) ;
890
+
891
+ self . resume ( true ) ;
892
+ } ) ;
882
893
} ;
883
894
884
895
@@ -1209,6 +1220,62 @@ Interface.prototype.step = Interface.stepGenerator('in', 1);
1209
1220
Interface . prototype . out = Interface . stepGenerator ( 'out' , 1 ) ;
1210
1221
1211
1222
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
+
1212
1279
// Add breakpoint
1213
1280
Interface . prototype . setBreakpoint = function ( script , line ,
1214
1281
condition , silent ) {
0 commit comments