@@ -705,10 +705,22 @@ function listener(event, execState, eventData, data) {
705705 }
706706 */
707707
708- //log(' f.scopeCount()', f.scopeCount());
708+ // first calculate the total number of block scopes so that we can
709+ // number them properly to maintain their identities (tricky hack)
710+ var nBlockScopes = 0 ;
711+ for ( ii = 0 ; ii < f . scopeCount ( ) ; ii ++ ) {
712+ sc = f . scope ( ii ) ;
713+ scopeType = sc . details_ . details_ [ 0 ] ;
714+ if ( scopeType === 5 ) {
715+ nBlockScopes ++ ;
716+ }
717+ }
718+
719+ //log(' f.scopeCount()', f.scopeCount(), ', nBlockScopes:', nBlockScopes);
720+
709721 var nParentScopes = 1 ;
710- // trick: go through the scopes BACKWARDS so that nested blocks
711- // are displayed in order (outer blocks before inner blocks)
722+ // TODO: for some weird reason, it doesn't work when I iterate
723+ // forwards, strange
712724 for ( ii = f . scopeCount ( ) - 1 ;
713725 ii >= 0 ;
714726 ii -- ) {
@@ -738,7 +750,7 @@ function listener(event, execState, eventData, data) {
738750
739751 */
740752 scopeType = sc . details_ . details_ [ 0 ] ;
741- //log(' scopeType:', scopeType);
753+ //log(' scopeType:', scopeType, ii );
742754 var e ;
743755 // DON'T grab globals again since it's redundant
744756 if ( scopeType === 1 || scopeType === 4 ) { // Local or Catch (for exceptions)
@@ -784,7 +796,9 @@ function listener(event, execState, eventData, data) {
784796 // toplevel scope (see below); it should all be pointing to
785797 // the same object anyhow
786798 } else if ( scopeType === 5 ) { // block scope for ES6 let (?) only in Node v6
787- scopeIdx = sc . scope_index_ + 1 ; // uniquely identify this block (+1 for human readability)
799+ // uniquely identify this block (need to subtract from nBlockScopes
800+ // to get the right number when there are multiple nested blocks (tricky weird!)
801+ scopeIdx = nBlockScopes - sc . scope_index_ ;
788802 scopeObj = sc . details_ . details_ [ 1 ] ;
789803 assert ( _ . isObject ( scopeObj ) ) ;
790804 //log('Local block:', scopeIdx, util.inspect(sc, {showHidden: true, depth: null}));
@@ -815,9 +829,23 @@ function listener(event, execState, eventData, data) {
815829 curTraceEntry . stack_to_render . unshift ( traceStackEntry ) ;
816830 }
817831
832+ // first calculate the total number of block scopes so that we can
833+ // number them properly to maintain their identities (tricky hack)
834+ var nGlobalBlockScopes = 0 ;
835+ for ( ii = 0 ; ii < topFrame . scopeCount ( ) ; ii ++ ) {
836+ sc = topFrame . scope ( ii ) ;
837+ scopeType = sc . details_ . details_ [ 0 ] ;
838+ if ( scopeType === 5 ) {
839+ nGlobalBlockScopes ++ ;
840+ }
841+ }
842+
818843 //log(' topFrame.scopeCount()', topFrame.scopeCount());
819844 // finally, inspect only the top-level "global" frame to grab globals
820- for ( ii = 0 ; ii < topFrame . scopeCount ( ) ; ii ++ ) {
845+ // go backwards to make things a bit more human-readable
846+ for ( ii = topFrame . scopeCount ( ) - 1 ;
847+ ii >= 0 ;
848+ ii -- ) {
821849 sc = topFrame . scope ( ii ) ;
822850
823851 /* From v8/src/debug/debug-scopes.h
@@ -883,7 +911,9 @@ function listener(event, execState, eventData, data) {
883911 // ends up being redundant and super confusing. hasLocalBlock is
884912 // a hack to elide that problem.
885913
886- scopeIdx = sc . scope_index_ + 1 ; // uniquely identify this block (+1 for human readability)
914+ // uniquely identify this block (need to subtract from nGlobalBlockScopes
915+ // to get the right number when there are multiple nested blocks (tricky weird!)
916+ scopeIdx = nGlobalBlockScopes - sc . scope_index_ ;
887917 scopeObj = sc . details_ . details_ [ 1 ] ;
888918 assert ( _ . isObject ( scopeObj ) ) ;
889919 //log('Global block:', util.inspect(sc, {showHidden: true, depth: null}));
0 commit comments