aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnthony Pesch <inolen@gmail.com>2013-07-22 15:07:56 -0700
committerAnthony Pesch <inolen@gmail.com>2013-07-26 10:47:14 -0700
commit80f517d7873a9f9bfa766bd2620e4a0e2146f1b9 (patch)
tree3589741d42cebc99320f80e2020c0ddeba088062 /src
parent005113837b0969b193c6c485f674a132140e39d1 (diff)
tidy up defensive environment initialization
Diffstat (limited to 'src')
-rw-r--r--src/shell.js76
1 files changed, 41 insertions, 35 deletions
diff --git a/src/shell.js b/src/shell.js
index 11420123..679201d0 100644
--- a/src/shell.js
+++ b/src/shell.js
@@ -1,4 +1,15 @@
+// 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 Module = typeof {{{ EXPORT_NAME }}} !== 'undefined' ? {{{ EXPORT_NAME }}} : {};
+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.
// *** Environment setup code ***
@@ -38,9 +49,7 @@ 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;
}
@@ -54,29 +63,23 @@ 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;
- }
+ 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);
- };
- }
+ Module['print'] = function(x) {
+ console.log(x);
+ };
- if (!Module['printErr']) {
- Module['printErr'] = function(x) {
- console.log(x);
- };
- }
+ Module['printErr'] = function(x) {
+ console.log(x);
+ };
this['{{{ EXPORT_NAME }}}'] = Module;
}
@@ -89,23 +92,19 @@ 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']) {
- 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['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;
}
@@ -139,9 +138,16 @@ Module.print = Module['print'];
Module.printErr = Module['printErr'];
// Callbacks
-if (!Module['preRun']) Module['preRun'] = [];
-if (!Module['postRun']) Module['postRun'] = [];
+Module['preRun'] = [];
+Module['postRun'] = [];
+
+// Merge back in the overrides
+for (var key in moduleOverrides) {
+ if (moduleOverrides.hasOwnProperty(key)) {
+ Module[key] = moduleOverrides[key];
+ }
+}
- {{BODY}}
+{{BODY}}
- // {{MODULE_ADDITIONS}}
+// {{MODULE_ADDITIONS}}