Skip to content

Commit e109dec

Browse files
committed
Merge branch 'master' of github.com:pgbovine/OnlinePythonTutor
2 parents 56dd19f + 09c54d1 commit e109dec

22 files changed

Lines changed: 2118 additions & 171 deletions

‎v4-cokapi/backends/javascript/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ diffall:
2020
jshint jslogger.js && python golden_test.py --diffall
2121

2222
deps:
23-
npm install eval underscore esprima minimist source-map
23+
npm install eval underscore minimist source-map
2424
sudo npm install typescript # ugh need sudo
2525
#sudo npm link typescript # (maybe needed so that require('typescript') will work

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

Lines changed: 185 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ https://code.google.com/p/v8-wiki/wiki/DebuggerProtocol
3131
Prereqs:
3232
npm install eval
3333
npm install underscore
34-
npm install esprima
3534
npm install minimist
3635
3736
For TypeScript support:
@@ -89,7 +88,6 @@ TypeScript TODOs:
8988
"use strict";
9089

9190
var _eval = require('eval');
92-
var esprima = require('esprima');
9391
var util = require('util');
9492
var fs = require('fs');
9593
var _ = require('underscore');
@@ -210,6 +208,7 @@ function assert(cond) {
210208
}
211209

212210

211+
var NUM_ADDED_LINES = 2; // match up with how many lines we added ...
213212
function wrapUserscript(userscript) {
214213
var s = "\"use strict\";\ndebugger;\n";
215214
s += userscript.rtrim();
@@ -435,7 +434,7 @@ var prevStack = null;
435434

436435
function listener(event, execState, eventData, data) {
437436
var stepType, i, n;
438-
var ii, jj, sc, scopeType, scopeObj;
437+
var ii, jj, sc, scopeType, scopeObj, scopeIdx;
439438
var f;
440439

441440
// TODO: catch CompileError and maybe other events too
@@ -451,8 +450,8 @@ function listener(event, execState, eventData, data) {
451450

452451
if (!script) return; // in Node 6.0, sometimes script is null, so skip it
453452

454-
assert(line >= 2);
455-
line -= 2; // to account for wrapUserscript() adding extra lines
453+
assert(line >= NUM_ADDED_LINES);
454+
line -= NUM_ADDED_LINES; // to account for wrapUserscript() adding extra lines
456455

457456
// if what we're currently executing isn't inside of userscript.js,
458457
// then PUNT, since we're probably in the first line of console.log()
@@ -526,6 +525,8 @@ function listener(event, execState, eventData, data) {
526525
}
527526
prevStack = curStack;
528527

528+
//log('======');
529+
//log('all_userscript_frames.length:', all_userscript_frames.length);
529530
var topFrame = all_userscript_frames[0];
530531
var topIsReturn = topFrame.isAtReturn();
531532
if (topIsReturn) {
@@ -603,47 +604,12 @@ function listener(event, execState, eventData, data) {
603604
curTraceEntry.event = logEventType;
604605
curTraceEntry.heap = getHeap();
605606

606-
// inspect only the top "global" frame to grab globals
607-
for (ii = 0; ii < topFrame.scopeCount(); ii++) {
608-
sc = topFrame.scope(ii);
609-
// According to https://code.google.com/p/v8-wiki/wiki/DebuggerProtocol
610-
//
611-
// 0: Global
612-
// 1: Local
613-
// 2: With
614-
// 3: Closure
615-
// 4: Catch
616-
scopeType = sc.details_.details_[0];
617-
if (scopeType === 0) { // Global -- to handle global variables
618-
scopeObj = sc.details_.details_[1];
619-
var globalScopePairs = _.pairs(scopeObj);
620-
for (jj = 0; jj < globalScopePairs.length; jj++) {
621-
var globalVarname = globalScopePairs[jj][0];
622-
var globalVal = globalScopePairs[jj][1];
623-
if (!_.has(IGNORE_GLOBAL_VARS, globalVarname)) {
624-
curTraceEntry.ordered_globals.push(globalVarname);
625-
assert(!_.has(curTraceEntry.globals, globalVarname));
626-
curTraceEntry.globals[globalVarname] = encodeObject(globalVal);
627-
}
628-
}
629-
}
630-
else if (scopeType === 4) { // Catch -- to handle global exception blocks
631-
scopeObj = sc.details_.details_[1];
632-
633-
var globalCatchScopePairs = _.pairs(scopeObj);
634-
for (jj = 0; jj < globalCatchScopePairs.length; jj++) {
635-
var globalCatchVarname = globalCatchScopePairs[jj][0];
636-
var globalCatchVal = globalCatchScopePairs[jj][1];
637-
curTraceEntry.ordered_globals.push(globalCatchVarname);
638-
assert(!_.has(curTraceEntry.globals, globalCatchVarname));
639-
curTraceEntry.globals[globalCatchVarname] = encodeObject(globalCatchVal);
640-
}
641-
}
642-
}
607+
var hasLocalBlock = false;
643608

644609
for (i = 0;
645-
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 */
646611
i++) {
612+
//log('all_userscript_frames[' + i + ']');
647613
var traceStackEntry = {};
648614

649615
f = all_userscript_frames[i];
@@ -739,20 +705,52 @@ function listener(event, execState, eventData, data) {
739705
}
740706
*/
741707

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+
742721
var nParentScopes = 1;
743-
for (ii = 0;
744-
ii < f.scopeCount();
745-
ii++) {
722+
// TODO: for some weird reason, it doesn't work when I iterate
723+
// forwards, strange
724+
for (ii = f.scopeCount() - 1;
725+
ii >= 0;
726+
ii--) {
746727
sc = f.scope(ii);
747728

748-
// According to https://code.google.com/p/v8-wiki/wiki/DebuggerProtocol
749-
//
750-
// 0: Global
751-
// 1: Local
752-
// 2: With
753-
// 3: Closure
754-
// 4: Catch
729+
/* From v8/src/debug/debug-scopes.h
730+
731+
enum ScopeType {
732+
ScopeTypeGlobal = 0,
733+
ScopeTypeLocal,
734+
ScopeTypeWith,
735+
ScopeTypeClosure,
736+
ScopeTypeCatch,
737+
ScopeTypeBlock,
738+
ScopeTypeScript,
739+
ScopeTypeModule
740+
};
741+
742+
0: Global
743+
1: Local
744+
2: With
745+
3: Closure
746+
4: Catch
747+
5: Block scope for ES6 let (only in Node v6)
748+
6: Script (only in Node v6)
749+
7: Module (only in Node v6)
750+
751+
*/
755752
scopeType = sc.details_.details_[0];
753+
//log(' scopeType:', scopeType, ii);
756754
var e;
757755
// DON'T grab globals again since it's redundant
758756
if (scopeType === 1 || scopeType === 4) { // Local or Catch (for exceptions)
@@ -761,6 +759,7 @@ function listener(event, execState, eventData, data) {
761759
scopeObj = sc.details_.details_[1];
762760
assert(_.isObject(scopeObj));
763761
var localScopePairs = _.pairs(scopeObj);
762+
//log('Local vars:', util.inspect(scopeObj));
764763
for (jj = 0; jj < localScopePairs.length; jj++) {
765764
e = localScopePairs[jj];
766765
traceStackEntry.ordered_varnames.push(e[0]);
@@ -792,10 +791,30 @@ function listener(event, execState, eventData, data) {
792791
}
793792

794793
nParentScopes++;
794+
} else if (scopeType === 6) { // Script
795+
// ignore since we'll extract the Script block data from the
796+
// toplevel scope (see below); it should all be pointing to
797+
// the same object anyhow
798+
} else if (scopeType === 5) { // block scope for ES6 let (?) only in Node v6
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_;
802+
scopeObj = sc.details_.details_[1];
803+
assert(_.isObject(scopeObj));
804+
//log('Local block:', scopeIdx, util.inspect(sc, {showHidden: true, depth: null}));
805+
hasLocalBlock = true;
806+
807+
var localScopePairs = _.pairs(scopeObj);
808+
for (jj = 0; jj < localScopePairs.length; jj++) {
809+
var mungedVarName = e[0] + ' (block ' + scopeIdx + ')';
810+
e = localScopePairs[jj];
811+
traceStackEntry.ordered_varnames.push(mungedVarName);
812+
assert(!_.has(traceStackEntry.encoded_locals, mungedVarName));
813+
traceStackEntry.encoded_locals[mungedVarName] = encodeObject(e[1]);
814+
}
795815
} else {
796816
assert(scopeType === 0 ||
797-
scopeType === 6 /* wtf?!? in Node v6.0.0, there's sometimes a
798-
scopeType of 6, which is mysterious, so skip it */);
817+
scopeType === 7);
799818
}
800819
}
801820

@@ -810,6 +829,107 @@ function listener(event, execState, eventData, data) {
810829
curTraceEntry.stack_to_render.unshift(traceStackEntry);
811830
}
812831

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+
843+
//log(' topFrame.scopeCount()', topFrame.scopeCount());
844+
// finally, inspect only the top-level "global" frame to grab globals
845+
// go backwards to make things a bit more human-readable
846+
for (ii = topFrame.scopeCount() - 1;
847+
ii >= 0;
848+
ii--) {
849+
sc = topFrame.scope(ii);
850+
851+
/* From v8/src/debug/debug-scopes.h
852+
853+
enum ScopeType {
854+
ScopeTypeGlobal = 0,
855+
ScopeTypeLocal,
856+
ScopeTypeWith,
857+
ScopeTypeClosure,
858+
ScopeTypeCatch,
859+
ScopeTypeBlock,
860+
ScopeTypeScript,
861+
ScopeTypeModule
862+
};
863+
864+
0: Global
865+
1: Local
866+
2: With
867+
3: Closure
868+
4: Catch
869+
5: Block scope for ES6 let (only in Node v6)
870+
6: Script (only in Node v6)
871+
7: Module (only in Node v6)
872+
873+
*/
874+
scopeType = sc.details_.details_[0];
875+
//log(' G scopeType:', scopeType);
876+
if (scopeType === 0 || scopeType === 6) {
877+
// 0: Global -- global variables declared with 'var' (not let/const)
878+
// 6: Script -- top-level globals declared with 'let' and 'const'
879+
// (i.e., not within a nested block)
880+
//
881+
// I think it's fine to handle both scopes here since variable
882+
// names should not collide. i.e., you can't declare a var and
883+
// let/const variable with the SAME NAME in the top-level global scope
884+
scopeObj = sc.details_.details_[1];
885+
var globalScopePairs = _.pairs(scopeObj);
886+
//log(scopeType, _.keys(scopeObj));
887+
for (jj = 0; jj < globalScopePairs.length; jj++) {
888+
var globalVarname = globalScopePairs[jj][0];
889+
var globalVal = globalScopePairs[jj][1];
890+
if (!_.has(IGNORE_GLOBAL_VARS, globalVarname)) {
891+
curTraceEntry.ordered_globals.push(globalVarname);
892+
assert(!_.has(curTraceEntry.globals, globalVarname));
893+
curTraceEntry.globals[globalVarname] = encodeObject(globalVal);
894+
}
895+
}
896+
} else if (scopeType === 4) { // Catch -- to handle global exception blocks
897+
scopeObj = sc.details_.details_[1];
898+
899+
var globalCatchScopePairs = _.pairs(scopeObj);
900+
for (jj = 0; jj < globalCatchScopePairs.length; jj++) {
901+
var globalCatchVarname = globalCatchScopePairs[jj][0];
902+
var globalCatchVal = globalCatchScopePairs[jj][1];
903+
curTraceEntry.ordered_globals.push(globalCatchVarname);
904+
assert(!_.has(curTraceEntry.globals, globalCatchVarname));
905+
curTraceEntry.globals[globalCatchVarname] = encodeObject(globalCatchVal);
906+
}
907+
} else if (scopeType === 5 && !hasLocalBlock) { // Block -- handle global ES6 let-style blocks
908+
// we do a hasLocalBlock check since for some WEIRD WEIRD WEIRD
909+
// reason, if there's a block scope in a function's frame, then
910+
// that block scope gets DUPLICATED in the global frame, which
911+
// ends up being redundant and super confusing. hasLocalBlock is
912+
// a hack to elide that problem.
913+
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_;
917+
scopeObj = sc.details_.details_[1];
918+
assert(_.isObject(scopeObj));
919+
//log('Global block:', util.inspect(sc, {showHidden: true, depth: null}));
920+
var globalScopePairs = _.pairs(scopeObj);
921+
//log(scopeType, _.keys(scopeObj));
922+
for (jj = 0; jj < globalScopePairs.length; jj++) {
923+
var globalVarname = globalScopePairs[jj][0] + ' (block ' + scopeIdx + ')';
924+
var globalVal = globalScopePairs[jj][1];
925+
if (!_.has(IGNORE_GLOBAL_VARS, globalVarname)) {
926+
curTraceEntry.ordered_globals.push(globalVarname);
927+
assert(!_.has(curTraceEntry.globals, globalVarname));
928+
curTraceEntry.globals[globalVarname] = encodeObject(globalVal);
929+
}
930+
}
931+
}
932+
}
813933

814934
// check whether the top frame is currently returning, and if so,
815935
// update frameIdCalls. it's VERY IMPORTANT to do this update at
@@ -964,35 +1084,19 @@ catch (e) {
9641084
// likely a compile error since nothing executed yet; trace is empty
9651085

9661086
var originalErrorMsg = e.toString();
1087+
var stackTrace = e.stack;
1088+
1089+
// grab the first line
1090+
var firstLine = stackTrace.split('\n')[0];
1091+
var lineNo = Number(_.last(firstLine.split(':')));
9671092

9681093
var errorTraceEntry = {};
9691094
errorTraceEntry.event = 'uncaught_exception';
970-
971-
// OK now try to parse the user's original code using esprima, not
972-
// node's compiler/parser, since esprima gives precise line/column
973-
// numbers for errors while node doesn't.
974-
//
975-
// the main caveat here is that the error messages might be slightly
976-
// different, and also out of sync, since we're using a different
977-
// parser. so beware!!!
978-
979-
var hasEsprimaErr = false;
980-
981-
try {
982-
esprima.parse(cod);
983-
}
984-
catch (esprimaErr) {
985-
hasEsprimaErr = true;
986-
errorTraceEntry.exception_msg = esprimaErr.description;
987-
errorTraceEntry.line = esprimaErr.lineNumber;
988-
errorTraceEntry.col = esprimaErr.column;
989-
}
990-
991-
if (!hasEsprimaErr) {
992-
// crap, esprima didn't find a parse error, so just go with
993-
// originalErrorMsg and throw up our hands since we can't pinpoint
994-
// the location of the error.
995-
errorTraceEntry.exception_msg = originalErrorMsg + "\n(sorry, tool can't find the line number)";
1095+
errorTraceEntry.exception_msg = originalErrorMsg;
1096+
if (isNaN(lineNo)) {
1097+
errorTraceEntry.exception_msg = originalErrorMsg + "\n(sorry, we can't find the line number)";
1098+
} else {
1099+
errorTraceEntry.line = lineNo - NUM_ADDED_LINES; // adjust to account for code that we injected -- subtle!
9961100
}
9971101

9981102
curTrace.push(errorTraceEntry);

‎v4-cokapi/backends/javascript/tests/caught-exception.golden‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@
3131
{ stdout: '',
3232
func_name: '',
3333
stack_to_render: [],
34-
globals: { e: [ 'REF', 1 ], x: 5 },
35-
ordered_globals: [ 'e', 'x' ],
34+
globals: { x: 5, e: [ 'REF', 1 ] },
35+
ordered_globals: [ 'x', 'e' ],
3636
line: 6,
3737
col: 2,
3838
event: 'step_line',
3939
heap: { '1': [ 'INSTANCE_PPRINT', 'object', 'Error: oh crapo' ] } },
4040
{ stdout: 'hello\n',
4141
func_name: '',
4242
stack_to_render: [],
43-
globals: { e: [ 'REF', 1 ], x: 5 },
44-
ordered_globals: [ 'e', 'x' ],
43+
globals: { x: 5, e: [ 'REF', 1 ] },
44+
ordered_globals: [ 'x', 'e' ],
4545
line: 7,
4646
col: 2,
4747
event: 'step_line',

0 commit comments

Comments
 (0)