Skip to content

Commit f92f383

Browse files
committed
more refactoring for correctness sake
1 parent 41ec46d commit f92f383

2 files changed

Lines changed: 137 additions & 138 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,34 @@ function setToggleOptions(dat) {
119119
}
120120

121121

122+
// get the ENTIRE current state of the app
123+
function getAppState() {
124+
return {code: pyInputCodeMirror.getValue(),
125+
mode: appMode,
126+
cumulative: $('#cumulativeModeSelector').val(),
127+
heapPrimitives: $('#heapPrimitivesSelector').val(),
128+
drawParentPointers: $('#drawParentPointerSelector').val(),
129+
textReferences: $('#textualMemoryLabelsSelector').val(),
130+
showOnlyOutputs: $('#showOnlyOutputsSelector').val(),
131+
py: $('#pythonVersionSelector').val(),
132+
curInstr: myVisualizer ? myVisualizer.curInstr : undefined,
133+
// the app state that led to the CURRENT visualization
134+
visualizedAppState: visualizedAppState};
135+
}
136+
137+
// sets the proper GUI features to match the given appState object
138+
function setVisibleAppState(appState) {
139+
setCodeMirrorVal(appState.code);
140+
141+
$('#cumulativeModeSelector').val(appState.cumulative);
142+
$('#heapPrimitivesSelector').val(appState.heapPrimitives);
143+
$('#drawParentPointerSelector').val(appState.drawParentPointers);
144+
$('#textualMemoryLabelsSelector').val(appState.textReferences);
145+
$('#showOnlyOutputsSelector').val(appState.showOnlyOutputs);
146+
$('#pythonVersionSelector').val(appState.py);
147+
}
148+
149+
122150
// update the app display based on current state of the appMode global
123151
// TODO: refactor all frontend clients to call this unified function
124152
function updateAppDisplay() {

‎v3/js/opt-frontend.js‎

Lines changed: 109 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -90,36 +90,6 @@ var try_hook = function(hook_name, args) {
9090
}
9191

9292

93-
// get the ENTIRE current state of the app
94-
function getAppState() {
95-
return {code: pyInputCodeMirror.getValue(),
96-
mode: appMode,
97-
cumulative: $('#cumulativeModeSelector').val(),
98-
heapPrimitives: $('#heapPrimitivesSelector').val(),
99-
drawParentPointers: $('#drawParentPointerSelector').val(),
100-
textReferences: $('#textualMemoryLabelsSelector').val(),
101-
showOnlyOutputs: $('#showOnlyOutputsSelector').val(),
102-
py: $('#pythonVersionSelector').val(),
103-
curInstr: myVisualizer ? myVisualizer.curInstr : undefined,
104-
// the app state that led to the CURRENT visualization
105-
visualizedAppState: visualizedAppState};
106-
}
107-
108-
// sets the proper GUI features to match the given appState object
109-
function setVisibleAppState(appState) {
110-
setCodeMirrorVal(appState.code);
111-
112-
$('#cumulativeModeSelector').val(appState.cumulative);
113-
$('#heapPrimitivesSelector').val(appState.heapPrimitives);
114-
$('#drawParentPointerSelector').val(appState.drawParentPointers);
115-
$('#textualMemoryLabelsSelector').val(appState.textReferences);
116-
$('#showOnlyOutputsSelector').val(appState.showOnlyOutputs);
117-
$('#pythonVersionSelector').val(appState.py);
118-
119-
// TODO: what to do about mode and curInstr?
120-
}
121-
122-
12393
// get this app ready for TogetherJS
12494
function initTogetherJS() {
12595
$("#surveyHeader").remove(); // kill this to save space
@@ -302,6 +272,115 @@ function initTogetherJS() {
302272
}
303273

304274

275+
function executeCode(forceStartingInstr) {
276+
var backend_script = null;
277+
if ($('#pythonVersionSelector').val() == '2') {
278+
backend_script = python2_backend_script;
279+
}
280+
else if ($('#pythonVersionSelector').val() == '3') {
281+
backend_script = python3_backend_script;
282+
}
283+
// experimental KRAZY MODE!!!
284+
else if ($('#pythonVersionSelector').val() == '2crazy') {
285+
backend_script = python2crazy_backend_script;
286+
}
287+
288+
$('#executeBtn').html("Please wait ... processing your code");
289+
$('#executeBtn').attr('disabled', true);
290+
$("#pyOutputPane").hide();
291+
$("#embedLinkDiv").hide();
292+
293+
var backendOptionsObj = {cumulative_mode: ($('#cumulativeModeSelector').val() == 'true'),
294+
heap_primitives: ($('#heapPrimitivesSelector').val() == 'true'),
295+
show_only_outputs: ($('#showOnlyOutputsSelector').val() == 'true'),
296+
py_crazy_mode: ($('#pythonVersionSelector').val() == '2crazy'),
297+
origin: 'opt-frontend.js'};
298+
299+
var surveyObj = getSurveyObject();
300+
if (surveyObj) {
301+
backendOptionsObj.survey = surveyObj;
302+
}
303+
304+
var startingInstruction = 0;
305+
306+
// only do this at most ONCE, and then clear out preseededCurInstr
307+
// NOP anyways if preseededCurInstr is 0
308+
if (preseededCurInstr) {
309+
startingInstruction = preseededCurInstr;
310+
preseededCurInstr = null;
311+
}
312+
313+
// forceStartingInstr overrides everything else
314+
if (forceStartingInstr !== undefined) {
315+
startingInstruction = forceStartingInstr;
316+
}
317+
318+
var frontendOptionsObj = {startingInstruction: startingInstruction,
319+
// tricky: selector 'true' and 'false' values are strings!
320+
disableHeapNesting: ($('#heapPrimitivesSelector').val() == 'true'),
321+
drawParentPointers: ($('#drawParentPointerSelector').val() == 'true'),
322+
textualMemoryLabels: ($('#textualMemoryLabelsSelector').val() == 'true'),
323+
showOnlyOutputs: ($('#showOnlyOutputsSelector').val() == 'true'),
324+
executeCodeWithRawInputFunc: executeCodeWithRawInput,
325+
// if TogetherJS is enabled, always use the same
326+
// visualizer ID for all ExecutionVisualizer
327+
// objects, so that they can sync properly
328+
visualizerIdOverride: enableTogetherJS ? '1' : undefined,
329+
updateOutputCallback: function() {$('#urlOutput,#embedCodeOutput').val('');},
330+
331+
// undocumented experimental modes:
332+
pyCrazyMode: ($('#pythonVersionSelector').val() == '2crazy'),
333+
holisticMode: ($('#cumulativeModeSelector').val() == 'holistic')
334+
}
335+
336+
function handleUncaughtExceptionFunc(trace) {
337+
if (trace.length == 1 && trace[0].line) {
338+
var errorLineNo = trace[0].line - 1; /* CodeMirror lines are zero-indexed */
339+
if (errorLineNo !== undefined && errorLineNo != NaN) {
340+
// highlight the faulting line in pyInputCodeMirror
341+
pyInputCodeMirror.focus();
342+
pyInputCodeMirror.setCursor(errorLineNo, 0);
343+
pyInputCodeMirror.setLineClass(errorLineNo, null, 'errorLine');
344+
345+
pyInputCodeMirror.setOption('onChange', function() {
346+
pyInputCodeMirror.setLineClass(errorLineNo, null, null); // reset line back to normal
347+
pyInputCodeMirror.setOption('onChange', null); // cancel
348+
});
349+
}
350+
351+
$('#executeBtn').html("Visualize Execution");
352+
$('#executeBtn').attr('disabled', false);
353+
}
354+
}
355+
356+
executePythonCode(pyInputCodeMirror.getValue(),
357+
backend_script, backendOptionsObj,
358+
frontendOptionsObj,
359+
'pyOutputPane',
360+
enterDisplayMode, handleUncaughtExceptionFunc);
361+
}
362+
363+
function executeCodeFromScratch() {
364+
// don't execute empty string:
365+
if ($.trim(pyInputCodeMirror.getValue()) == '') {
366+
alert('Type in some code to visualize.');
367+
return;
368+
}
369+
370+
// reset these globals
371+
rawInputLst = [];
372+
executeCode();
373+
}
374+
375+
function executeCodeWithRawInput(rawInputStr, curInstr) {
376+
enterDisplayNoFrillsMode();
377+
378+
// set some globals
379+
rawInputLst.push(rawInputStr);
380+
executeCode(curInstr);
381+
}
382+
383+
305384
$(document).ready(function() {
306385
setSurveyHTML();
307386

@@ -342,114 +421,6 @@ $(document).ready(function() {
342421
});
343422

344423

345-
function executeCode(forceStartingInstr) {
346-
var backend_script = null;
347-
if ($('#pythonVersionSelector').val() == '2') {
348-
backend_script = python2_backend_script;
349-
}
350-
else if ($('#pythonVersionSelector').val() == '3') {
351-
backend_script = python3_backend_script;
352-
}
353-
// experimental KRAZY MODE!!!
354-
else if ($('#pythonVersionSelector').val() == '2crazy') {
355-
backend_script = python2crazy_backend_script;
356-
}
357-
358-
$('#executeBtn').html("Please wait ... processing your code");
359-
$('#executeBtn').attr('disabled', true);
360-
$("#pyOutputPane").hide();
361-
$("#embedLinkDiv").hide();
362-
363-
var backendOptionsObj = {cumulative_mode: ($('#cumulativeModeSelector').val() == 'true'),
364-
heap_primitives: ($('#heapPrimitivesSelector').val() == 'true'),
365-
show_only_outputs: ($('#showOnlyOutputsSelector').val() == 'true'),
366-
py_crazy_mode: ($('#pythonVersionSelector').val() == '2crazy'),
367-
origin: 'opt-frontend.js'};
368-
369-
var surveyObj = getSurveyObject();
370-
if (surveyObj) {
371-
backendOptionsObj.survey = surveyObj;
372-
}
373-
374-
var startingInstruction = 0;
375-
376-
// only do this at most ONCE, and then clear out preseededCurInstr
377-
// NOP anyways if preseededCurInstr is 0
378-
if (preseededCurInstr) {
379-
startingInstruction = preseededCurInstr;
380-
preseededCurInstr = null;
381-
}
382-
383-
// forceStartingInstr overrides everything else
384-
if (forceStartingInstr !== undefined) {
385-
startingInstruction = forceStartingInstr;
386-
}
387-
388-
var frontendOptionsObj = {startingInstruction: startingInstruction,
389-
// tricky: selector 'true' and 'false' values are strings!
390-
disableHeapNesting: ($('#heapPrimitivesSelector').val() == 'true'),
391-
drawParentPointers: ($('#drawParentPointerSelector').val() == 'true'),
392-
textualMemoryLabels: ($('#textualMemoryLabelsSelector').val() == 'true'),
393-
showOnlyOutputs: ($('#showOnlyOutputsSelector').val() == 'true'),
394-
executeCodeWithRawInputFunc: executeCodeWithRawInput,
395-
// if TogetherJS is enabled, always use the same
396-
// visualizer ID for all ExecutionVisualizer
397-
// objects, so that they can sync properly
398-
visualizerIdOverride: enableTogetherJS ? '1' : undefined,
399-
updateOutputCallback: function() {$('#urlOutput,#embedCodeOutput').val('');},
400-
401-
// undocumented experimental modes:
402-
pyCrazyMode: ($('#pythonVersionSelector').val() == '2crazy'),
403-
holisticMode: ($('#cumulativeModeSelector').val() == 'holistic')
404-
}
405-
406-
function handleUncaughtExceptionFunc(trace) {
407-
if (trace.length == 1 && trace[0].line) {
408-
var errorLineNo = trace[0].line - 1; /* CodeMirror lines are zero-indexed */
409-
if (errorLineNo !== undefined && errorLineNo != NaN) {
410-
// highlight the faulting line in pyInputCodeMirror
411-
pyInputCodeMirror.focus();
412-
pyInputCodeMirror.setCursor(errorLineNo, 0);
413-
pyInputCodeMirror.setLineClass(errorLineNo, null, 'errorLine');
414-
415-
pyInputCodeMirror.setOption('onChange', function() {
416-
pyInputCodeMirror.setLineClass(errorLineNo, null, null); // reset line back to normal
417-
pyInputCodeMirror.setOption('onChange', null); // cancel
418-
});
419-
}
420-
421-
$('#executeBtn').html("Visualize Execution");
422-
$('#executeBtn').attr('disabled', false);
423-
}
424-
}
425-
426-
executePythonCode(pyInputCodeMirror.getValue(),
427-
backend_script, backendOptionsObj,
428-
frontendOptionsObj,
429-
'pyOutputPane',
430-
enterDisplayMode, handleUncaughtExceptionFunc);
431-
}
432-
433-
function executeCodeFromScratch() {
434-
// don't execute empty string:
435-
if ($.trim(pyInputCodeMirror.getValue()) == '') {
436-
alert('Type in some code to visualize.');
437-
return;
438-
}
439-
440-
// reset these globals
441-
rawInputLst = [];
442-
executeCode();
443-
}
444-
445-
function executeCodeWithRawInput(rawInputStr, curInstr) {
446-
enterDisplayNoFrillsMode();
447-
448-
// set some globals
449-
rawInputLst.push(rawInputStr);
450-
executeCode(curInstr);
451-
}
452-
453424
$("#executeBtn").attr('disabled', false);
454425
$("#executeBtn").click(executeCodeFromScratch);
455426

0 commit comments

Comments
 (0)