Skip to content

Commit 818a89a

Browse files
committed
got basic py/js execution working (without sandbox)
1 parent cdcbf7c commit 818a89a

3 files changed

Lines changed: 65 additions & 16 deletions

File tree

‎v4-cokapi/cokapi.js‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3434
var USE_DOCKER_SANDBOX = false;
3535

3636
var assert = require('assert');
37+
var child_process = require('child_process');
3738
var express = require('express');
3839
var serveStatic = require('serve-static');
40+
41+
3942
var app = express();
4043

4144
app.use(serveStatic('static/')); // put all static files in here
@@ -44,10 +47,69 @@ app.get('/exec_py2', function(req, res) {
4447
if (USE_DOCKER_SANDBOX) {
4548
assert(false);
4649
} else {
47-
res.send('Hello World!');
50+
executePython('python', req, res);
51+
}
52+
});
53+
54+
app.get('/exec_py3', function(req, res) {
55+
if (USE_DOCKER_SANDBOX) {
56+
assert(false);
57+
} else {
58+
executePython('python3', req, res);
59+
}
60+
});
61+
62+
app.get('/exec_js', function(req, res) {
63+
if (USE_DOCKER_SANDBOX) {
64+
assert(false);
65+
} else {
66+
var usrCod = req.query.user_script;
67+
var args = ['--expose-debug-as=Debug',
68+
'backends/javascript/jslogger.js',
69+
'--jsondump=true',
70+
'--code=' + usrCod];
71+
72+
var child = child_process.spawn('node', args);
73+
var stdoutDat = '';
74+
child.stdout.on('data', function(data) {
75+
stdoutDat += data;
76+
});
77+
child.on('close', function(code) {
78+
res.send(String(stdoutDat));
79+
});
4880
}
4981
});
5082

83+
function executePython(pythonExe, req, res) {
84+
var parsedOptions = JSON.parse(req.query.options_json);
85+
86+
var usrCod = req.query.user_script;
87+
var usrInputJson = req.query.raw_input_json;
88+
var cumulativeMode = parsedOptions.cumulative_mode;
89+
var heapPrimitives = parsedOptions.heap_primitives;
90+
91+
var args = ['backends/python/generate_json_trace.py'];
92+
if (cumulativeMode) {
93+
args.push('-c');
94+
}
95+
if (heapPrimitives) {
96+
args.push('-p');
97+
}
98+
if (usrInputJson) {
99+
args.push('--input=' + usrInputJson);
100+
}
101+
args.push('--code=' + usrCod);
102+
103+
var child = child_process.spawn(pythonExe, args);
104+
var stdoutDat = '';
105+
child.stdout.on('data', function(data) {
106+
stdoutDat += data;
107+
});
108+
child.on('close', function(code) {
109+
res.send(String(stdoutDat));
110+
});
111+
}
112+
51113
var server = app.listen(3000, function() {
52114
var host = server.address().address;
53115
var port = server.address().port;

‎v4-cokapi/static/js/cokapi-frontend.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function executeCode(forceStartingInstr, forceRawInputLst) {
101101

102102

103103
$(document).ready(function() {
104-
$("#optionsPane").hide();
104+
//$("#optionsPane").hide();
105105

106106
// canned examples
107107

‎v4-cokapi/static/js/opt-frontend-common.js‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,9 @@ function updateAppDisplay(newAppMode) {
460460
appMode = 'edit'; // canonicalize
461461

462462
$("#pyInputPane").show();
463-
$("#pyOutputPane,#surveyHeader").hide();
463+
$("#pyOutputPane").hide();
464464
$("#embedLinkDiv").hide();
465465

466-
$(".surveyQ").val(''); // clear all survey results when user hits forward/back
467-
468466
// destroy all annotation bubbles (NB: kludgy)
469467
if (myVisualizer) {
470468
myVisualizer.destroyAllAnnotationBubbles();
@@ -503,13 +501,6 @@ function updateAppDisplay(newAppMode) {
503501

504502
$(document).scrollTop(0); // scroll to top to make UX better on small monitors
505503

506-
507-
// NASTY global :(
508-
if (pendingCodeOutputScrollTop) {
509-
myVisualizer.domRoot.find('#pyCodeOutputDiv').scrollTop(pendingCodeOutputScrollTop);
510-
pendingCodeOutputScrollTop = null;
511-
}
512-
513504
$.doTimeout('pyCodeOutputDivScroll'); // cancel any prior scheduled calls
514505

515506
myVisualizer.domRoot.find('#pyCodeOutputDiv').scroll(function(e) {
@@ -604,10 +595,6 @@ function optFinishSuccessfulExecution() {
604595
// compress the logs a bit when there's rapid scrubbing or scrolling)
605596
myVisualizer.updateHistory = [];
606597
myVisualizer.updateHistory.push([myVisualizer.curInstr, 0]); // seed it!
607-
608-
// For version 3+, dynamically generate a survey whenever the user
609-
// successfully executes a piece of code
610-
initializeDisplayModeSurvey();
611598
}
612599

613600

0 commit comments

Comments
 (0)