aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/compiler.js19
-rw-r--r--src/library.js1
-rw-r--r--src/shell.js104
3 files changed, 93 insertions, 31 deletions
diff --git a/src/compiler.js b/src/compiler.js
index d2d14d9b..90f05cf0 100644
--- a/src/compiler.js
+++ b/src/compiler.js
@@ -7,6 +7,9 @@ try {
gcparam('maxBytes', 1024*1024*1024);
} catch(e) {}
+
+// The environment setup code appears both here and in shell.js, because it can't be shared. Keep them in sync!
+// *** Environment setup code ***
var arguments_ = [];
var ENVIRONMENT_IS_NODE = typeof process === 'object';
@@ -16,20 +19,24 @@ 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');
+ process['stdout'].write(x + '\n');
};
printErr = function(x) {
- process.stderr.write(x + '\n');
+ process['stderr'].write(x + '\n');
};
var nodeFS = require('fs');
read = function(filename) {
- if (filename[0] != '/') filename = __dirname.split('/').slice(0, -1).join('/') + '/src/' + filename;
- return nodeFS.readFileSync(filename).toString();
+ 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);
+ arguments_ = process['argv'].slice(2);
} else if (ENVIRONMENT_IS_SHELL) {
// Polyfill over SpiderMonkey/V8 differences
@@ -71,6 +78,8 @@ if (!this['load']) {
globalEval(read(f));
};
}
+// *** Environment setup code ***
+
// Basic utilities
diff --git a/src/library.js b/src/library.js
index ce6152d1..1d296981 100644
--- a/src/library.js
+++ b/src/library.js
@@ -393,6 +393,7 @@ LibraryManager.library = {
},
quit: function() {
+ if (!FS.init.initialized) return;
// Flush any partially-printed lines in stdout and stderr
if (FS.streams[2].object.output.buffer.length > 0) FS.streams[2].object.output('\n'.charCodeAt(0));
if (FS.streams[3].object.output.buffer.length > 0) FS.streams[3].object.output('\n'.charCodeAt(0));
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
-*/
-