2015-08-27 18:35:41 +00:00
|
|
|
/* eslint-disable no-eval */
|
|
|
|
/* global importScripts, application */
|
2015-04-15 03:04:54 +00:00
|
|
|
// executes the given code and handles the result
|
2015-08-27 18:35:41 +00:00
|
|
|
|
|
|
|
function importScript(url, error) {
|
|
|
|
try {
|
|
|
|
importScripts(url);
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:10:23 +00:00
|
|
|
function run(code, cb) {
|
|
|
|
var err = null;
|
2015-11-18 05:25:16 +00:00
|
|
|
var result = {};
|
2015-04-15 03:04:54 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
var codeExec = runHidden(code);
|
|
|
|
result.type = typeof codeExec;
|
|
|
|
result.output = stringify(codeExec);
|
2015-11-04 20:37:25 +00:00
|
|
|
} catch (e) {
|
2015-11-22 03:48:24 +00:00
|
|
|
err = e.message;
|
2015-11-13 19:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
2015-11-18 05:25:16 +00:00
|
|
|
cb(err, null);
|
2015-11-13 19:10:23 +00:00
|
|
|
} else {
|
|
|
|
cb(null, result);
|
2015-04-15 03:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.close();
|
2015-08-27 18:35:41 +00:00
|
|
|
}
|
2015-04-15 03:04:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
// protects even the worker scope from being accessed
|
2015-08-27 18:35:41 +00:00
|
|
|
function runHidden(code) {
|
|
|
|
|
2015-11-18 05:25:16 +00:00
|
|
|
/* eslint-disable no-unused-vars */
|
2015-04-15 03:04:54 +00:00
|
|
|
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;
|
2015-11-18 05:25:16 +00:00
|
|
|
/* eslint-enable no-unused-vars */
|
2015-08-27 18:35:41 +00:00
|
|
|
|
|
|
|
var error = null;
|
|
|
|
error = importScript(
|
|
|
|
'https://cdnjs.cloudflare.com/ajax/libs/chai/2.2.0/chai.min.js'
|
|
|
|
);
|
|
|
|
|
2015-08-27 19:36:29 +00:00
|
|
|
|
|
|
|
/* eslint-disable*/
|
|
|
|
var assert = chai.assert;
|
|
|
|
/* eslint-enable */
|
|
|
|
|
2015-08-27 18:35:41 +00:00
|
|
|
if (error) {
|
|
|
|
return error;
|
|
|
|
}
|
2015-04-15 03:04:54 +00:00
|
|
|
|
|
|
|
return eval(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// converts the output into a string
|
2015-08-27 18:35:41 +00:00
|
|
|
function stringify(output) {
|
2015-04-15 03:04:54 +00:00
|
|
|
var result;
|
|
|
|
|
2015-08-27 18:35:41 +00:00
|
|
|
if (typeof output === 'undefined') {
|
2015-04-15 03:04:54 +00:00
|
|
|
result = 'undefined';
|
|
|
|
} else if (output === null) {
|
|
|
|
result = 'null';
|
|
|
|
} else {
|
|
|
|
result = JSON.stringify(output) || output.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-08-27 18:35:41 +00:00
|
|
|
application.setInterface({ run: run });
|