Skip to content

Commit 1c94497

Browse files
committed
started adding support for raw_input
1 parent 01d6a81 commit 1c94497

1 file changed

Lines changed: 133 additions & 96 deletions

File tree

‎v3/js/opt-frontend.js‎

Lines changed: 133 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3838
var python2_backend_script = 'exec';
3939
var python3_backend_script = null;
4040

41-
var appMode = 'edit'; // 'edit' or 'display'
41+
var appMode = 'edit'; // 'edit', 'display', or 'display_no_frills'
4242

4343
var preseededCode = null; // if you passed in a 'code=<code string>' in the URL, then set this var
4444
var preseededCurInstr = null; // if you passed in a 'curInstr=<number>' in the URL, then set this var
4545

46+
var rawInputLst = []; // a list of strings inputted by the user in calls to prompt()
4647

4748
var myVisualizer = null; // singleton ExecutionVisualizer instance
4849

@@ -52,6 +53,9 @@ function enterEditMode() {
5253
$.bbq.pushState({ mode: 'edit' }, 2 /* completely override other hash strings to keep URL clean */);
5354
}
5455

56+
function enterDisplayNoFrillsMode() {
57+
$.bbq.pushState({ mode: 'display_no_frills' }, 2 /* completely override other hash strings to keep URL clean */);
58+
}
5559

5660
var pyInputCodeMirror; // CodeMirror object that contains the input text
5761

@@ -116,6 +120,11 @@ $(document).ready(function() {
116120
enterEditMode();
117121
});
118122
}
123+
else if (appMode == 'display_no_frills') {
124+
$("#pyInputPane").hide();
125+
$("#pyOutputPane").show();
126+
$("#embedLinkDiv").show();
127+
}
119128
else {
120129
assert(false);
121130
}
@@ -124,119 +133,142 @@ $(document).ready(function() {
124133
});
125134

126135

127-
$("#executeBtn").attr('disabled', false);
128-
$("#executeBtn").click(function() {
136+
function executeCode(forceStartingInstr) {
137+
var backend_script = null;
138+
if ($('#pythonVersionSelector').val() == '2') {
139+
backend_script = python2_backend_script;
140+
}
141+
else if ($('#pythonVersionSelector').val() == '3') {
142+
backend_script = python3_backend_script;
143+
}
129144

130-
var backend_script = null;
131-
if ($('#pythonVersionSelector').val() == '2') {
132-
backend_script = python2_backend_script;
133-
}
134-
else if ($('#pythonVersionSelector').val() == '3') {
135-
backend_script = python3_backend_script;
136-
}
145+
if (!backend_script) {
146+
alert('Error: This server is not configured to run Python ' + $('#pythonVersionSelector').val());
147+
return;
148+
}
149+
150+
$('#executeBtn').html("Please wait ... processing your code");
151+
$('#executeBtn').attr('disabled', true);
152+
$("#pyOutputPane").hide();
153+
$("#embedLinkDiv").hide();
137154

138-
if (!backend_script) {
139-
alert('Error: This server is not configured to run Python ' + $('#pythonVersionSelector').val());
140-
return;
141-
}
142155

143-
$('#executeBtn').html("Please wait ... processing your code");
144-
$('#executeBtn').attr('disabled', true);
145-
$("#pyOutputPane").hide();
146-
$("#embedLinkDiv").hide();
147-
148-
149-
$.get(backend_script,
150-
{user_script : pyInputCodeMirror.getValue(),
151-
cumulative_mode: $('#cumulativeModeSelector').val(),
152-
heap_primitives: $('#heapPrimitivesSelector').val()},
153-
function(dataFromBackend) {
154-
var trace = dataFromBackend.trace;
155-
156-
// don't enter visualize mode if there are killer errors:
157-
if (!trace ||
158-
(trace.length == 0) ||
159-
(trace[trace.length - 1].event == 'uncaught_exception')) {
160-
161-
if (trace.length == 1) {
162-
var errorLineNo = trace[0].line - 1; /* CodeMirror lines are zero-indexed */
163-
if (errorLineNo !== undefined) {
164-
// highlight the faulting line in pyInputCodeMirror
165-
pyInputCodeMirror.focus();
166-
pyInputCodeMirror.setCursor(errorLineNo, 0);
167-
pyInputCodeMirror.setLineClass(errorLineNo, null, 'errorLine');
168-
169-
pyInputCodeMirror.setOption('onChange', function() {
170-
pyInputCodeMirror.setLineClass(errorLineNo, null, null); // reset line back to normal
171-
pyInputCodeMirror.setOption('onChange', null); // cancel
172-
});
156+
$.get(backend_script,
157+
{user_script : pyInputCodeMirror.getValue(),
158+
raw_input_json: rawInputLst.length > 0 ? JSON.stringify(rawInputLst) : '',
159+
cumulative_mode: $('#cumulativeModeSelector').val(),
160+
heap_primitives: $('#heapPrimitivesSelector').val()},
161+
function(dataFromBackend) {
162+
var trace = dataFromBackend.trace;
163+
164+
// don't enter visualize mode if there are killer errors:
165+
if (!trace ||
166+
(trace.length == 0) ||
167+
(trace[trace.length - 1].event == 'uncaught_exception')) {
168+
169+
if (trace.length == 1) {
170+
var errorLineNo = trace[0].line - 1; /* CodeMirror lines are zero-indexed */
171+
if (errorLineNo !== undefined) {
172+
// highlight the faulting line in pyInputCodeMirror
173+
pyInputCodeMirror.focus();
174+
pyInputCodeMirror.setCursor(errorLineNo, 0);
175+
pyInputCodeMirror.setLineClass(errorLineNo, null, 'errorLine');
176+
177+
pyInputCodeMirror.setOption('onChange', function() {
178+
pyInputCodeMirror.setLineClass(errorLineNo, null, null); // reset line back to normal
179+
pyInputCodeMirror.setOption('onChange', null); // cancel
180+
});
181+
}
182+
183+
alert(trace[0].exception_msg);
184+
}
185+
else if (trace[trace.length - 1].exception_msg) {
186+
alert(trace[trace.length - 1].exception_msg);
187+
}
188+
else {
189+
alert("Whoa, 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.)");
173190
}
174191

175-
alert(trace[0].exception_msg);
176-
}
177-
else if (trace[trace.length - 1].exception_msg) {
178-
alert(trace[trace.length - 1].exception_msg);
192+
$('#executeBtn').html("Visualize execution");
193+
$('#executeBtn').attr('disabled', false);
179194
}
180195
else {
181-
alert("Whoa, 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.)");
182-
}
196+
var startingInstruction = 0;
183197

184-
$('#executeBtn').html("Visualize execution");
185-
$('#executeBtn').attr('disabled', false);
186-
}
187-
else {
188-
var startingInstruction = 0;
198+
// only do this at most ONCE, and then clear out preseededCurInstr
199+
if (preseededCurInstr && preseededCurInstr < trace.length) { // NOP anyways if preseededCurInstr is 0
200+
startingInstruction = preseededCurInstr;
201+
preseededCurInstr = null;
202+
}
189203

190-
// only do this at most ONCE, and then clear out preseededCurInstr
191-
if (preseededCurInstr && preseededCurInstr < trace.length) { // NOP anyways if preseededCurInstr is 0
192-
startingInstruction = preseededCurInstr;
193-
preseededCurInstr = null;
194-
}
204+
// forceStartingInstr overrides everything else
205+
if (forceStartingInstr !== undefined) {
206+
startingInstruction = forceStartingInstr;
207+
}
195208

196-
myVisualizer = new ExecutionVisualizer('pyOutputPane',
197-
dataFromBackend,
198-
{startingInstruction: startingInstruction,
199-
updateOutputCallback: function() {$('#urlOutput,#embedCodeOutput').val('');},
200-
// tricky: selector 'true' and 'false' values are strings!
201-
disableHeapNesting: ($('#heapPrimitivesSelector').val() == 'true'),
202-
drawParentPointers: ($('#drawParentPointerSelector').val() == 'true'),
203-
textualMemoryLabels: ($('#textualMemoryLabelsSelector').val() == 'true'),
204-
//allowEditAnnotations: true,
205-
});
206-
207-
208-
// set keyboard bindings
209-
$(document).keydown(function(k) {
210-
if (!keyStuckDown) {
211-
if (k.keyCode == 37) { // left arrow
212-
if (myVisualizer.stepBack()) {
213-
k.preventDefault(); // don't horizontally scroll the display
214-
keyStuckDown = true;
209+
myVisualizer = new ExecutionVisualizer('pyOutputPane',
210+
dataFromBackend,
211+
{startingInstruction: startingInstruction,
212+
updateOutputCallback: function() {$('#urlOutput,#embedCodeOutput').val('');},
213+
// tricky: selector 'true' and 'false' values are strings!
214+
disableHeapNesting: ($('#heapPrimitivesSelector').val() == 'true'),
215+
drawParentPointers: ($('#drawParentPointerSelector').val() == 'true'),
216+
textualMemoryLabels: ($('#textualMemoryLabelsSelector').val() == 'true'),
217+
executeCodeWithRawInputFunc: executeCodeWithRawInput,
218+
//allowEditAnnotations: true,
219+
});
220+
221+
222+
// set keyboard bindings
223+
$(document).keydown(function(k) {
224+
if (!keyStuckDown) {
225+
if (k.keyCode == 37) { // left arrow
226+
if (myVisualizer.stepBack()) {
227+
k.preventDefault(); // don't horizontally scroll the display
228+
keyStuckDown = true;
229+
}
215230
}
216-
}
217-
else if (k.keyCode == 39) { // right arrow
218-
if (myVisualizer.stepForward()) {
219-
k.preventDefault(); // don't horizontally scroll the display
220-
keyStuckDown = true;
231+
else if (k.keyCode == 39) { // right arrow
232+
if (myVisualizer.stepForward()) {
233+
k.preventDefault(); // don't horizontally scroll the display
234+
keyStuckDown = true;
235+
}
221236
}
222237
}
223-
}
224-
});
238+
});
225239

226-
$(document).keyup(function(k) {
227-
keyStuckDown = false;
228-
});
240+
$(document).keyup(function(k) {
241+
keyStuckDown = false;
242+
});
229243

230244

231-
// also scroll to top to make the UI more usable on smaller monitors
232-
$(document).scrollTop(0);
245+
// also scroll to top to make the UI more usable on smaller monitors
246+
$(document).scrollTop(0);
233247

234-
$.bbq.pushState({ mode: 'display' }, 2 /* completely override other hash strings to keep URL clean */);
235-
}
236-
},
237-
"json");
238-
});
248+
$.bbq.pushState({ mode: 'display' }, 2 /* completely override other hash strings to keep URL clean */);
249+
}
250+
},
251+
"json");
252+
}
253+
254+
function executeCodeFromScratch() {
255+
// reset these globals
256+
rawInputLst = [];
239257

258+
executeCode();
259+
}
260+
261+
function executeCodeWithRawInput(rawInputStr, curInstr) {
262+
enterDisplayNoFrillsMode();
263+
264+
// set some globals
265+
rawInputLst.push(rawInputStr);
266+
267+
executeCode(curInstr);
268+
}
269+
270+
$("#executeBtn").attr('disabled', false);
271+
$("#executeBtn").click(executeCodeFromScratch);
240272

241273

242274
// canned examples
@@ -306,6 +338,11 @@ $(document).ready(function() {
306338
return false;
307339
});
308340

341+
$("#rawInputExampleLink").click(function() {
342+
$.get("example-code/raw_input.txt", setCodeMirrorVal);
343+
return false;
344+
});
345+
309346
$("#oop1ExampleLink").click(function() {
310347
$.get("example-code/oop_1.txt", setCodeMirrorVal);
311348
return false;

0 commit comments

Comments
 (0)