@@ -194,10 +194,16 @@ Client.prototype._removeScript = function(desc) {
194194
195195
196196Client . prototype . _onResponse = function ( res ) {
197- for ( var i = 0 ; i < this . _reqCallbacks . length ; i ++ ) {
198- var cb = this . _reqCallbacks [ i ] ;
199- if ( this . _reqCallbacks [ i ] . request_seq == res . body . request_seq ) break ;
200- }
197+ var cb ,
198+ index = - 1 ;
199+
200+ this . _reqCallbacks . some ( function ( fn , i ) {
201+ if ( fn . request_seq == res . body . request_seq ) {
202+ cb = fn ;
203+ index = i ;
204+ return true ;
205+ }
206+ } ) ;
201207
202208 var self = this ;
203209 var handled = false ;
@@ -224,7 +230,7 @@ Client.prototype._onResponse = function(res) {
224230 }
225231
226232 if ( cb ) {
227- this . _reqCallbacks . splice ( i , 1 ) ;
233+ this . _reqCallbacks . splice ( index , 1 ) ;
228234 handled = true ;
229235 cb ( res . body ) ;
230236 }
@@ -746,6 +752,7 @@ function Interface() {
746752 debug : [ ] ,
747753 control : [ ]
748754 } ;
755+ this . breakpoints = [ ] ;
749756} ;
750757
751758
@@ -874,13 +881,13 @@ Interface.prototype.debugEval = function(code, context, filename, callback) {
874881function intChars ( n ) {
875882 // TODO dumb:
876883 if ( n < 50 ) {
877- return 2 ;
878- } else if ( n < 950 ) {
879884 return 3 ;
880- } else if ( n < 9950 ) {
885+ } else if ( n < 950 ) {
881886 return 4 ;
882- } else {
887+ } else if ( n < 9950 ) {
883888 return 5 ;
889+ } else {
890+ return 6 ;
884891 }
885892}
886893
@@ -914,15 +921,17 @@ Interface.prototype.run = function() {
914921
915922// Restart script
916923Interface . prototype . restart = function ( ) {
917- if ( ! this . child ) return this . error ( 'App isn\'t running... Try `run` instead' ) ;
924+ if ( ! this . requireConnection ( ) ) return ;
918925
919926 var self = this ;
920927
921- this . killChild ( ) ;
928+ self . pause ( ) ;
929+ self . killChild ( ) ;
922930
923931 // XXX need to wait a little bit for the restart to work?
924932 setTimeout ( function ( ) {
925933 self . trySpawn ( ) ;
934+ self . resume ( ) ;
926935 } , 1000 ) ;
927936} ;
928937
@@ -1093,14 +1102,15 @@ Interface.prototype.out = Interface.stepGenerator('out', 1);
10931102
10941103
10951104// Add breakpoint
1096- Interface . prototype . setBreakpoint = function ( script , line , condition ) {
1105+ Interface . prototype . setBreakpoint = function ( script , line ,
1106+ condition , silent ) {
10971107 if ( ! this . requireConnection ( ) ) return ;
10981108
10991109 var self = this ,
11001110 scriptId ,
11011111 ambiguous ;
11021112
1103- if ( ! this . client . scripts [ script ] ) {
1113+ if ( script != + script && ! this . client . scripts [ script ] ) {
11041114 Object . keys ( this . client . scripts ) . forEach ( function ( id ) {
11051115 if ( self . client . scripts [ id ] . name . indexOf ( script ) !== - 1 ) {
11061116 if ( scriptId ) {
@@ -1127,16 +1137,21 @@ Interface.prototype.setBreakpoint = function(script, line, condition) {
11271137 self . pause ( ) ;
11281138 self . client . setBreakpoint ( req , function ( res ) {
11291139 if ( res . success ) {
1130- self . list ( 5 ) ;
1140+ if ( ! silent ) {
1141+ self . list ( 5 ) ;
1142+ }
11311143 self . client . breakpoints . push ( {
11321144 id : res . body . breakpoint ,
11331145 scriptId : scriptId ,
1134- script : self . client . scripts [ scriptId ] . name ,
1135- line : line
1146+ script : ( self . client . scripts [ scriptId ] || { } ) . name ,
1147+ line : line ,
1148+ condition : condition
11361149 } ) ;
11371150
11381151 } else {
1139- self . print ( req . message || 'error!' ) ;
1152+ if ( ! silent ) {
1153+ self . print ( req . message || 'error!' ) ;
1154+ }
11401155 }
11411156 self . resume ( ) ;
11421157 } ) ;
@@ -1177,7 +1192,7 @@ Interface.prototype.clearBreakpoint = function(script, line) {
11771192 self . pause ( ) ;
11781193 self . client . clearBreakpoint ( req , function ( res ) {
11791194 if ( res . success ) {
1180- self . client . breakpoints = self . client . breakpoints . splice ( index , - 1 ) ;
1195+ self . client . breakpoints . splice ( index , 1 ) ;
11811196 self . list ( 5 ) ;
11821197 } else {
11831198 self . print ( req . message || 'error!' ) ;
@@ -1273,16 +1288,18 @@ Interface.prototype.killChild = function() {
12731288 }
12741289
12751290 if ( this . client ) {
1291+ // Save breakpoints
1292+ this . breakpoints = this . client . breakpoints ;
1293+
12761294 this . client . destroy ( ) ;
12771295 this . client = null ;
12781296 }
1279-
1280- this . resume ( ) ;
12811297} ;
12821298
12831299
12841300Interface . prototype . trySpawn = function ( cb ) {
1285- var self = this ;
1301+ var self = this ,
1302+ breakpoints = this . breakpoints || [ ] ;
12861303
12871304 this . killChild ( ) ;
12881305
@@ -1293,17 +1310,23 @@ Interface.prototype.trySpawn = function(cb) {
12931310
12941311 this . pause ( ) ;
12951312
1296- var client = self . client = new Client ( ) ;
1297- var connectionAttempts = 0 ;
1313+ var client = self . client = new Client ( ) ,
1314+ connectionAttempts = 0 ;
12981315
12991316 client . once ( 'ready' , function ( ) {
13001317 process . stdout . write ( ' ok\n' ) ;
13011318
13021319 // since we did debug-brk, we're hitting a break point immediately
13031320 // continue before anything else.
13041321 client . reqContinue ( function ( ) {
1305- self . resume ( ) ;
13061322 if ( cb ) cb ( ) ;
1323+
1324+ // Restore breakpoints
1325+ breakpoints . forEach ( function ( bp ) {
1326+ self . setBreakpoint ( bp . scriptId , bp . line , bp . condition , true ) ;
1327+ } ) ;
1328+
1329+ self . resume ( ) ;
13071330 } ) ;
13081331
13091332 client . on ( 'close' , function ( ) {
0 commit comments