diff options
Diffstat (limited to 'src/shell.js')
-rw-r--r-- | src/shell.js | 114 |
1 files changed, 63 insertions, 51 deletions
diff --git a/src/shell.js b/src/shell.js index 1f987926..bac4eaa3 100644 --- a/src/shell.js +++ b/src/shell.js @@ -1,8 +1,31 @@ -try { - this['Module'] = Module; - Module.test; -} catch(e) { - this['Module'] = Module = {}; +// The Module object: Our interface to the outside world. We import +// and export values on it, and do the work to get that through +// closure compiler if necessary. There are various ways Module can be used: +// 1. Not defined. We create it here +// 2. A function parameter, function(Module) { ..generated code.. } +// 3. pre-run appended it, var Module = {}; ..generated code.. +// 4. External script tag defines var Module. +// We need to do an eval in order to handle the closure compiler +// case, where this code here is minified but Module was defined +// elsewhere (e.g. case 4 above). We also need to check if Module +// already exists (e.g. case 3 above). +// Note that if you want to run closure, and also to use Module +// after the generated code, you will need to define var Module = {}; +// before the code. Then that object will be used in the code, and you +// can continue to use Module afterwards as well. +var Module; +if (!Module) Module = eval('(function() { try { return {{{ EXPORT_NAME }}} || {} } catch(e) { return {} } })()'); + +// Sometimes an existing Module object exists with properties +// meant to overwrite the default module functionality. Here +// we collect those properties and reapply _after_ we configure +// the current environment's defaults to avoid having to be so +// defensive during initialization. +var moduleOverrides = {}; +for (var key in Module) { + if (Module.hasOwnProperty(key)) { + moduleOverrides[key] = Module[key]; + } } // The environment setup code below is customized to use Module. @@ -43,14 +66,11 @@ if (ENVIRONMENT_IS_NODE) { globalEval(read(f)); }; - if (!Module['arguments']) { - Module['arguments'] = process['argv'].slice(2); - } + Module['arguments'] = process['argv'].slice(2); module.exports = Module; } - -if (ENVIRONMENT_IS_SHELL) { +else if (ENVIRONMENT_IS_SHELL) { Module['print'] = print; if (typeof printErr != 'undefined') Module['printErr'] = printErr; // not present in v8 or older sm @@ -59,34 +79,15 @@ if (ENVIRONMENT_IS_SHELL) { return read(f, 'binary'); }; - if (!Module['arguments']) { - if (typeof scriptArgs != 'undefined') { - Module['arguments'] = scriptArgs; - } else if (typeof arguments != 'undefined') { - Module['arguments'] = arguments; - } - } - - this['{{{ EXPORT_NAME }}}'] = Module; -} - -if (ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER) { - if (!Module['print']) { - Module['print'] = function(x) { - console.log(x); - }; - } - - if (!Module['printErr']) { - Module['printErr'] = function(x) { - console.log(x); - }; + if (typeof scriptArgs != 'undefined') { + Module['arguments'] = scriptArgs; + } else if (typeof arguments != 'undefined') { + Module['arguments'] = arguments; } this['{{{ EXPORT_NAME }}}'] = Module; } - -if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) { +else if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) { Module['read'] = function(url) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, false); @@ -94,28 +95,33 @@ if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) { return xhr.responseText; }; - if (!Module['arguments']) { - if (typeof arguments != 'undefined') { - Module['arguments'] = arguments; - } + if (typeof arguments != 'undefined') { + Module['arguments'] = arguments; } -} -if (ENVIRONMENT_IS_WORKER) { - // We can do very little here... - var TRY_USE_DUMP = false; - if (!Module['print']) { + if (ENVIRONMENT_IS_WEB) { + Module['print'] = function(x) { + console.log(x); + }; + + Module['printErr'] = function(x) { + console.log(x); + }; + + this['{{{ EXPORT_NAME }}}'] = Module; + } else if (ENVIRONMENT_IS_WORKER) { + // We can do very little here... + var TRY_USE_DUMP = false; Module['print'] = (TRY_USE_DUMP && (typeof(dump) !== "undefined") ? (function(x) { dump(x); }) : (function(x) { // self.postMessage(x); // enable this if you want stdout to be sent as messages })); - } - Module['load'] = importScripts; + Module['load'] = importScripts; + } } - -if (!ENVIRONMENT_IS_WORKER && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_SHELL) { +else { // Unreachable because SHELL is dependant on the others throw 'Unknown runtime environment. Where are we?'; } @@ -144,10 +150,16 @@ Module.print = Module['print']; Module.printErr = Module['printErr']; // Callbacks -if (!Module['preRun']) Module['preRun'] = []; -if (!Module['postRun']) Module['postRun'] = []; +Module['preRun'] = []; +Module['postRun'] = []; - {{BODY}} +// Merge back in the overrides +for (var key in moduleOverrides) { + if (moduleOverrides.hasOwnProperty(key)) { + Module[key] = moduleOverrides[key]; + } +} - // {{MODULE_ADDITIONS}} +{{BODY}} +// {{MODULE_ADDITIONS}} |