@@ -479,6 +479,9 @@ function createCommandContext(inspector) {
479479
480480 const knownBreakpoints = new Map ( ) ;
481481
482+ const writer = createRemoteAwareWriter ( true ) ;
483+ const formatValue = value => writer ( value ) ;
484+
482485 // Clear current line
483486 function clearline ( ) {
484487 if ( stdout . isTTY ) {
@@ -569,26 +572,83 @@ function createCommandContext(inspector) {
569572 } ,
570573
571574 setBreakpoint ( script , line , condition , silent ) {
572- const registerBreakpoint = ( { breakpointId, actualLocation } ) => {
573- knownBreakpoints . set ( breakpointId , actualLocation ) ;
574- } ;
575+ function registerBreakpoint ( { breakpointId, actualLocation } ) {
576+ if ( actualLocation ) {
577+ knownBreakpoints . set ( breakpointId , actualLocation ) ;
578+ if ( ! silent ) return ctx . list ( 5 ) ;
579+ } else {
580+ print ( `Warning: script '${ script } ' was not loaded yet.` ) ;
581+ }
582+ return undefined ;
583+ }
575584
576- // setBreakpoint()
585+ // setBreakpoint(): set breakpoint at current location
577586 if ( script === undefined ) {
578- // set breakpoint at current location
587+ // TODO: assertIsPaused()
579588 return Debugger . setBreakpoint ( { location : currentSourceLocation , condition } )
580589 . then ( registerBreakpoint ) ;
581590 }
582591
583- // setBreakpoint(line)
592+ // setBreakpoint(line): set breakpoint in current script at specific line
584593 if ( line === undefined && typeof script === 'number' ) {
594+ // TODO: assertIsPaused()
585595 const location = Object . assign ( { } , currentSourceLocation , {
586596 lineNumber : script - 1 ,
587597 } ) ;
588598 return Debugger . setBreakpoint ( { location, condition } )
589599 . then ( registerBreakpoint ) ;
590600 }
591- throw new Error ( 'Not implemented' ) ;
601+
602+ if ( typeof script !== 'string' ) {
603+ throw new TypeError ( `setBreakpoint() expects a string, got ${ script } ` ) ;
604+ }
605+
606+ // setBreakpoint('fn()'): Break when a function is called
607+ if ( script . endsWith ( '()' ) ) {
608+ // TODO: handle !currentFrame (~Runtime.evaluate)
609+ return Debugger . evaluateOnCallFrame ( {
610+ callFrameId : currentFrame ,
611+ expression : `debug(${ script . slice ( 0 , - 2 ) } )` ,
612+ includeCommandLineAPI : true ,
613+ } ) . then ( ( ) => undefined ) ; // hide the noise
614+ }
615+
616+ // setBreakpoint('scriptname')
617+ let scriptId = null ;
618+ let ambiguous = false ;
619+ if ( scripts [ script ] ) {
620+ scriptId = script ;
621+ } else {
622+ for ( const id of Object . keys ( scripts ) ) {
623+ if ( scripts [ id ] && scripts [ id ] . url && scripts [ id ] . url . indexOf ( script ) !== - 1 ) {
624+ if ( scriptId !== null ) {
625+ ambiguous = true ;
626+ }
627+ scriptId = id ;
628+ }
629+ }
630+ }
631+
632+ if ( ambiguous ) {
633+ print ( 'Script name is ambiguous' ) ;
634+ return undefined ;
635+ }
636+ if ( line <= 0 ) {
637+ print ( 'Line should be a positive value' ) ;
638+ return undefined ;
639+ }
640+
641+ if ( scriptId !== null ) {
642+ const location = { scriptId, lineNumber : line - 1 } ;
643+ return Debugger . setBreakpoint ( { location, condition } )
644+ . then ( registerBreakpoint ) ;
645+ }
646+
647+ const escapedPath = script . replace ( / ( [ / \\ . ? * ( ) ^ $ { } | [ \] ] ) / g, '\\$1' ) ;
648+ const urlRegex = `^(.*[\\/\\\\])?${ escapedPath } $` ;
649+
650+ return Debugger . setBreakpointByUrl ( { urlRegex, lineNumber : line - 1 , condition } )
651+ . then ( registerBreakpoint ) ;
592652 } ,
593653
594654 get cont ( ) {
@@ -678,9 +738,6 @@ function createCommandContext(inspector) {
678738 return Promise . resolve ( ) ;
679739 }
680740
681- const writer = createRemoteAwareWriter ( true ) ;
682- const formatValue = value => writer ( value ) ;
683-
684741 const inspectValue = expr =>
685742 ctx . debugEval ( expr )
686743 . then ( formatValue )
@@ -729,6 +786,8 @@ function createCommandContext(inspector) {
729786 . then ( ( ) => repl . displayPrompt ( ) ) ;
730787 } ) ;
731788
789+ // TODO: Debugger.on('breakpointResolved', ({ })
790+
732791 Debugger . on ( 'scriptParsed' , ( { scriptId, url } ) => {
733792 if ( url ) {
734793 scripts [ scriptId ] = { url } ;
0 commit comments