diff options
-rw-r--r-- | src/compiler.js | 21 | ||||
-rw-r--r-- | src/shell.js | 21 | ||||
-rw-r--r-- | tests/hello_world.js | 21 | ||||
-rw-r--r-- | tests/hello_world_worker.cpp | 9 | ||||
-rw-r--r-- | tests/runner.py | 41 |
5 files changed, 91 insertions, 22 deletions
diff --git a/src/compiler.js b/src/compiler.js index 2015a09d..579cd98f 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -14,7 +14,8 @@ 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; +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 @@ -54,9 +55,6 @@ if (ENVIRONMENT_IS_NODE) { printErr = function(x) { console.log(x); }; - if (typeof print === 'undefined') { - print = printErr; - } read = function(url) { var xhr = new XMLHttpRequest(); @@ -68,6 +66,11 @@ if (ENVIRONMENT_IS_NODE) { if (this['arguments']) { arguments_ = arguments; } +} else if (ENVIRONMENT_IS_WORKER) { + // We can do very little here... + + load = importScripts; + } else { throw 'Unknown runtime environment. Where are we?'; } @@ -76,11 +79,19 @@ function globalEval(x) { eval.call(null, x); } -if (!this['load']) { +if (typeof load == 'undefined' && typeof read != 'undefined') { load = function(f) { globalEval(read(f)); }; } + +if (typeof printErr === 'undefined') { + printErr = function(){}; +} + +if (typeof print === 'undefined') { + print = printErr; +} // *** Environment setup code *** diff --git a/src/shell.js b/src/shell.js index 038fde02..61f2d5cd 100644 --- a/src/shell.js +++ b/src/shell.js @@ -6,7 +6,8 @@ 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; +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 @@ -46,9 +47,6 @@ if (ENVIRONMENT_IS_NODE) { printErr = function(x) { console.log(x); }; - if (typeof print === 'undefined') { - print = printErr; - } read = function(url) { var xhr = new XMLHttpRequest(); @@ -60,6 +58,11 @@ if (ENVIRONMENT_IS_NODE) { if (this['arguments']) { arguments_ = arguments; } +} else if (ENVIRONMENT_IS_WORKER) { + // We can do very little here... + + load = importScripts; + } else { throw 'Unknown runtime environment. Where are we?'; } @@ -68,11 +71,19 @@ function globalEval(x) { eval.call(null, x); } -if (!this['load']) { +if (typeof load == 'undefined' && typeof read != 'undefined') { load = function(f) { globalEval(read(f)); }; } + +if (typeof printErr === 'undefined') { + printErr = function(){}; +} + +if (typeof print === 'undefined') { + print = printErr; +} // *** Environment setup code *** diff --git a/tests/hello_world.js b/tests/hello_world.js index 530f5d4c..24bc0fdd 100644 --- a/tests/hello_world.js +++ b/tests/hello_world.js @@ -3,7 +3,8 @@ 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; +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 @@ -43,9 +44,6 @@ if (ENVIRONMENT_IS_NODE) { printErr = function(x) { console.log(x); }; - if (typeof print === 'undefined') { - print = printErr; - } read = function(url) { var xhr = new XMLHttpRequest(); @@ -57,6 +55,11 @@ if (ENVIRONMENT_IS_NODE) { if (this['arguments']) { arguments_ = arguments; } +} else if (ENVIRONMENT_IS_WORKER) { + // We can do very little here... + + load = importScripts; + } else { throw 'Unknown runtime environment. Where are we?'; } @@ -65,11 +68,19 @@ function globalEval(x) { eval.call(null, x); } -if (!this['load']) { +if (typeof load == 'undefined' && typeof read != 'undefined') { load = function(f) { globalEval(read(f)); }; } + +if (typeof printErr === 'undefined') { + printErr = function(){}; +} + +if (typeof print === 'undefined') { + print = printErr; +} // *** Environment setup code *** print('hello, world!'); diff --git a/tests/hello_world_worker.cpp b/tests/hello_world_worker.cpp new file mode 100644 index 00000000..5ea26d91 --- /dev/null +++ b/tests/hello_world_worker.cpp @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <emscripten.h> + +int main() +{ + printf("you should not see this text when in a worker!\n"); // this should not crash, but also should not show up anywhere if you are in a worker + emscripten_run_script("if (typeof postMessage !== 'undefined') { postMessage('hello from worker!') }"); +} + diff --git a/tests/runner.py b/tests/runner.py index 0d224c50..0ac04314 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -4780,6 +4780,7 @@ Child2:9 ''' self.do_run(src, 'hello, world!\nExit Status: 118') + # Generate tests for everything def make_run(name=-1, compiler=-1, llvm_opts=0, embetter=0, quantum_size=0, typed_arrays=0, defaults=False): exec(''' @@ -4977,17 +4978,43 @@ JavaScript in the final linking stage of building. # TODO: when this is done, more test runner to test these (i.e., test all -Ox thoroughly) # TODO: use -O3 in benchmarks, which will test that -O3 is optimized for max speed - # Finally, test HTML generation. (Coincidentally we also test that compiling a .cpp works in EMCC here.) + # Finally, do some web browser tests + def run_browser(html_file, message): + webbrowser.open_new(html_file) + print 'A web browser window should have opened a page containing the results of a part of this test.' + print 'You need to manually look at the page to see that it works ok: ' + message + print '(sleeping for a bit to keep the directory alive for the web browser..)' + time.sleep(5) + print '(moving on..)' + + # test HTML generation. clear() output = Popen([EMCC, path_from_root('tests', 'hello_world_sdl.cpp'), '-o', 'something.html'], stdout=PIPE, stderr=PIPE).communicate(input) assert len(output[0]) == 0, output[0] assert os.path.exists('something.html'), output - webbrowser.open_new(os.path.join(self.get_dir(), 'something.html')) - print 'A web browser window should have opened a page containing the results of a part of this test.' - print 'You need to manually look at the page to see that it works ok: You should see "hello, world!" and a colored cube.' - print '(sleeping for a bit to keep the directory alive for the web browser..)' - time.sleep(5) - print '(moving on..)' + run_browser('something.html', 'You should see "hello, world!" and a colored cube.') + + # And test running in a web worker + clear() + output = Popen([EMCC, path_from_root('tests', 'hello_world_worker.cpp'), '-o', 'worker.js'], stdout=PIPE, stderr=PIPE).communicate(input) + assert len(output[0]) == 0, output[0] + assert os.path.exists('worker.js'), output + self.assertContained('you should not see this text when in a worker!', run_js('worker.js')) # code should run standalone + html_file = open('main.html', 'w') + html_file.write(''' + <html> + <body> + <script> + var worker = new Worker('worker.js'); + worker.onmessage = function(event) { + document.write("<hr>Called back by the worker: " + event.data + "<br><hr>"); + }; + </script> + </body> + </html> + ''') + html_file.close() + run_browser('main.html', 'You should see that the worker was called, and said "hello from worker!"') def test_eliminator(self): input = open(path_from_root('tools', 'eliminator', 'eliminator-test.js')).read() |