aboutsummaryrefslogtreecommitdiff
path: root/src/shell.js
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2011-12-03 20:09:11 -0800
committerAlon Zakai <alonzakai@gmail.com>2011-12-03 20:09:11 -0800
commit556decb19a4bcb32daad1569402fcc385d49b1d4 (patch)
tree1fdea88574ffd481f4b68776db86bc3d0169ef22 /src/shell.js
parent3d4a6d6d24fc5a220ba4b32d9bdae069389a084e (diff)
fixes for using node as both compiler engine and code running engine. node is now default in settings.py
Diffstat (limited to 'src/shell.js')
-rw-r--r--src/shell.js104
1 files changed, 78 insertions, 26 deletions
diff --git a/src/shell.js b/src/shell.js
index 7be76536..d52cd77d 100644
--- a/src/shell.js
+++ b/src/shell.js
@@ -1,36 +1,88 @@
// TODO: " u s e s t r i c t ";
-/*
-// Capture the output of this into a variable, if you want
-(function(Module, args) {
- Module = Module || {};
- Module.arguments = args || [];
-*/
-
-///*
-// Runs much faster, for some reason
-if (!this['Module']) {
- this['Module'] = {};
-}
-if (!Module.arguments) {
- try {
- Module.arguments = scriptArgs;
- } catch(e) {
- try {
- Module.arguments = arguments;
- } catch(e) {
- Module.arguments = [];
+
+// *** Environment setup code ***
+var arguments_ = [];
+
+var ENVIRONMENT_IS_NODE = typeof process === 'object';
+var ENVIRONMENT_IS_WEB = typeof window === 'object';
+var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE;
+
+if (ENVIRONMENT_IS_NODE) {
+ // Expose functionality in the same simple way that the shells work
+ print = function(x) {
+ process['stdout'].write(x + '\n');
+ };
+ printErr = function(x) {
+ process['stderr'].write(x + '\n');
+ };
+
+ var nodeFS = require('fs');
+
+ read = function(filename) {
+ var ret = nodeFS['readFileSync'](filename).toString();
+ if (!ret && filename[0] != '/') {
+ filename = __dirname.split('/').slice(0, -1).join('/') + '/src/' + filename;
+ ret = nodeFS['readFileSync'](filename).toString();
}
+ return ret;
+ };
+
+ arguments_ = process['argv'].slice(2);
+
+} else if (ENVIRONMENT_IS_SHELL) {
+ // Polyfill over SpiderMonkey/V8 differences
+ if (!this['read']) {
+ read = function(f) { snarf(f) };
+ }
+
+ if (!this['arguments']) {
+ arguments_ = scriptArgs;
+ } else {
+ arguments_ = arguments;
}
+
+} else if (ENVIRONMENT_IS_WEB) {
+ printErr = function(x) {
+ console.log(x);
+ };
+
+ read = function(url) {
+ var xhr = new XMLHttpRequest();
+ xhr.open('GET', url, false);
+ xhr.send(null);
+ return xhr.responseText;
+ };
+
+ if (this['arguments']) {
+ arguments_ = arguments;
+ }
+} else {
+ throw 'Unknown runtime environment. Where are we?';
+}
+
+function globalEval(x) {
+ eval.call(null, x);
+}
+
+if (!this['load']) {
+ load = function(f) {
+ globalEval(read(f));
+ };
+}
+// *** Environment setup code ***
+
+
+try {
+ this['Module'] = Module;
+} catch(e) {
+ this['Module'] = Module = {};
+}
+if (!Module.arguments) {
+ Module.arguments = arguments_;
}
-//*/
{{BODY}}
// {{MODULE_ADDITIONS}}
-/*
- return Module;
-}).call(this, {}, arguments); // Replace parameters as needed
-*/
-