forked from daveagp/java_visualize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopt-frontend-common.js
More file actions
143 lines (121 loc) · 5.61 KB
/
opt-frontend-common.js
File metadata and controls
143 lines (121 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Online Python Tutor
https://github.com/pgbovine/OnlinePythonTutor/
Copyright (C) 2010-2013 Philip J. Guo (philip@pgbovine.net)
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// backend scripts to execute (Python 2 and 3 variants, if available)
// make two copies of ../web_exec.py and give them the following names,
// then change the first line (starting with #!) to the proper version
// of the Python interpreter (i.e., Python 2 or Python 3).
// Note that your hosting provider might have stringent rules for what
// kind of scripts are allowed to execute. For instance, my provider
// (Webfaction) seems to let scripts execute only if permissions are
// something like:
// -rwxr-xr-x 1 pgbovine pgbovine 2.5K Jul 5 22:46 web_exec_py2.py*
// (most notably, only the owner of the file should have write
// permissions)
//var python2_backend_script = 'web_exec_py2.py';
//var python3_backend_script = 'web_exec_py3.py';
var rawInputLst = []; // a list of strings inputted by the user in response to raw_input or mouse_input events
function getQueryStringOptions() {
// note that any of these can be 'undefined'
var preseededCode = $.bbq.getState('code');
var preseededCurInstr = Number($.bbq.getState('curInstr'));
var pyState = $.bbq.getState('py');
var verticalStack = $.bbq.getState('verticalStack');
var heapPrimitives = $.bbq.getState('heapPrimitives');
var drawParentPointers = $.bbq.getState('drawParentPointers');
var disableHeapNesting = true;
var textRefs = $.bbq.getState('textReferences');
var showOnlyOutputs = $.bbq.getState('showOnlyOutputs');
var cumulativeState = $.bbq.getState('cumulative');
var raw_input = $.bbq.getState('raw_input');
return {preseededCode: preseededCode,
preseededCurInstr: preseededCurInstr,
pyState: pyState,
verticalStack: verticalStack,
heapPrimitives: heapPrimitives,
drawParentPointers: drawParentPointers,
disableHeapNesting: disableHeapNesting,
textRefs: textRefs,
showOnlyOutputs: showOnlyOutputs,
cumulativeState: cumulativeState,
raw_input: raw_input};
}
function executePythonCode(pythonSourceCode,
backendScript, backendOptionsObj,
frontendOptionsObj,
outputDiv,
handleSuccessFunc, handleUncaughtExceptionFunc) {
if (!backendScript) {
alert('Server configuration error: No backend script');
return;
}
$.get(backendScript,
{data:$.bbq.getState('data')},
/*
raw_input_json: rawInputLst.length > 0 ? JSON.stringify(rawInputLst) : '',
*/
function(dataFromBackend) {
var trace = dataFromBackend.trace;
// don't enter visualize mode if there are killer errors:
if (!trace ||
(trace.length == 0) ||
(trace[trace.length - 1].event == 'uncaught_exception')) {
handleUncaughtExceptionFunc(trace);
if (trace.length == 1) {
alert(trace[0].exception_msg);
}
else if (trace[trace.length - 1].exception_msg) {
alert(trace[trace.length - 1].exception_msg);
}
else {
alert("Unknown error. Reload to try again," +
"or report a bug to philip@pgbovine.net\n\n" +
"(Click the 'Generate URL' button to include a " +
"unique URL in your email bug report.)");
}
}
else {
// fail-soft to prevent running off of the end of trace
if (frontendOptionsObj.startingInstruction >= trace.length) {
frontendOptionsObj.startingInstruction = 0;
}
myVisualizer = new ExecutionVisualizer(outputDiv, dataFromBackend, frontendOptionsObj);
// set keyboard bindings
// VERY IMPORTANT to clear and reset this every time or
// else the handlers might be bound multiple times
$(document).unbind('keydown');
$(document).keydown(function(k) {
if (k.keyCode == 37) { // left arrow
if (myVisualizer.stepBack()) {
k.preventDefault(); // don't horizontally scroll the display
}
}
else if (k.keyCode == 39) { // right arrow
if (myVisualizer.stepForward()) {
k.preventDefault(); // don't horizontally scroll the display
}
}
});
handleSuccessFunc();
}
},
"json");
}