Remove bonfire and courseware controller, relying on all challenge related logic in challenge.js. Remove bonfire folder. Move necessary JS to coursewares folder and rename.
This commit is contained in:
88
public/js/lib/coursewares/plugin_v0.1.5.js
Normal file
88
public/js/lib/coursewares/plugin_v0.1.5.js
Normal file
@@ -0,0 +1,88 @@
|
||||
|
||||
// executes the given code and handles the result
|
||||
var run = function(code) {
|
||||
var result = {
|
||||
input: code,
|
||||
output: null,
|
||||
error: null,
|
||||
type: null
|
||||
};
|
||||
|
||||
try {
|
||||
var codeExec = runHidden(code);
|
||||
result.type = typeof codeExec;
|
||||
result.output = stringify(codeExec);
|
||||
} catch(e) {
|
||||
result.error = e.message;
|
||||
}
|
||||
|
||||
application.remote.output(result);
|
||||
self.close();
|
||||
};
|
||||
|
||||
|
||||
// protects even the worker scope from being accessed
|
||||
var runHidden = function(code) {
|
||||
|
||||
var importScript = function(url) {
|
||||
var error = null;
|
||||
try {
|
||||
importScripts(url);
|
||||
} catch (e) {
|
||||
error = e;
|
||||
console.log('Unable to load %s!', url);
|
||||
}
|
||||
};
|
||||
var indexedDB = null;
|
||||
var location = null;
|
||||
var navigator = null;
|
||||
var onerror = null;
|
||||
var onmessage = null;
|
||||
var performance = null;
|
||||
var self = null;
|
||||
var webkitIndexedDB = null;
|
||||
var postMessage = null;
|
||||
var close = null;
|
||||
var openDatabase = null;
|
||||
var openDatabaseSync = null;
|
||||
var webkitRequestFileSystem = null;
|
||||
var webkitRequestFileSystemSync = null;
|
||||
var webkitResolveLocalFileSystemSyncURL = null;
|
||||
var webkitResolveLocalFileSystemURL = null;
|
||||
var addEventListener = null;
|
||||
var dispatchEvent = null;
|
||||
var removeEventListener = null;
|
||||
var dump = null;
|
||||
var onoffline = null;
|
||||
var ononline = null;
|
||||
importScripts(
|
||||
"https://cdnjs.cloudflare.com/ajax/libs/ramda/0.13.0/ramda.min.js");
|
||||
importScripts(
|
||||
"https://cdnjs.cloudflare.com/ajax/libs/chai/2.2.0/chai.min.js"
|
||||
)
|
||||
|
||||
var expect = chai.expect;
|
||||
var assert = chai.assert;
|
||||
|
||||
|
||||
return eval(code);
|
||||
}
|
||||
|
||||
|
||||
// converts the output into a string
|
||||
var stringify = function(output) {
|
||||
var result;
|
||||
|
||||
if (typeof output == 'undefined') {
|
||||
result = 'undefined';
|
||||
} else if (output === null) {
|
||||
result = 'null';
|
||||
} else {
|
||||
result = JSON.stringify(output) || output.toString();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
application.setInterface({run:run});
|
||||
87
public/js/lib/coursewares/sandbox.js
Normal file
87
public/js/lib/coursewares/sandbox.js
Normal file
@@ -0,0 +1,87 @@
|
||||
var printCallback;
|
||||
// sends the input to the plugin for evaluation
|
||||
var submit = function(code,callback) {
|
||||
printCallback = callback;
|
||||
// postpone the evaluation until the plugin is initialized
|
||||
plugin.whenConnected(
|
||||
function() {
|
||||
if (requests == 0) {
|
||||
startLoading();
|
||||
}
|
||||
|
||||
requests++;
|
||||
plugin.remote.run(code);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
// puts the message on the terminal
|
||||
var print = function(cls, msg) {
|
||||
printCallback(cls,msg);
|
||||
};
|
||||
|
||||
|
||||
// will restart the plugin if it does not respond
|
||||
var disconnectTimeout = null;
|
||||
var startLoading = function() {
|
||||
disconnectTimeout = setTimeout(disconnect, 3000);
|
||||
};
|
||||
|
||||
var endLoading = function() {
|
||||
clearTimeout(disconnectTimeout);
|
||||
};
|
||||
|
||||
var disconnect = function() {
|
||||
plugin.disconnect();
|
||||
};
|
||||
|
||||
|
||||
// interface provided to the plugin
|
||||
var api = {
|
||||
output: function(data) {
|
||||
endLoading();
|
||||
//print('input', data.input);
|
||||
if (data.error) {
|
||||
print('Error', data);
|
||||
reset();
|
||||
} else {
|
||||
print(null, data);
|
||||
reset();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// obtaining absolute path of this script
|
||||
var scripts = document.getElementsByTagName('script');
|
||||
var path = scripts[scripts.length-1].src
|
||||
.split('?')[0]
|
||||
.split('/')
|
||||
.slice(0, -1)
|
||||
.join('/')+'/';
|
||||
|
||||
|
||||
|
||||
var requests;
|
||||
|
||||
// (re)initializes the plugin
|
||||
var reset = function() {
|
||||
requests = 0;
|
||||
plugin = new jailed.Plugin(path+'plugin_v0.1.5.js', api);
|
||||
plugin.whenDisconnected( function() {
|
||||
// give some time to handle the last responce
|
||||
setTimeout( function() {
|
||||
endLoading();
|
||||
console.log("resetting on fatal plugin error");
|
||||
codeOutput.setValue("Sorry, your code is either too slow, has a fatal error, or contains an infinite loop.");
|
||||
reset();
|
||||
}, 10);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
// initialize everything
|
||||
var plugin = null;
|
||||
|
||||
reset();
|
||||
Reference in New Issue
Block a user