diff options
author | Alon Zakai <alonzakai@gmail.com> | 2014-01-03 15:16:17 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2014-01-03 15:16:17 -0800 |
commit | 8478d6aee54d6c52de16d8c58309534afbf5bf9e (patch) | |
tree | bec73fd8e0cd6888d2dfb6b9e6a1423cc2432f61 /src/hello_world.js | |
parent | 1a007b1631509b9d72499a8f4402294017ee04dc (diff) | |
parent | a8e26049c1a72fa6b19dac45fa2b44616f94241a (diff) |
Merge branch 'incoming'
Diffstat (limited to 'src/hello_world.js')
-rw-r--r-- | src/hello_world.js | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/hello_world.js b/src/hello_world.js new file mode 100644 index 00000000..01082eb4 --- /dev/null +++ b/src/hello_world.js @@ -0,0 +1,92 @@ +// *** 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'; +var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER; + +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) { + 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; + }; + + 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 (typeof scriptArgs != 'undefined') { + arguments_ = scriptArgs; + } else if (typeof arguments != 'undefined') { + arguments_ = arguments; + } + +} else if (ENVIRONMENT_IS_WEB) { + this['print'] = printErr = function(x) { + console.log(x); + }; + + this['read'] = function(url) { + var xhr = new XMLHttpRequest(); + xhr.open('GET', url, false); + xhr.send(null); + return xhr.responseText; + }; + + if (this['arguments']) { + arguments_ = arguments; + } +} else if (ENVIRONMENT_IS_WORKER) { + // We can do very little here... + + this['load'] = importScripts; + +} else { + throw 'Unknown runtime environment. Where are we?'; +} + +function globalEval(x) { + eval.call(null, x); +} + +if (typeof load == 'undefined' && typeof read != 'undefined') { + this['load'] = function(f) { + globalEval(read(f)); + }; +} + +if (typeof printErr === 'undefined') { + this['printErr'] = function(){}; +} + +if (typeof print === 'undefined') { + this['print'] = printErr; +} +// *** Environment setup code *** + +print('hello, world!'); + |