@@ -142,7 +142,10 @@ Client.prototype._onResponse = function(res) {
142142
143143 if ( res . headers . Type == 'connect' ) {
144144 // do nothing
145+ console . log ( res ) ;
145146 this . emit ( 'ready' ) ;
147+ } else if ( res . body && res . body . event == 'break' ) {
148+ this . emit ( 'break' , res . body ) ;
146149 } else if ( cb ) {
147150 this . _reqCallbacks . splice ( i , 1 ) ;
148151 cb ( res . body ) ;
@@ -214,33 +217,76 @@ Client.prototype.reqScripts = function(cb) {
214217
215218Client . prototype . reqContinue = function ( cb ) {
216219 this . req ( { command : 'continue' } , function ( res ) {
217- if ( cb ) cb ( res . body ) ;
220+ if ( cb ) cb ( res ) ;
218221 } ) ;
219222} ;
220223
221224
222225var helpMessage = "Commands: scripts, backtrace, version, eval, help, quit" ;
223226
227+ function SourceUnderline ( source_text , position ) {
228+ if ( ! source_text ) {
229+ return ;
230+ }
231+
232+ // Create an underline with a caret pointing to the source position. If the
233+ // source contains a tab character the underline will have a tab character in
234+ // the same place otherwise the underline will have a space character.
235+ var underline = '' ;
236+ for ( var i = 0 ; i < position ; i ++ ) {
237+ if ( source_text [ i ] == '\t' ) {
238+ underline += '\t' ;
239+ } else {
240+ underline += ' ' ;
241+ }
242+ }
243+ underline += '^' ;
244+
245+ // Return the source line text with the underline beneath.
246+ return source_text + '\n' + underline ;
247+ }
248+
249+ function SourceInfo ( body ) {
250+ var result = '' ;
251+
252+ if ( body . script ) {
253+ if ( body . script . name ) {
254+ result += body . script . name ;
255+ } else {
256+ result += '[unnamed]' ;
257+ }
258+ }
259+ result += ' line ' ;
260+ result += body . sourceLine + 1 ;
261+ result += ' column ' ;
262+ result += body . sourceColumn + 1 ;
263+
264+ return result ;
265+ }
266+
267+
268+
269+
224270
225271function startInterface ( ) {
226272
227- var i = readline . createInterface ( process . stdout ) ;
273+ var term = readline . createInterface ( process . stdout ) ;
228274 var stdin = process . openStdin ( ) ;
229275 stdin . addListener ( 'data' , function ( chunk ) {
230- i . write ( chunk ) ;
276+ term . write ( chunk ) ;
231277 } ) ;
232278
233279 var prompt = 'debug> ' ;
234280
235- i . setPrompt ( prompt ) ;
236- i . prompt ( ) ;
281+ term . setPrompt ( 'debug> ' ) ;
282+ term . prompt ( ) ;
237283
238284 var quitTried = false ;
239285
240286 function tryQuit ( ) {
241287 if ( quitTried ) return ;
242288 quitTried = true ;
243- i . close ( ) ;
289+ term . close ( ) ;
244290 console . log ( "debug done\n" ) ;
245291 if ( c . writable ) {
246292 c . reqContinue ( function ( res ) {
@@ -251,63 +297,110 @@ function startInterface() {
251297 }
252298 }
253299
254- i . on ( 'SIGINT' , tryQuit ) ;
255- i . on ( 'close' , tryQuit ) ;
300+ term . on ( 'SIGINT' , tryQuit ) ;
301+ term . on ( 'close' , tryQuit ) ;
302+ c . on ( 'close' , function ( ) {
303+ process . exit ( 0 ) ;
304+ } ) ;
256305
257- i . on ( 'line' , function ( cmd ) {
306+ term . on ( 'line' , function ( cmd ) {
258307 if ( cmd == 'quit' ) {
259308 tryQuit ( ) ;
260309 } else if ( / ^ h e l p / . test ( cmd ) ) {
261310 console . log ( helpMessage ) ;
262- i . prompt ( ) ;
311+ term . prompt ( ) ;
263312
264313 } else if ( 'version' == cmd ) {
265314 c . reqVersion ( function ( v ) {
266315 console . log ( v ) ;
267- i . prompt ( ) ;
316+ term . prompt ( ) ;
268317 } ) ;
269318
270- } else if ( ' backtrace' == cmd || 'bt' == cmd ) {
319+ } else if ( / ^ b a c k t r a c e / . test ( cmd ) || / ^ b t / . test ( cmd ) ) {
271320 c . reqBacktrace ( function ( bt ) {
272- console . log ( bt ) ;
273- i . prompt ( ) ;
321+ if ( / f u l l / . test ( cmd ) ) {
322+ console . log ( bt ) ;
323+ } else if ( bt . totalFrames == 0 ) {
324+ console . log ( '(empty stack)' ) ;
325+ } else {
326+ var result = 'Frames #' + bt . fromFrame +
327+ ' to #' + ( bt . toFrame - 1 ) +
328+ ' of ' + bt . totalFrames + '\n' ;
329+ for ( j = 0 ; j < bt . frames . length ; j ++ ) {
330+ if ( j != 0 ) result += '\n' ;
331+ result += bt . frames [ j ] . text ;
332+ }
333+ console . log ( result ) ;
334+ }
335+ term . prompt ( ) ;
274336 } ) ;
275337
276- } else if ( ' continue' == cmd || 'c' == cmd ) {
338+ } else if ( / ^ c o n t i n u e / . test ( cmd ) || / ^ c / . test ( cmd ) ) {
277339 c . reqContinue ( function ( res ) {
278- console . log ( res ) ;
279- i . prompt ( ) ;
340+ // Wait for break point. (disable raw mode?)
280341 } ) ;
281342
282343 } else if ( / ^ s c r i p t s / . test ( cmd ) ) {
283344 c . reqScripts ( function ( res ) {
284- var text = res . map ( function ( x ) { return x . text ; } ) ;
285- console . log ( text . join ( '\n' ) ) ;
286- i . prompt ( ) ;
345+ if ( / f u l l / . test ( cmd ) ) {
346+ console . log ( res ) ;
347+ } else {
348+ var text = res . map ( function ( x ) { return x . text ; } ) ;
349+ console . log ( text . join ( '\n' ) ) ;
350+ }
351+ term . prompt ( ) ;
287352 } ) ;
288353
289354 } else if ( / ^ e v a l / . test ( cmd ) ) {
290355 c . reqEval ( cmd . slice ( 5 ) , function ( res ) {
291356 console . log ( res ) ;
292- i . prompt ( ) ;
357+ term . prompt ( ) ;
293358 } ) ;
294359
295360 } else {
296361 if ( ! / ^ \s * $ / . test ( cmd ) ) {
297362 // If it's not all white-space print this error message.
298363 console . log ( 'Unknown command "%s". Try "help"' , cmd ) ;
299364 }
300- i . prompt ( ) ;
365+ term . prompt ( ) ;
301366 }
302367 } ) ;
303368
304369 c . on ( 'unhandledResponse' , function ( res ) {
305370 console . log ( "\r\nunhandled res:" ) ;
306- console . log ( res . body ) ;
307- i . prompt ( ) ;
371+ console . log ( res ) ;
372+ term . prompt ( ) ;
308373 } ) ;
309374
310- i . on ( 'close' , function ( ) {
311- stdin . destroy ( ) ;
375+ c . on ( 'break' , function ( res ) {
376+ var result = '' ;
377+ if ( res . body . breakpoints ) {
378+ result += 'breakpoint' ;
379+ if ( res . body . breakpoints . length > 1 ) {
380+ result += 's' ;
381+ }
382+ result += ' #' ;
383+ for ( var i = 0 ; i < res . body . breakpoints . length ; i ++ ) {
384+ if ( i > 0 ) {
385+ result += ', #' ;
386+ }
387+ result += res . body . breakpoints [ i ] ;
388+ }
389+ } else {
390+ result += 'break' ;
391+ }
392+ result += ' in ' ;
393+ result += res . body . invocationText ;
394+ result += ', ' ;
395+ result += SourceInfo ( res . body ) ;
396+ result += '\n' ;
397+ result += SourceUnderline ( res . body . sourceLineText , res . body . sourceColumn ) ;
398+
399+ c . currentSourceLine = res . body . sourceLine ;
400+ c . currentFrame = 0 ;
401+
402+ console . log ( result ) ;
403+
404+ term . prompt ( ) ;
312405 } ) ;
313406}
0 commit comments