Skip to content

Commit 44741c1

Browse files
committed
start displaying let and const vars
1 parent 42f7533 commit 44741c1

11 files changed

Lines changed: 398 additions & 187 deletions

‎v4-cokapi/backends/javascript/jslogger.js‎

Lines changed: 117 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ var prevStack = null;
434434

435435
function 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

‎v4-cokapi/backends/javascript/tests/closure1.golden‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@
133133
is_zombie: false,
134134
parent_frame_id_list: [],
135135
unique_hash: 'bar_f3',
136-
ordered_varnames: [ 'x', 'parent:y' ],
137-
encoded_locals: { x: 2, 'parent:y': 1 } } ],
136+
ordered_varnames: [ 'parent:y', 'x' ],
137+
encoded_locals: { 'parent:y': 1, x: 2 } } ],
138138
globals: { globalZ: 10, foo: [ 'REF', 1 ], b: [ 'REF', 2 ] },
139139
ordered_globals: [ 'globalZ', 'foo', 'b' ],
140140
line: 5,
@@ -163,8 +163,8 @@
163163
is_zombie: false,
164164
parent_frame_id_list: [],
165165
unique_hash: 'bar_f3',
166-
ordered_varnames: [ 'x', 'parent:y' ],
167-
encoded_locals: { x: 2, 'parent:y': 1 } } ],
166+
ordered_varnames: [ 'parent:y', 'x' ],
167+
encoded_locals: { 'parent:y': 1, x: 2 } } ],
168168
globals: { globalZ: 110, foo: [ 'REF', 1 ], b: [ 'REF', 2 ] },
169169
ordered_globals: [ 'globalZ', 'foo', 'b' ],
170170
line: 6,
@@ -193,8 +193,8 @@
193193
is_zombie: false,
194194
parent_frame_id_list: [],
195195
unique_hash: 'bar_f3',
196-
ordered_varnames: [ 'x', 'parent:y', '__return__' ],
197-
encoded_locals: { x: 2, 'parent:y': 1, __return__: 113 } } ],
196+
ordered_varnames: [ 'parent:y', 'x', '__return__' ],
197+
encoded_locals: { 'parent:y': 1, x: 2, __return__: 113 } } ],
198198
globals: { globalZ: 110, foo: [ 'REF', 1 ], b: [ 'REF', 2 ] },
199199
ordered_globals: [ 'globalZ', 'foo', 'b' ],
200200
line: 6,

‎v4-cokapi/backends/javascript/tests/es6-const-mozilla.golden‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,55 @@
1212
{ stdout: '',
1313
func_name: '',
1414
stack_to_render: [],
15-
globals: {},
16-
ordered_globals: [],
15+
globals: { MY_FAV: 7 },
16+
ordered_globals: [ 'MY_FAV' ],
1717
line: 7,
1818
col: 0,
1919
event: 'step_line',
2020
heap: {} },
2121
{ stdout: 'my favorite number is: 7\n',
2222
func_name: '',
2323
stack_to_render: [],
24-
globals: {},
25-
ordered_globals: [],
24+
globals: { MY_FAV: 7 },
25+
ordered_globals: [ 'MY_FAV' ],
2626
line: 10,
2727
col: 0,
2828
event: 'step_line',
2929
heap: {} },
3030
{ stdout: 'my favorite number is: 7\nmy favorite number is 7\n',
3131
func_name: '',
3232
stack_to_render: [],
33-
globals: {},
34-
ordered_globals: [],
33+
globals: { MY_FAV: 7 },
34+
ordered_globals: [ 'MY_FAV' ],
3535
line: 13,
3636
col: 0,
3737
event: 'step_line',
3838
heap: {} },
3939
{ stdout: 'my favorite number is: 7\nmy favorite number is 7\n',
4040
func_name: '',
4141
stack_to_render: [],
42-
globals: {},
43-
ordered_globals: [],
42+
globals: { MY_FAV: 7, A: 1 },
43+
ordered_globals: [ 'MY_FAV', 'A' ],
4444
line: 16,
4545
col: 0,
4646
event: 'step_line',
4747
heap: {} },
4848
{ stdout: 'my favorite number is: 7\nmy favorite number is 7\n',
4949
func_name: '',
5050
stack_to_render: [],
51-
globals: {},
52-
ordered_globals: [],
51+
globals: { MY_FAV: 7, A: 1, MY_OBJECT: [ 'REF', 1 ] },
52+
ordered_globals: [ 'MY_FAV', 'A', 'MY_OBJECT' ],
5353
line: 19,
5454
col: 0,
5555
event: 'step_line',
56-
heap: {} },
56+
heap: { '1': [ 'INSTANCE', '', [ 'key', 'value' ] ] } },
5757
{ exception_msg: 'TypeError: Assignment to constant variable.',
5858
stdout: 'my favorite number is: 7\nmy favorite number is 7\n',
5959
func_name: '',
6060
stack_to_render: [],
61-
globals: {},
62-
ordered_globals: [],
61+
globals: { MY_FAV: 7, A: 1, MY_OBJECT: [ 'REF', 1 ] },
62+
ordered_globals: [ 'MY_FAV', 'A', 'MY_OBJECT' ],
6363
line: 19,
6464
col: 10,
6565
event: 'exception',
66-
heap: {} } ] }
66+
heap: { '1': [ 'INSTANCE', '', [ 'key', 'value' ] ] } } ] }

‎v4-cokapi/backends/javascript/tests/es6-global-let.golden‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,35 @@
3232
{ stdout: '',
3333
func_name: '',
3434
stack_to_render: [],
35-
globals: { a: 5, b: 10 },
36-
ordered_globals: [ 'a', 'b' ],
35+
globals: { 'a (block 1)': [ 'JS_SPECIAL_VAL', 'undefined' ], a: 5, b: 10 },
36+
ordered_globals: [ 'a (block 1)', 'a', 'b' ],
3737
line: 5,
3838
col: 2,
3939
event: 'step_line',
4040
heap: {} },
4141
{ stdout: '',
4242
func_name: '',
4343
stack_to_render: [],
44-
globals: { a: 5, b: 10 },
45-
ordered_globals: [ 'a', 'b' ],
44+
globals: { 'a (block 1)': 4, a: 5, b: 10 },
45+
ordered_globals: [ 'a (block 1)', 'a', 'b' ],
4646
line: 6,
4747
col: 2,
4848
event: 'step_line',
4949
heap: {} },
5050
{ stdout: '',
5151
func_name: '',
5252
stack_to_render: [],
53-
globals: { a: 5, b: 1 },
54-
ordered_globals: [ 'a', 'b' ],
53+
globals: { 'a (block 1)': 4, a: 5, b: 1 },
54+
ordered_globals: [ 'a (block 1)', 'a', 'b' ],
5555
line: 8,
5656
col: 2,
5757
event: 'step_line',
5858
heap: {} },
5959
{ stdout: '4\n',
6060
func_name: '',
6161
stack_to_render: [],
62-
globals: { a: 5, b: 1 },
63-
ordered_globals: [ 'a', 'b' ],
62+
globals: { 'a (block 1)': 4, a: 5, b: 1 },
63+
ordered_globals: [ 'a (block 1)', 'a', 'b' ],
6464
line: 9,
6565
col: 2,
6666
event: 'step_line',

0 commit comments

Comments
 (0)