From 376b58cc150d71f6a845d66482d562b516b04ba0 Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Thu, 27 Aug 2015 12:37:41 -0700 Subject: [PATCH] combine sandbox into commonFramework a @benmcmahon suggestion --- client/commonFramework.js | 117 ++++++++++++++++++++-- client/sandbox.js | 91 ----------------- server/views/coursewares/showBonfire.jade | 1 - server/views/coursewares/showHTML.jade | 1 - server/views/coursewares/showJS.jade | 1 - 5 files changed, 107 insertions(+), 104 deletions(-) delete mode 100644 client/sandbox.js diff --git a/client/commonFramework.js b/client/commonFramework.js index 8545d542f9..dd91756e06 100644 --- a/client/commonFramework.js +++ b/client/commonFramework.js @@ -1,4 +1,4 @@ -/* globals CodeMirror, challenge_Id, challenge_Name, challengeType */ +/* globals jailed, CodeMirror, challenge_Id, challenge_Name, challengeType */ // codeStorage var codeStorageFactory = (function($, localStorage) { @@ -68,6 +68,103 @@ var codeStorageFactory = (function($, localStorage) { return codeStorageFactory; }($, localStorage)); +var sandBox = (function() { + + var plugin = null; + + var sandBox = { + }; + + var printCallback; + + // sends the input to the plugin for evaluation + function submit(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.js', api); + plugin.whenDisconnected( function() { + // give some time to handle the last responce + setTimeout( function() { + endLoading(); + console.log('resetting on fatal plugin error'); + + if (challengeType === 0) { + codeOutput.setValue( + 'Sorry, your code is either too slow, has a fatal error, ' + + 'or contains an infinite loop.' + ); + } + reset(); + }, 10); + }); + }; + reset(); + sandBox.submit = submit; + return sandBox; +}()); + function replaceSafeTags(value) { return value .replace(/fccss/gi, '