aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-09-17 10:20:57 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-09-17 13:16:39 -0700
commit7ae024b94dbd7c54a22022ec4794f853579d1153 (patch)
treea4f4b336eac3a4631bbc15ea23e671eb98485ca7
parentec631376c3bc6143606e9c340d1ad912917db770 (diff)
add void* argument to emscripten_async_call and emscripten_push_main_loop_blocker
-rw-r--r--src/library_browser.js21
-rw-r--r--system/include/emscripten/emscripten.h26
-rw-r--r--tests/emscripten_api_browser.cpp24
3 files changed, 39 insertions, 32 deletions
diff --git a/src/library_browser.js b/src/library_browser.js
index 43732e5b..5291dcee 100644
--- a/src/library_browser.js
+++ b/src/library_browser.js
@@ -384,7 +384,7 @@ mergeInto(LibraryManager.library, {
if (Browser.mainLoop.queue.length > 0) {
var start = Date.now();
var blocker = Browser.mainLoop.queue.shift();
- blocker.func();
+ blocker.func(blocker.arg);
if (Browser.mainLoop.remainingBlockers) {
var remaining = Browser.mainLoop.remainingBlockers;
var next = remaining%1 == 0 ? remaining-1 : Math.floor(remaining);
@@ -451,13 +451,13 @@ mergeInto(LibraryManager.library, {
Browser.mainLoop.resume();
},
- _emscripten_push_main_loop_blocker: function(func, name) {
- Browser.mainLoop.queue.push({ func: FUNCTION_TABLE[func], name: Pointer_stringify(name), counted: true });
+ _emscripten_push_main_loop_blocker: function(func, arg, name) {
+ Browser.mainLoop.queue.push({ func: FUNCTION_TABLE[func], arg: arg, name: Pointer_stringify(name), counted: true });
Browser.mainLoop.updateStatus();
},
- _emscripten_push_uncounted_main_loop_blocker: function(func, name) {
- Browser.mainLoop.queue.push({ func: FUNCTION_TABLE[func], name: Pointer_stringify(name), counted: false });
+ _emscripten_push_uncounted_main_loop_blocker: function(func, arg, name) {
+ Browser.mainLoop.queue.push({ func: FUNCTION_TABLE[func], arg: arg, name: Pointer_stringify(name), counted: false });
Browser.mainLoop.updateStatus();
},
@@ -467,14 +467,17 @@ mergeInto(LibraryManager.library, {
Browser.mainLoop.updateStatus();
},
- emscripten_async_call: function(func, millis) {
+ emscripten_async_call: function(func, arg, millis) {
Module['noExitRuntime'] = true;
- var asyncCall = Runtime.getFuncWrapper(func);
+ function wrapper() {
+ Runtime.getFuncWrapper(func)(arg);
+ }
+
if (millis >= 0) {
- setTimeout(asyncCall, millis);
+ setTimeout(wrapper, millis);
} else {
- Browser.requestAnimationFrame(asyncCall);
+ Browser.requestAnimationFrame(wrapper);
}
},
diff --git a/system/include/emscripten/emscripten.h b/system/include/emscripten/emscripten.h
index 4d489142..8c42a18a 100644
--- a/system/include/emscripten/emscripten.h
+++ b/system/include/emscripten/emscripten.h
@@ -62,20 +62,20 @@ extern void emscripten_cancel_main_loop();
* at specific time in the future.
*/
#if EMSCRIPTEN
-extern void _emscripten_push_main_loop_blocker(void (*func)(), const char *name);
-extern void _emscripten_push_uncounted_main_loop_blocker(void (*func)(), const char *name);
+extern void _emscripten_push_main_loop_blocker(void (*func)(void *), void *arg, const char *name);
+extern void _emscripten_push_uncounted_main_loop_blocker(void (*func)(void *), void *arg, const char *name);
#else
-inline void _emscripten_push_main_loop_blocker(void (*func)(), const char *name) {
- func();
+inline void _emscripten_push_main_loop_blocker(void (*func)(void *), void *arg, const char *name) {
+ func(arg);
}
-inline void _emscripten_push_uncounted_main_loop_blocker(void (*func)(), const char *name) {
- func();
+inline void _emscripten_push_uncounted_main_loop_blocker(void (*func)(void *), void *arg, const char *name) {
+ func(arg);
}
#endif
-#define emscripten_push_main_loop_blocker(func) \
- _emscripten_push_main_loop_blocker(func, #func)
-#define emscripten_push_uncounted_main_loop_blocker(func) \
- _emscripten_push_uncounted_main_loop_blocker(func, #func)
+#define emscripten_push_main_loop_blocker(func, arg) \
+ _emscripten_push_main_loop_blocker(func, arg, #func)
+#define emscripten_push_uncounted_main_loop_blocker(func, arg) \
+ _emscripten_push_uncounted_main_loop_blocker(func, arg, #func)
/*
* Sets the number of blockers remaining until some user-relevant
@@ -99,11 +99,11 @@ inline void emscripten_set_main_loop_expected_blockers(int num) {}
* mechanism is used.
*/
#if EMSCRIPTEN
-extern void emscripten_async_call(void (*func)(), int millis);
+extern void emscripten_async_call(void (*func)(void *), void *arg, int millis);
#else
-inline void emscripten_async_call(void (*func)(), int millis) {
+inline void emscripten_async_call(void (*func)(void *), void *arg, int millis) {
if (millis) SDL_Delay(millis);
- func();
+ func(arg);
}
#endif
diff --git a/tests/emscripten_api_browser.cpp b/tests/emscripten_api_browser.cpp
index 305265e5..c209550b 100644
--- a/tests/emscripten_api_browser.cpp
+++ b/tests/emscripten_api_browser.cpp
@@ -11,19 +11,22 @@ extern "C" {
bool pre1ed = false;
bool pre2ed = false;
-void pre1() {
+void pre1(void *arg) {
assert(!pre1ed);
assert(!pre2ed);
+ assert((int)arg == 123);
pre1ed = true;
}
-void pre2() {
+void pre2(void *arg) {
assert(pre1ed);
assert(!pre2ed);
+ assert((int)arg == 98);
pre2ed = true;
}
bool fived = false;
-void five() {
+void five(void *arg) {
+ assert((int)arg == 55);
fived = true;
emscripten_resume_main_loop();
}
@@ -33,11 +36,11 @@ void mainey() {
printf("mainey: %d\n", counter++);
if (counter == 20) {
emscripten_pause_main_loop();
- emscripten_async_call(five, 1000);
+ emscripten_async_call(five, (void*)55, 1000);
} else if (counter == 22) { // very soon after 20, so without pausing we fail
assert(fived);
- emscripten_push_main_loop_blocker(pre1);
- emscripten_push_main_loop_blocker(pre2);
+ emscripten_push_main_loop_blocker(pre1, (void*)123);
+ emscripten_push_main_loop_blocker(pre2, (void*)98);
} else if (counter == 23) {
assert(pre1ed);
assert(pre2ed);
@@ -47,7 +50,8 @@ void mainey() {
}
}
-void four() {
+void four(void *arg) {
+ assert((int)arg == 43);
printf("four!\n");
emscripten_set_main_loop(mainey, 0);
}
@@ -56,10 +60,10 @@ void __attribute__((used)) third() {
int now = SDL_GetTicks();
printf("thard! %d\n", now);
assert(fabs(now - last - 1000) < 500);
- emscripten_async_call(four, -1); // triggers requestAnimationFrame
+ emscripten_async_call(four, (void*)43, -1); // triggers requestAnimationFrame
}
-void second() {
+void second(void *arg) {
int now = SDL_GetTicks();
printf("sacond! %d\n", now);
assert(fabs(now - last - 500) < 250);
@@ -81,7 +85,7 @@ int main() {
atexit(never); // should never be called - it is wrong to exit the runtime orderly if we have async calls!
- emscripten_async_call(second, 500);
+ emscripten_async_call(second, (void*)0, 500);
return 1;
}