diff options
author | Alon Zakai <alonzakai@gmail.com> | 2014-01-09 18:18:05 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2014-01-09 18:18:05 -0800 |
commit | dfd9cf8cc063158cbaeecc81bc0e4b27ef6bec20 (patch) | |
tree | f30bb2c6b9912b455cd913a44131fc82e1f9c1f9 | |
parent | bcce3b5fb4528a9a10f85cdcd11cf169a84350cd (diff) |
prevent calling run more than once; fixes #1992
-rw-r--r-- | src/postamble.js | 7 | ||||
-rw-r--r-- | tests/test_other.py | 32 |
2 files changed, 35 insertions, 4 deletions
diff --git a/src/postamble.js b/src/postamble.js index 63495914..d6c059b8 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -117,16 +117,15 @@ function run(args) { preRun(); - if (runDependencies > 0) { - // a preRun added a dependency, run will be called later - return; - } + if (runDependencies > 0) return; // a preRun added a dependency, run will be called later + if (Module['calledRun']) return; // run may have just been called through dependencies being fulfilled just in this very frame function doRun() { ensureInitRuntime(); preMain(); + assert(!Module['calledRun']); Module['calledRun'] = true; if (Module['_main'] && shouldRunNow) { Module['callMain'](args); diff --git a/tests/test_other.py b/tests/test_other.py index c5f0c583..f91b4683 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -2219,3 +2219,35 @@ mergeInto(LibraryManager.library, { process.communicate() assert(os.path.isfile(outdir + 'hello_world.obj')) + + def test_doublestart_bug(self): + open('code.cpp', 'w').write(r''' +#include <stdio.h> +#include <emscripten.h> + +void main_loop(void) { + static int cnt = 0; + if (++cnt >= 10) emscripten_cancel_main_loop(); +} + +int main(void) { + printf("This should only appear once.\n"); + emscripten_set_main_loop(main_loop, 10, 0); + return 0; +} +''') + + open('pre.js', 'w').write(r''' +if (typeof Module === 'undefined') Module = eval('(function() { try { return Module || {} } catch(e) { return {} } })()'); +if (!Module['preRun']) Module['preRun'] = []; +Module["preRun"].push(function () { + Module['addRunDependency']('test_run_dependency'); + Module['removeRunDependency']('test_run_dependency'); +}); +''') + + Popen([PYTHON, EMCC, 'code.cpp', '--pre-js', 'pre.js']).communicate() + output = run_js(os.path.join(self.get_dir(), 'a.out.js'), engine=NODE_JS) + + assert output.count('This should only appear once.') == 1, '\n'+output + |