diff options
author | Alon Zakai <alonzakai@gmail.com> | 2014-03-06 11:41:41 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2014-03-06 11:42:43 -0800 |
commit | 20d5ffb5925d57a5facacce453d4643e8809929d (patch) | |
tree | 0fdfca1be241a1792ceaf5c275b674f744a313e2 | |
parent | 1ac8ea0859d91d86c23b5574bd0474887df1b96e (diff) |
add docs and testing for mem init file being async
-rwxr-xr-x | emcc | 11 | ||||
-rw-r--r-- | tests/mem_init.cpp | 24 | ||||
-rw-r--r-- | tests/test_browser.py | 17 |
3 files changed, 51 insertions, 1 deletions
@@ -470,7 +470,16 @@ Options that are modified or new in %s include: 1: Emit a separate memory initialization file in binary format. This is more efficient than storing it as text inside JavaScript, but does - mean you have another file to publish. + mean you have another file to publish. The + binary file will also be loaded asynchronously, + which means main() will not be called until + the file is downloaded and applied; you cannot + call any C functions until it arrives. (Call + yourself from main() to know when all async + stuff has happened and it is safe to call + library functions, as main() will only be + called at that time. You can also call + addOnPreMain from a preRun.) -Wno-warn-absolute-paths If not specified, the compiler will warn about any uses of absolute paths in -I and -L command line diff --git a/tests/mem_init.cpp b/tests/mem_init.cpp new file mode 100644 index 00000000..e642bfc9 --- /dev/null +++ b/tests/mem_init.cpp @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <emscripten.h> + +extern "C" { + +int noted = 0; + +void EMSCRIPTEN_KEEPALIVE note(int n) { + EM_ASM_({ Module.print([$0, $1]) }, n, noted); + noted = noted | n; + EM_ASM_({ Module.print(['noted is now', $0]) }, noted); + if (noted == 3) { + int result = noted; + REPORT_RESULT(); + } +} + +} + +int main() { + EM_ASM( myJSCallback() ); // calls a global JS func + return 0; +} + diff --git a/tests/test_browser.py b/tests/test_browser.py index dfb1ef69..7ffe56c8 100644 --- a/tests/test_browser.py +++ b/tests/test_browser.py @@ -1752,6 +1752,23 @@ void *getBindBuffer() { for mem in [0, 1]: self.btest('pre_run_deps.cpp', expected='10', args=['--pre-js', 'pre.js', '--memory-init-file', str(mem)]) + def test_mem_init(self): + open(os.path.join(self.get_dir(), 'pre.js'), 'w').write(''' + function myJSCallback() { // called from main() + Module._note(1); + } + Module.preRun = function() { + addOnPreMain(function() { + Module._note(2); + }); + }; + ''') + open(os.path.join(self.get_dir(), 'post.js'), 'w').write(''' + Module._note(4); // this happens too early! and is overwritten when the mem init arrives + ''') + + self.btest('mem_init.cpp', expected='3', args=['--pre-js', 'pre.js', '--post-js', 'post.js', '--memory-init-file', '1']) + def test_worker_api(self): Popen([PYTHON, EMCC, path_from_root('tests', 'worker_api_worker.cpp'), '-o', 'worker.js', '-s', 'BUILD_AS_WORKER=1', '-s', 'EXPORTED_FUNCTIONS=["_one"]']).communicate() self.btest('worker_api_main.cpp', expected='566') |