Commit cfab203f91239a039be49a953242ad07db032b6d

Sam Clegg 2022-09-29T07:36:14

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

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;