aboutsummaryrefslogtreecommitdiff
path: root/src/shell.js
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-03-19 13:21:03 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-03-19 13:25:46 -0700
commitea490e9c7f6f98b0df1de1e6259a86704d8d150d (patch)
tree696a62faf94dda343f0ed32f965412f6171e8716 /src/shell.js
parent10cfa9fc93ab611fb8c94aed563513fe9c124644 (diff)
refactor use of this[..] in shell.js to use Module[..]
Diffstat (limited to 'src/shell.js')
-rw-r--r--src/shell.js86
1 files changed, 45 insertions, 41 deletions
diff --git a/src/shell.js b/src/shell.js
index 1648218a..5b6419c0 100644
--- a/src/shell.js
+++ b/src/shell.js
@@ -1,9 +1,13 @@
// TODO: " u s e s t r i c t ";
+try {
+ this['Module'] = Module;
+} catch(e) {
+ this['Module'] = Module = {};
+}
+// The environment setup code below is customized to use Module.
// *** Environment setup code ***
-var arguments_ = [];
-
var ENVIRONMENT_IS_NODE = typeof process === 'object';
var ENVIRONMENT_IS_WEB = typeof window === 'object';
var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
@@ -12,16 +16,16 @@ var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIR
if (ENVIRONMENT_IS_NODE) {
// Expose functionality in the same simple way that the shells work
// Note that we pollute the global namespace here, otherwise we break in node
- print = function(x) {
+ Module['print'] = function(x) {
process['stdout'].write(x + '\n');
};
- printErr = function(x) {
+ Module['printErr'] = function(x) {
process['stderr'].write(x + '\n');
};
var nodeFS = require('fs');
- read = function(filename) {
+ Module['read'] = function(filename) {
var ret = nodeFS['readFileSync'](filename).toString();
if (!ret && filename[0] != '/') {
filename = __dirname.split('/').slice(0, -1).join('/') + '/src/' + filename;
@@ -30,43 +34,56 @@ if (ENVIRONMENT_IS_NODE) {
return ret;
};
- load = function(f) {
+ Module['load'] = function(f) {
globalEval(read(f));
};
- arguments_ = process['argv'].slice(2);
+ Module['arguments'] = process['argv'].slice(2);
} else if (ENVIRONMENT_IS_SHELL) {
+ Module['print'] = print;
+ Module['printErr'] = printErr;
+
// Polyfill over SpiderMonkey/V8 differences
- if (!this['read']) {
- this['read'] = function(f) { snarf(f) };
+ if (typeof read != 'undefined') {
+ Module['read'] = read;
+ } else {
+ Module['read'] = function(f) { snarf(f) };
}
if (typeof scriptArgs != 'undefined') {
- arguments_ = scriptArgs;
+ Module['arguments'] = scriptArgs;
} else if (typeof arguments != 'undefined') {
- arguments_ = arguments;
+ Module['arguments'] = arguments;
}
} else if (ENVIRONMENT_IS_WEB) {
- this['print'] = printErr = function(x) {
- console.log(x);
- };
+ if (!Module['print']) {
+ Module['print'] = function(x) {
+ console.log(x);
+ };
+ }
+
+ if (!Module['printErr']) {
+ Module['printErr'] = function(x) {
+ console.log(x);
+ };
+ }
- this['read'] = function(url) {
+ Module['read'] = function(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, false);
xhr.send(null);
return xhr.responseText;
};
- if (this['arguments']) {
- arguments_ = arguments;
+ if (typeof arguments != 'undefined') {
+ Module['arguments'] = arguments;
}
} else if (ENVIRONMENT_IS_WORKER) {
// We can do very little here...
- this['load'] = importScripts;
+ Module['load'] = importScripts;
} else {
throw 'Unknown runtime environment. Where are we?';
@@ -75,34 +92,21 @@ if (ENVIRONMENT_IS_NODE) {
function globalEval(x) {
eval.call(null, x);
}
-
-if (typeof load == 'undefined' && typeof read != 'undefined') {
- this['load'] = function(f) {
- globalEval(read(f));
+if (!Module['load'] == 'undefined' && Module['read']) {
+ Module['load'] = function(f) {
+ globalEval(Module['read'](f));
};
}
-
-if (typeof printErr === 'undefined') {
- this['printErr'] = function(){};
-}
-
-if (typeof print === 'undefined') {
- this['print'] = printErr;
-}
-// *** Environment setup code ***
-
-
-try {
- this['Module'] = Module;
-} catch(e) {
- this['Module'] = Module = {};
+if (!Module['printErr']) {
+ Module['printErr'] = function(){};
}
-if (!Module.arguments) {
- Module.arguments = arguments_;
+if (!Module['print']) {
+ Module['print'] = Module['printErr'];
}
-if (Module.print) {
- print = Module.print;
+if (!Module['arguments']) {
+ Module['arguments'] = [];
}
+// *** Environment setup code ***
{{BODY}}