aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2011-12-01 17:45:31 -0800
committerAlon Zakai <alonzakai@gmail.com>2011-12-01 17:45:31 -0800
commit12e85d4ab3fb0447d60165c6ca00296ae0fce9ce (patch)
tree866ec6b3c85706d96af23dccec745336d25692a7
parent40ba40ebb65f14bf51a229f4b799164f1c24caaf (diff)
support for running emscripten in the browser
-rw-r--r--src/compiler.html14
-rw-r--r--src/compiler.js58
2 files changed, 60 insertions, 12 deletions
diff --git a/src/compiler.html b/src/compiler.html
new file mode 100644
index 00000000..69724076
--- /dev/null
+++ b/src/compiler.html
@@ -0,0 +1,14 @@
+<html>
+<body>
+<h2>Run the emscripten compiler in a web page, just for laughs</h2>
+Open the web console to see stderr output
+<hr>
+<pre id="output"></pre>
+<script>
+arguments = ['', '../tests/cases/phicubed.ll'];
+</script>
+<script src="compiler.js">
+</script>
+</body>
+</html>
+
diff --git a/src/compiler.js b/src/compiler.js
index c6059f29..dbd44f69 100644
--- a/src/compiler.js
+++ b/src/compiler.js
@@ -5,15 +5,47 @@ try {
gcparam('maxBytes', 1024*1024*1024);
} catch(e) {}
-// Prep - allow this to run in both SpiderMonkey and V8
-if (!this['load']) {
- load = function(f) { eval(snarf(f)) };
-}
-if (!this['read']) {
- read = function(f) { snarf(f) };
-}
-if (!this['arguments']) {
- arguments = scriptArgs;
+var ENVIRONMENT_IS_SHELL = typeof window === 'undefined';
+
+if (ENVIRONMENT_IS_SHELL) {
+ // Polyfill over SpiderMonkey/V8 differences
+ if (!this['load']) {
+ load = function(f) { eval(snarf(f)) };
+ }
+ if (!this['read']) {
+ read = function(f) { snarf(f) };
+ }
+ if (!this['arguments']) {
+ arguments = scriptArgs;
+ }
+} else {
+ // We are on the web.
+ var outputElement = document.getElementById('output');
+ print = function(x) {
+ outputElement.innerHTML += x;
+ };
+
+ printErr = function(x) {
+ console.log(x);
+ };
+
+ read = function(url) {
+ var xhr = new XMLHttpRequest();
+ xhr.open('GET', url, false);
+ xhr.send(null);
+ return xhr.responseText;
+ };
+
+ var that = this;
+ load = function(url) {
+ // We can't just eval naively, we need properties here to be added to the toplevel global.
+ var src = read(url);
+ eval.call(null, src);
+ };
+
+ if (!this['arguments']) {
+ arguments = [];
+ }
}
// Basic utilities
@@ -27,9 +59,11 @@ load('settings.js');
var settings_file = arguments[0];
var ll_file = arguments[1];
-var settings = JSON.parse(read(settings_file));
-for (setting in settings) {
- this[setting] = settings[setting];
+if (settings_file) {
+ var settings = JSON.parse(read(settings_file));
+ for (setting in settings) {
+ this[setting] = settings[setting];
+ }
}
var CONSTANTS = { 'QUANTUM_SIZE': QUANTUM_SIZE };