diff options
author | Alon Zakai <alonzakai@gmail.com> | 2014-02-18 17:46:30 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2014-02-18 17:56:13 -0800 |
commit | 133b7ebbdd39cfa04231d39e84ee178db9dd527b (patch) | |
tree | ba2d148890b471b851d6bb5d3607c350007ffddb | |
parent | 9a864d50be044455bcb7626898e58abc406215d9 (diff) |
add a version of emscripten_set_main_loop where the main loop takes an argument; fixes #2114
-rw-r--r-- | src/library_browser.js | 13 | ||||
-rw-r--r-- | system/include/emscripten/emscripten.h | 4 | ||||
-rw-r--r-- | tests/emscripten_api_browser.cpp | 16 |
3 files changed, 29 insertions, 4 deletions
diff --git a/src/library_browser.js b/src/library_browser.js index 50a8fc6f..46082281 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -752,7 +752,7 @@ mergeInto(LibraryManager.library, { document.body.appendChild(script); }, - emscripten_set_main_loop: function(func, fps, simulateInfiniteLoop) { + emscripten_set_main_loop: function(func, fps, simulateInfiniteLoop, arg) { Module['noExitRuntime'] = true; Browser.mainLoop.runner = function Browser_mainLoop_runner() { @@ -803,7 +803,11 @@ mergeInto(LibraryManager.library, { } try { - Runtime.dynCall('v', func); + if (typeof arg !== 'undefined') { + Runtime.dynCall('vi', func, [arg]); + } else { + Runtime.dynCall('v', func); + } } catch (e) { if (e instanceof ExitStatus) { return; @@ -843,6 +847,11 @@ mergeInto(LibraryManager.library, { } }, + emscripten_set_main_loop_arg__deps: ['emscripten_set_main_loop'], + emscripten_set_main_loop_arg: function(func, arg, fps, simulateInfiniteLoop) { + _emscripten_set_main_loop(func, fps, simulateInfiniteLoop, arg); + }, + emscripten_cancel_main_loop: function() { Browser.mainLoop.scheduler = null; Browser.mainLoop.shouldPause = true; diff --git a/system/include/emscripten/emscripten.h b/system/include/emscripten/emscripten.h index 426206c4..b92d920c 100644 --- a/system/include/emscripten/emscripten.h +++ b/system/include/emscripten/emscripten.h @@ -116,6 +116,9 @@ extern void emscripten_async_load_script(const char *script, void (*onload)(), v * asynchronous callbacks, but you must pause the main * loop until they complete. * + * If you want your main loop function to receive a void* + * argument, use emscripten_set_main_loop_arg. + * @simulate_infinite_loop If true, this function will throw an * exception in order to stop execution of the caller. This * will lead to the main loop being entered instead of code @@ -134,6 +137,7 @@ extern void emscripten_async_load_script(const char *script, void (*onload)(), v */ #if EMSCRIPTEN extern void emscripten_set_main_loop(void (*func)(), int fps, int simulate_infinite_loop); +extern void emscripten_set_main_loop_arg(void (*func)(void*), void *arg, int fps, int simulate_infinite_loop); extern void emscripten_pause_main_loop(); extern void emscripten_resume_main_loop(); extern void emscripten_cancel_main_loop(); diff --git a/tests/emscripten_api_browser.cpp b/tests/emscripten_api_browser.cpp index 18046ca3..a03b71ed 100644 --- a/tests/emscripten_api_browser.cpp +++ b/tests/emscripten_api_browser.cpp @@ -31,6 +31,18 @@ void five(void *arg) { emscripten_resume_main_loop(); } +void argey(void* arg) { + static int counter = 0; + assert((int)arg == 17); + counter++; + printf("argey: %d\n", counter); + if (counter == 5) { + emscripten_cancel_main_loop(); + int result = 1; + REPORT_RESULT(); + } +} + void mainey() { static int counter = 0; printf("mainey: %d\n", counter++); @@ -45,8 +57,8 @@ void mainey() { assert(pre1ed); assert(pre2ed); printf("Good!\n"); - int result = 1; - REPORT_RESULT(); + emscripten_cancel_main_loop(); + emscripten_set_main_loop_arg(argey, (void*)17, 0, 0); } } |