diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-07-12 18:26:46 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-07-12 18:26:46 -0700 |
commit | 3b6025a2c41faa2bdbe3dae354d5be439d78e38c (patch) | |
tree | d834771d424539976762961588be72caaba51469 | |
parent | 42f93a46a60f2c4408a700aadc5d3fa104ae757e (diff) |
emscripten_push_main_loop_blocker
-rw-r--r-- | src/library_browser.js | 16 | ||||
-rw-r--r-- | system/include/emscripten/emscripten.h | 6 | ||||
-rw-r--r-- | tests/emscripten_api_browser.cpp | 22 |
3 files changed, 40 insertions, 4 deletions
diff --git a/src/library_browser.js b/src/library_browser.js index 075bd8bf..4c26037e 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -12,6 +12,7 @@ mergeInto(LibraryManager.library, { scheduler: null, shouldPause: false, paused: false, + queue: [], pause: function() { Browser.mainLoop.shouldPause = true; }, @@ -220,6 +221,11 @@ mergeInto(LibraryManager.library, { var jsFunc = FUNCTION_TABLE[func]; var wrapper = function() { + if (Browser.mainLoop.queue.length > 0) { + Browser.mainLoop.queue.shift()(); + Browser.mainLoop.scheduler(); + return; + } if (Browser.mainLoop.shouldPause) { // catch pauses from non-main loop sources Browser.mainLoop.paused = true; @@ -247,19 +253,23 @@ mergeInto(LibraryManager.library, { Browser.mainLoop.scheduler(); }, - emscripten_cancel_main_loop: function(func) { + emscripten_cancel_main_loop: function() { Browser.mainLoop.scheduler = null; Browser.mainLoop.shouldPause = true; }, - emscripten_pause_main_loop: function(func) { + emscripten_pause_main_loop: function() { Browser.mainLoop.pause(); }, - emscripten_resume_main_loop: function(func) { + emscripten_resume_main_loop: function() { Browser.mainLoop.resume(); }, + emscripten_push_main_loop_blocker: function(func) { + Browser.mainLoop.queue.push(FUNCTION_TABLE[func]); + }, + emscripten_async_call: function(func, millis) { Module['noExitRuntime'] = true; diff --git a/system/include/emscripten/emscripten.h b/system/include/emscripten/emscripten.h index 5b71ce6a..575c3e2a 100644 --- a/system/include/emscripten/emscripten.h +++ b/system/include/emscripten/emscripten.h @@ -49,6 +49,12 @@ extern void emscripten_resume_main_loop(); extern void emscripten_cancel_main_loop(); /* + * Add a function to a queue of events that will execute + * before the main loop will continue. + */ +extern void emscripten_push_main_loop_blocker(void (*func)()); + +/* * Call a C function asynchronously, that is, after returning * control to the JS event loop. This is done by a setTimeout. * When building natively this becomes a simple direct call, diff --git a/tests/emscripten_api_browser.cpp b/tests/emscripten_api_browser.cpp index 20e2dbb5..305265e5 100644 --- a/tests/emscripten_api_browser.cpp +++ b/tests/emscripten_api_browser.cpp @@ -9,6 +9,19 @@ int last = 0; extern "C" { +bool pre1ed = false; +bool pre2ed = false; +void pre1() { + assert(!pre1ed); + assert(!pre2ed); + pre1ed = true; +} +void pre2() { + assert(pre1ed); + assert(!pre2ed); + pre2ed = true; +} + bool fived = false; void five() { fived = true; @@ -22,7 +35,14 @@ void mainey() { emscripten_pause_main_loop(); emscripten_async_call(five, 1000); } else if (counter == 22) { // very soon after 20, so without pausing we fail - int result = fived; + assert(fived); + emscripten_push_main_loop_blocker(pre1); + emscripten_push_main_loop_blocker(pre2); + } else if (counter == 23) { + assert(pre1ed); + assert(pre2ed); + printf("Good!\n"); + int result = 1; REPORT_RESULT(); } } |