@@ -434,7 +434,7 @@ var prevStack = null;
434434
435435function listener ( event , execState , eventData , data ) {
436436 var stepType , i , n ;
437- var ii , jj , sc , scopeType , scopeObj ;
437+ var ii , jj , sc , scopeType , scopeObj , scopeIdx ;
438438 var f ;
439439
440440 // TODO: catch CompileError and maybe other events too
@@ -525,6 +525,8 @@ function listener(event, execState, eventData, data) {
525525 }
526526 prevStack = curStack ;
527527
528+ //log('======');
529+ //log('all_userscript_frames.length:', all_userscript_frames.length);
528530 var topFrame = all_userscript_frames [ 0 ] ;
529531 var topIsReturn = topFrame . isAtReturn ( ) ;
530532 if ( topIsReturn ) {
@@ -602,67 +604,12 @@ function listener(event, execState, eventData, data) {
602604 curTraceEntry . event = logEventType ;
603605 curTraceEntry . heap = getHeap ( ) ;
604606
605- // inspect only the top "global" frame to grab globals
606- for ( ii = 0 ; ii < topFrame . scopeCount ( ) ; ii ++ ) {
607- sc = topFrame . scope ( ii ) ;
608-
609- /* From v8/src/debug/debug-scopes.h
610-
611- enum ScopeType {
612- ScopeTypeGlobal = 0,
613- ScopeTypeLocal,
614- ScopeTypeWith,
615- ScopeTypeClosure,
616- ScopeTypeCatch,
617- ScopeTypeBlock,
618- ScopeTypeScript,
619- ScopeTypeModule
620- };
621-
622- 0: Global
623- 1: Local
624- 2: With
625- 3: Closure
626- 4: Catch
627- 5: Block scope for ES6 let (only in Node v6)
628- 6: Script (only in Node v6)
629- 7: Module (only in Node v6)
630-
631- */
632- scopeType = sc . details_ . details_ [ 0 ] ;
633- if ( scopeType === 0 ) { // Global -- to handle global variables
634- scopeObj = sc . details_ . details_ [ 1 ] ;
635- var globalScopePairs = _ . pairs ( scopeObj ) ;
636- for ( jj = 0 ; jj < globalScopePairs . length ; jj ++ ) {
637- var globalVarname = globalScopePairs [ jj ] [ 0 ] ;
638- var globalVal = globalScopePairs [ jj ] [ 1 ] ;
639- if ( ! _ . has ( IGNORE_GLOBAL_VARS , globalVarname ) ) {
640- curTraceEntry . ordered_globals . push ( globalVarname ) ;
641- assert ( ! _ . has ( curTraceEntry . globals , globalVarname ) ) ;
642- curTraceEntry . globals [ globalVarname ] = encodeObject ( globalVal ) ;
643- }
644- }
645- } else if ( scopeType === 4 ) { // Catch -- to handle global exception blocks
646- scopeObj = sc . details_ . details_ [ 1 ] ;
647-
648- var globalCatchScopePairs = _ . pairs ( scopeObj ) ;
649- for ( jj = 0 ; jj < globalCatchScopePairs . length ; jj ++ ) {
650- var globalCatchVarname = globalCatchScopePairs [ jj ] [ 0 ] ;
651- var globalCatchVal = globalCatchScopePairs [ jj ] [ 1 ] ;
652- curTraceEntry . ordered_globals . push ( globalCatchVarname ) ;
653- assert ( ! _ . has ( curTraceEntry . globals , globalCatchVarname ) ) ;
654- curTraceEntry . globals [ globalCatchVarname ] = encodeObject ( globalCatchVal ) ;
655- }
656- } else if ( scopeType === 5 ) { // Block -- handle global ES6 let-style blocks
657- // TODO: handle me. how do we uniquely identify this block? by
658- // line or other code location? look up GetNestedScopeChain in
659- // v8 source code
660- }
661- }
607+ var hasLocalBlock = false ;
662608
663609 for ( i = 0 ;
664- i < all_userscript_frames . length - 1 ; /* last frame is fake 'top-level' frame */
610+ i < all_userscript_frames . length - 1 ; /* last frame is fake 'top-level' global frame */
665611 i ++ ) {
612+ //log('all_userscript_frames[' + i + ']');
666613 var traceStackEntry = { } ;
667614
668615 f = all_userscript_frames [ i ] ;
@@ -758,10 +705,13 @@ function listener(event, execState, eventData, data) {
758705 }
759706 */
760707
708+ //log(' f.scopeCount()', f.scopeCount());
761709 var nParentScopes = 1 ;
762- for ( ii = 0 ;
763- ii < f . scopeCount ( ) ;
764- ii ++ ) {
710+ // trick: go through the scopes BACKWARDS so that nested blocks
711+ // are displayed in order (outer blocks before inner blocks)
712+ for ( ii = f . scopeCount ( ) - 1 ;
713+ ii >= 0 ;
714+ ii -- ) {
765715 sc = f . scope ( ii ) ;
766716
767717 /* From v8/src/debug/debug-scopes.h
@@ -788,6 +738,7 @@ function listener(event, execState, eventData, data) {
788738
789739 */
790740 scopeType = sc . details_ . details_ [ 0 ] ;
741+ //log(' scopeType:', scopeType);
791742 var e ;
792743 // DON'T grab globals again since it's redundant
793744 if ( scopeType === 1 || scopeType === 4 ) { // Local or Catch (for exceptions)
@@ -796,6 +747,7 @@ function listener(event, execState, eventData, data) {
796747 scopeObj = sc . details_ . details_ [ 1 ] ;
797748 assert ( _ . isObject ( scopeObj ) ) ;
798749 var localScopePairs = _ . pairs ( scopeObj ) ;
750+ //log('Local vars:', util.inspect(scopeObj));
799751 for ( jj = 0 ; jj < localScopePairs . length ; jj ++ ) {
800752 e = localScopePairs [ jj ] ;
801753 traceStackEntry . ordered_varnames . push ( e [ 0 ] ) ;
@@ -827,13 +779,27 @@ function listener(event, execState, eventData, data) {
827779 }
828780
829781 nParentScopes ++ ;
782+ } else if ( scopeType === 6 ) { // Script
783+ // ignore since we'll extract the Script block data from the
784+ // toplevel scope (see below); it should all be pointing to
785+ // the same object anyhow
830786 } else if ( scopeType === 5 ) { // block scope for ES6 let (?) only in Node v6
831- // TODO: handle me. how do we uniquely identify this block? by
832- // line or other code location? look up GetNestedScopeChain in
833- // v8 source code
787+ scopeIdx = sc . scope_index_ + 1 ; // uniquely identify this block (+1 for human readability)
788+ scopeObj = sc . details_ . details_ [ 1 ] ;
789+ assert ( _ . isObject ( scopeObj ) ) ;
790+ //log('Local block:', scopeIdx, util.inspect(sc, {showHidden: true, depth: null}));
791+ hasLocalBlock = true ;
792+
793+ var localScopePairs = _ . pairs ( scopeObj ) ;
794+ for ( jj = 0 ; jj < localScopePairs . length ; jj ++ ) {
795+ var mungedVarName = e [ 0 ] + ' (block ' + scopeIdx + ')' ;
796+ e = localScopePairs [ jj ] ;
797+ traceStackEntry . ordered_varnames . push ( mungedVarName ) ;
798+ assert ( ! _ . has ( traceStackEntry . encoded_locals , mungedVarName ) ) ;
799+ traceStackEntry . encoded_locals [ mungedVarName ] = encodeObject ( e [ 1 ] ) ;
800+ }
834801 } else {
835802 assert ( scopeType === 0 ||
836- scopeType === 6 ||
837803 scopeType === 7 ) ;
838804 }
839805 }
@@ -849,6 +815,91 @@ function listener(event, execState, eventData, data) {
849815 curTraceEntry . stack_to_render . unshift ( traceStackEntry ) ;
850816 }
851817
818+ //log(' topFrame.scopeCount()', topFrame.scopeCount());
819+ // finally, inspect only the top-level "global" frame to grab globals
820+ for ( ii = 0 ; ii < topFrame . scopeCount ( ) ; ii ++ ) {
821+ sc = topFrame . scope ( ii ) ;
822+
823+ /* From v8/src/debug/debug-scopes.h
824+
825+ enum ScopeType {
826+ ScopeTypeGlobal = 0,
827+ ScopeTypeLocal,
828+ ScopeTypeWith,
829+ ScopeTypeClosure,
830+ ScopeTypeCatch,
831+ ScopeTypeBlock,
832+ ScopeTypeScript,
833+ ScopeTypeModule
834+ };
835+
836+ 0: Global
837+ 1: Local
838+ 2: With
839+ 3: Closure
840+ 4: Catch
841+ 5: Block scope for ES6 let (only in Node v6)
842+ 6: Script (only in Node v6)
843+ 7: Module (only in Node v6)
844+
845+ */
846+ scopeType = sc . details_ . details_ [ 0 ] ;
847+ //log(' G scopeType:', scopeType);
848+ if ( scopeType === 0 || scopeType === 6 ) {
849+ // 0: Global -- global variables declared with 'var' (not let/const)
850+ // 6: Script -- top-level globals declared with 'let' and 'const'
851+ // (i.e., not within a nested block)
852+ //
853+ // I think it's fine to handle both scopes here since variable
854+ // names should not collide. i.e., you can't declare a var and
855+ // let/const variable with the SAME NAME in the top-level global scope
856+ scopeObj = sc . details_ . details_ [ 1 ] ;
857+ var globalScopePairs = _ . pairs ( scopeObj ) ;
858+ //log(scopeType, _.keys(scopeObj));
859+ for ( jj = 0 ; jj < globalScopePairs . length ; jj ++ ) {
860+ var globalVarname = globalScopePairs [ jj ] [ 0 ] ;
861+ var globalVal = globalScopePairs [ jj ] [ 1 ] ;
862+ if ( ! _ . has ( IGNORE_GLOBAL_VARS , globalVarname ) ) {
863+ curTraceEntry . ordered_globals . push ( globalVarname ) ;
864+ assert ( ! _ . has ( curTraceEntry . globals , globalVarname ) ) ;
865+ curTraceEntry . globals [ globalVarname ] = encodeObject ( globalVal ) ;
866+ }
867+ }
868+ } else if ( scopeType === 4 ) { // Catch -- to handle global exception blocks
869+ scopeObj = sc . details_ . details_ [ 1 ] ;
870+
871+ var globalCatchScopePairs = _ . pairs ( scopeObj ) ;
872+ for ( jj = 0 ; jj < globalCatchScopePairs . length ; jj ++ ) {
873+ var globalCatchVarname = globalCatchScopePairs [ jj ] [ 0 ] ;
874+ var globalCatchVal = globalCatchScopePairs [ jj ] [ 1 ] ;
875+ curTraceEntry . ordered_globals . push ( globalCatchVarname ) ;
876+ assert ( ! _ . has ( curTraceEntry . globals , globalCatchVarname ) ) ;
877+ curTraceEntry . globals [ globalCatchVarname ] = encodeObject ( globalCatchVal ) ;
878+ }
879+ } else if ( scopeType === 5 && ! hasLocalBlock ) { // Block -- handle global ES6 let-style blocks
880+ // we do a hasLocalBlock check since for some WEIRD WEIRD WEIRD
881+ // reason, if there's a block scope in a function's frame, then
882+ // that block scope gets DUPLICATED in the global frame, which
883+ // ends up being redundant and super confusing. hasLocalBlock is
884+ // a hack to elide that problem.
885+
886+ scopeIdx = sc . scope_index_ + 1 ; // uniquely identify this block (+1 for human readability)
887+ scopeObj = sc . details_ . details_ [ 1 ] ;
888+ assert ( _ . isObject ( scopeObj ) ) ;
889+ //log('Global block:', util.inspect(sc, {showHidden: true, depth: null}));
890+ var globalScopePairs = _ . pairs ( scopeObj ) ;
891+ //log(scopeType, _.keys(scopeObj));
892+ for ( jj = 0 ; jj < globalScopePairs . length ; jj ++ ) {
893+ var globalVarname = globalScopePairs [ jj ] [ 0 ] + ' (block ' + scopeIdx + ')' ;
894+ var globalVal = globalScopePairs [ jj ] [ 1 ] ;
895+ if ( ! _ . has ( IGNORE_GLOBAL_VARS , globalVarname ) ) {
896+ curTraceEntry . ordered_globals . push ( globalVarname ) ;
897+ assert ( ! _ . has ( curTraceEntry . globals , globalVarname ) ) ;
898+ curTraceEntry . globals [ globalVarname ] = encodeObject ( globalVal ) ;
899+ }
900+ }
901+ }
902+ }
852903
853904 // check whether the top frame is currently returning, and if so,
854905 // update frameIdCalls. it's VERY IMPORTANT to do this update at
0 commit comments