Skip to content

Instantly share code, notes, and snippets.

@daniely
Last active April 30, 2016 00:28
Show Gist options
  • Save daniely/53d92b9a7aa5cf4a6eed5a62a44d088b to your computer and use it in GitHub Desktop.
Save daniely/53d92b9a7aa5cf4a6eed5a62a44d088b to your computer and use it in GitHub Desktop.
// executes the given code and handles the result
var run = function(code) {
var result = {
input: code,
output: null,
error: null
};
try {
//result.output = stringify(runHidden(code));
// don't stringify since we are doing a join now
result.output = runHidden(code);
} catch(e) {
result.error = e.message;
}
if (result.output === 'undefined') {
result.output = '';
}
application.remote.output(result);
}
// protects even the worker scope from being accessed
var runHidden = function(code) {
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;
var importScripts = null;
var application = null;
//var console = null;
// override console.log and store log entires in log_a
var log_a = [];
if (typeof console != "undefined")
if (typeof console.log != 'undefined')
console.olog = console.log;
else
console.olog = function() {};
console.log = function(message) {
//console.olog(message);
log_a.push(message);
};
console.error = console.debug = console.info = console.log
// better error messages
var result;
try {
result = eval(code);
} catch(e) {
result = e.message;
}
// include output of eval-ing user submitted code
log_a.push(result);
return log_a.join("\n");
}
// 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});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment