emscripten: Remove use of EM_ASM from SDL_timer code. Instead use the native emscripten timer API. See https://github.com/emscripten-core/emscripten/issues/17941
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
diff --git a/.github/workflows/emscripten.yml b/.github/workflows/emscripten.yml
index 06aee9e..3e8647c 100644
--- a/.github/workflows/emscripten.yml
+++ b/.github/workflows/emscripten.yml
@@ -9,7 +9,7 @@ jobs:
- uses: actions/checkout@v2
- uses: mymindstorm/setup-emsdk@v10
with:
- version: 2.0.31
+ version: 2.0.32
- name: Configure CMake
run: |
emcmake cmake -S . -B build \
diff --git a/src/timer/SDL_timer.c b/src/timer/SDL_timer.c
index 7b7692d..6741e97 100644
--- a/src/timer/SDL_timer.c
+++ b/src/timer/SDL_timer.c
@@ -375,11 +375,15 @@ SDL_RemoveTimer(SDL_TimerID id)
#else
#include <emscripten/emscripten.h>
+#include <emscripten/eventloop.h>
typedef struct _SDL_TimerMap
{
int timerID;
int timeoutID;
+ Uint32 interval;
+ SDL_TimerCallback callback;
+ void *param;
struct _SDL_TimerMap *next;
} SDL_TimerMap;
@@ -391,18 +395,14 @@ typedef struct {
static SDL_TimerData SDL_timer_data;
static void
-SDL_Emscripten_TimerHelper(SDL_TimerMap *entry, Uint32 interval, SDL_TimerCallback callback, void *param)
+SDL_Emscripten_TimerHelper(void *userdata)
{
- Uint32 new_timeout;
-
- new_timeout = callback(interval, param);
-
- if (new_timeout != 0) {
- entry->timeoutID = EM_ASM_INT({
- return Browser.safeSetTimeout(function() {
- dynCall('viiii', $0, [$1, $2, $3, $4]);
- }, $2);
- }, &SDL_Emscripten_TimerHelper, entry, interval, callback, param);
+ SDL_TimerMap *entry = (SDL_TimerMap*)userdata;
+ entry->interval = entry->callback(entry->interval, entry->param);
+ if (entry->interval > 0) {
+ entry->timeoutID = emscripten_set_timeout(&SDL_Emscripten_TimerHelper,
+ entry->interval,
+ entry);
}
}
@@ -437,12 +437,13 @@ SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *param)
return 0;
}
entry->timerID = ++data->nextID;
+ entry->callback = callback;
+ entry->param = param;
+ entry->interval = interval;
- entry->timeoutID = EM_ASM_INT({
- return Browser.safeSetTimeout(function() {
- dynCall('viiii', $0, [$1, $2, $3, $4]);
- }, $2);
- }, &SDL_Emscripten_TimerHelper, entry, interval, callback, param);
+ entry->timeoutID = emscripten_set_timeout(&SDL_Emscripten_TimerHelper,
+ entry->interval,
+ entry);
entry->next = data->timermap;
data->timermap = entry;
@@ -470,9 +471,7 @@ SDL_RemoveTimer(SDL_TimerID id)
}
if (entry) {
- EM_ASM_({
- window.clearTimeout($0);
- }, entry->timeoutID);
+ emscripten_clear_timeout(entry->timeoutID);
SDL_free(entry);
return SDL_TRUE;