aboutsummaryrefslogtreecommitdiff
path: root/src/shell.js
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-04-18 19:22:09 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-04-18 19:22:09 -0700
commit1c11fdd98530b6d35e51c0eba14fb1f375d05229 (patch)
treebd471ce02003d0e1d16dfe773d1ed4d0acdcb499 /src/shell.js
parentd6cff2177ec065aa14f228ab547abc29ef37b248 (diff)
parent327b6f859e95be71e5613f24cc1c9d4f4b97c15f (diff)
merge
Diffstat (limited to 'src/shell.js')
-rw-r--r--src/shell.js107
1 files changed, 61 insertions, 46 deletions
diff --git a/src/shell.js b/src/shell.js
index bc527192..e48be3b0 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,61 +16,81 @@ 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');
+ var nodePath = require('path');
- read = function(filename) {
+ Module['read'] = function(filename) {
+ filename = nodePath['normalize'](filename);
var ret = nodeFS['readFileSync'](filename).toString();
- if (!ret && filename[0] != '/') {
- filename = __dirname.split('/').slice(0, -1).join('/') + '/src/' + filename;
+ // The path is absolute if the normalized version is the same as the resolved.
+ if (!ret && filename != nodePath['resolve'](filename)) {
+ filename = path.join(__dirname, '..', 'src', filename);
ret = nodeFS['readFileSync'](filename).toString();
}
return ret;
};
- load = function(f) {
+ Module['load'] = function(f) {
globalEval(read(f));
};
- arguments_ = process['argv'].slice(2);
-
-} else if (ENVIRONMENT_IS_SHELL) {
- // Polyfill over SpiderMonkey/V8 differences
- if (!this['read']) {
- this['read'] = function(f) { snarf(f) };
+ if (!Module['arguments']) {
+ Module['arguments'] = process['argv'].slice(2);
}
+} else if (ENVIRONMENT_IS_SHELL) {
+ Module['print'] = print;
+ Module['printErr'] = printErr;
- if (!this['arguments']) {
- arguments_ = scriptArgs;
+ // Polyfill over SpiderMonkey/V8 differences
+ if (typeof read != 'undefined') {
+ Module['read'] = read;
} else {
- arguments_ = arguments;
+ Module['read'] = function(f) { snarf(f) };
}
+ if (!Module['arguments']) {
+ if (typeof scriptArgs != 'undefined') {
+ Module['arguments'] = scriptArgs;
+ } else if (typeof arguments != 'undefined') {
+ 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);
+ };
+ }
- this['read'] = function(url) {
+ if (!Module['printErr']) {
+ Module['printErr'] = function(x) {
+ console.log(x);
+ };
+ }
+
+ 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 (!Module['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 +99,25 @@ 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 (!Module['printErr']) {
+ Module['printErr'] = function(){};
}
-
-if (typeof print === 'undefined') {
- this['print'] = printErr;
+if (!Module['print']) {
+ Module['print'] = Module['printErr'];
+}
+if (!Module['arguments']) {
+ Module['arguments'] = [];
}
// *** Environment setup code ***
-
-try {
- this['Module'] = Module;
-} catch(e) {
- this['Module'] = Module = {};
-}
-if (!Module.arguments) {
- Module.arguments = arguments_;
-}
-if (Module.print) {
- print = Module.print;
-}
+// Closure helpers
+Module.print = Module['print'];
+Module.printErr = Module['printErr'];
{{BODY}}