Skip to content

Commit 8765355

Browse files
committed
GIANT refactoring!!!
1 parent 2b7276f commit 8765355

3 files changed

Lines changed: 171 additions & 196 deletions

File tree

‎v3/js/iframe-embed.js‎

Lines changed: 37 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3737
var myVisualizer = null; // singleton ExecutionVisualizer instance
3838

3939

40+
function NOP() {};
41+
42+
4043
$(document).ready(function() {
4144
var queryStrOptions = getQueryStringOptions();
4245

@@ -49,16 +52,9 @@ $(document).ready(function() {
4952
var showOnlyOutputsBool = (queryStrOptions.showOnlyOutputs == 'true');
5053
var cumModeBool = (queryStrOptions.cumulativeState == 'true');
5154

52-
// set up all options in a JS object
53-
var options = {cumulative_mode: cumModeBool,
54-
heap_primitives: heapPrimitivesBool,
55-
show_only_outputs: showOnlyOutputsBool,
56-
py_crazy_mode: (pyState == '2crazy')};
57-
58-
59-
var preseededCurInstr = queryStrOptions.preseededCurInstr;
60-
if (!preseededCurInstr) {
61-
preseededCurInstr = 0;
55+
var startingInstruction = queryStrOptions.preseededCurInstr;
56+
if (!startingInstruction) {
57+
startingInstruction = 0;
6258
}
6359

6460
var backend_script = null;
@@ -68,10 +64,8 @@ $(document).ready(function() {
6864
else if (pyState == '3') {
6965
backend_script = python3_backend_script;
7066
}
71-
72-
if (!backend_script) {
73-
alert('Error: This server is not configured to run Python ' + $('#pythonVersionSelector').val());
74-
return;
67+
else if (pyState == '2crazy') {
68+
backend_script = python2crazy_backend_script;
7569
}
7670

7771

@@ -98,92 +92,48 @@ $(document).ready(function() {
9892
}
9993

10094

101-
function executeCode(forceStartingInstr) {
102-
$.get(backend_script,
103-
{user_script : preseededCode,
104-
raw_input_json: rawInputLst.length > 0 ? JSON.stringify(rawInputLst) : '',
105-
options_json: JSON.stringify(options)},
106-
function(dataFromBackend) {
107-
var trace = dataFromBackend.trace;
108-
109-
// don't enter visualize mode if there are killer errors:
110-
if (!trace ||
111-
(trace.length == 0) ||
112-
(trace[trace.length - 1].event == 'uncaught_exception')) {
113-
114-
if (trace.length == 1) {
115-
alert(trace[0].exception_msg);
116-
}
117-
else if (trace[trace.length - 1].exception_msg) {
118-
alert(trace[trace.length - 1].exception_msg);
119-
}
120-
else {
121-
alert("Whoa, unknown error! Reload to try again, or report a bug to philip@pgbovine.net");
122-
}
123-
}
124-
else {
125-
var startingInstruction = 0;
126-
127-
// only do this at most ONCE, and then clear out preseededCurInstr
128-
if (preseededCurInstr && preseededCurInstr < trace.length) { // NOP anyways if preseededCurInstr is 0
129-
startingInstruction = preseededCurInstr;
130-
}
131-
132-
// forceStartingInstr overrides everything else
133-
if (forceStartingInstr !== undefined) {
134-
startingInstruction = forceStartingInstr;
135-
}
95+
// set up all options in a JS object
96+
var backendOptionsObj = {cumulative_mode: cumModeBool,
97+
heap_primitives: heapPrimitivesBool,
98+
show_only_outputs: showOnlyOutputsBool,
99+
py_crazy_mode: (pyState == '2crazy')};
100+
101+
var frontendOptionsObj = {startingInstruction: startingInstruction,
102+
embeddedMode: true,
103+
verticalStack: verticalStackBool,
104+
disableHeapNesting: heapPrimitivesBool,
105+
drawParentPointers: drawParentPointerBool,
106+
textualMemoryLabels: textRefsBool,
107+
showOnlyOutputs: showOnlyOutputsBool,
108+
executeCodeWithRawInputFunc: executeCodeWithRawInput,
109+
updateOutputCallback: (resizeContainer ? resizeContainerNow : null)
110+
111+
// undocumented experimental modes:
112+
pyCrazyMode: (pyState == '2crazy'),
113+
highlightLines: typeof $.bbq.getState("highlightLines") !== "undefined",
114+
}
136115

137-
myVisualizer = new ExecutionVisualizer('vizDiv',
138-
dataFromBackend,
139-
{startingInstruction: preseededCurInstr,
140-
embeddedMode: true,
141-
verticalStack: verticalStackBool,
142-
disableHeapNesting: heapPrimitivesBool,
143-
drawParentPointers: drawParentPointerBool,
144-
textualMemoryLabels: textRefsBool,
145-
showOnlyOutputs: showOnlyOutputsBool,
146-
highlightLines: typeof $.bbq.getState("highlightLines") !== "undefined",
147-
executeCodeWithRawInputFunc: executeCodeWithRawInput,
148-
pyCrazyMode: (pyState == '2crazy'),
149-
updateOutputCallback: (resizeContainer ? resizeContainerNow : null)
150-
});
151-
152-
// set keyboard bindings
153-
// VERY IMPORTANT to clear and reset this every time or
154-
// else the handlers might be bound multiple times
155-
$(document).unbind('keydown');
156-
$(document).keydown(function(k) {
157-
if (k.keyCode == 37) { // left arrow
158-
if (myVisualizer.stepBack()) {
159-
k.preventDefault(); // don't horizontally scroll the display
160-
}
161-
}
162-
else if (k.keyCode == 39) { // right arrow
163-
if (myVisualizer.stepForward()) {
164-
k.preventDefault(); // don't horizontally scroll the display
165-
}
166-
}
167-
});
168-
}
169-
},
170-
"json");
116+
function executeCode(forceStartingInstr) {
117+
if (forceStartingInstr) {
118+
frontendOptionsObj.startingInstruction = forceStartingInstr;
119+
}
120+
executePythonCode(preseededCode,
121+
backend_script, backendOptionsObj,
122+
frontendOptionsObj,
123+
'vizDiv',
124+
NOP, NOP);
171125
}
172126

173127

174128
function executeCodeFromScratch() {
175129
// reset these globals
176130
rawInputLst = [];
177-
178131
executeCode();
179132
}
180133

181134
function executeCodeWithRawInput(rawInputStr, curInstr) {
182-
enterDisplayNoFrillsMode();
183-
184135
// set some globals
185136
rawInputLst.push(rawInputStr);
186-
187137
executeCode(curInstr);
188138
}
189139

‎v3/js/opt-frontend-common.js‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,68 @@ function getQueryStringOptions() {
7777
showOnlyOutputs: showOnlyOutputs,
7878
cumulativeState: cumulativeState};
7979
}
80+
81+
82+
function executePythonCode(pythonSourceCode,
83+
backendScript, backendOptionsObj,
84+
frontendOptionsObj,
85+
outputDiv,
86+
handleSuccessFunc, handleUncaughtExceptionFunc) {
87+
if (!backendScript) {
88+
alert('Server configuration error: No backend script');
89+
return;
90+
}
91+
92+
$.get(backendScript,
93+
{user_script : pythonSourceCode,
94+
raw_input_json: rawInputLst.length > 0 ? JSON.stringify(rawInputLst) : '',
95+
options_json: JSON.stringify(backendOptionsObj)},
96+
function(dataFromBackend) {
97+
var trace = dataFromBackend.trace;
98+
99+
// don't enter visualize mode if there are killer errors:
100+
if (!trace ||
101+
(trace.length == 0) ||
102+
(trace[trace.length - 1].event == 'uncaught_exception')) {
103+
104+
handleUncaughtExceptionFunc(trace);
105+
106+
if (trace.length == 1) {
107+
alert(trace[0].exception_msg);
108+
}
109+
else if (trace[trace.length - 1].exception_msg) {
110+
alert(trace[trace.length - 1].exception_msg);
111+
}
112+
else {
113+
alert("Unknown error. Reload to try again," +
114+
"or report a bug to philip@pgbovine.net\n\n" +
115+
"(Click the 'Generate URL' button to include a " +
116+
"unique URL in your email bug report.)");
117+
}
118+
}
119+
else {
120+
myVisualizer = new ExecutionVisualizer(outputDiv, dataFromBackend, frontendOptionsObj);
121+
122+
123+
// set keyboard bindings
124+
// VERY IMPORTANT to clear and reset this every time or
125+
// else the handlers might be bound multiple times
126+
$(document).unbind('keydown');
127+
$(document).keydown(function(k) {
128+
if (k.keyCode == 37) { // left arrow
129+
if (myVisualizer.stepBack()) {
130+
k.preventDefault(); // don't horizontally scroll the display
131+
}
132+
}
133+
else if (k.keyCode == 39) { // right arrow
134+
if (myVisualizer.stepForward()) {
135+
k.preventDefault(); // don't horizontally scroll the display
136+
}
137+
}
138+
});
139+
140+
handleSuccessFunc();
141+
}
142+
},
143+
"json");
144+
}

0 commit comments

Comments
 (0)