assert: Better Emscripten support. (Better than nothing, that is.) Fixes Bugzilla #3459.
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
diff --git a/src/SDL_assert.c b/src/SDL_assert.c
index 67710d4..c64d626 100644
--- a/src/SDL_assert.c
+++ b/src/SDL_assert.c
@@ -44,6 +44,11 @@
#endif
#endif
+#if defined(__EMSCRIPTEN__)
+#include <emscripten.h>
+#endif
+
+
static SDL_assert_state
SDL_PromptAssertion(const SDL_assert_data *data, void *userdata);
@@ -119,6 +124,10 @@ static SDL_NORETURN void SDL_ExitProcess(int exitcode)
{
#ifdef __WIN32__
ExitProcess(exitcode);
+#elif defined(__EMSCRIPTEN__)
+ emscripten_cancel_main_loop(); /* this should "kill" the app. */
+ emscripten_force_exit(exitcode); /* this should "kill" the app. */
+ exit(exitcode);
#else
_exit(exitcode);
#endif
@@ -221,9 +230,45 @@ SDL_PromptAssertion(const SDL_assert_data *data, void *userdata)
state = (SDL_assert_state)selected;
}
}
-#ifdef HAVE_STDIO_H
+
else
{
+#if defined(__EMSCRIPTEN__)
+ /* This is nasty, but we can't block on a custom UI. */
+ for ( ; ; ) {
+ SDL_bool okay = SDL_TRUE;
+ char *buf = (char *) EM_ASM_INT({
+ var str =
+ Pointer_stringify($0) + '\n\n' +
+ 'Abort/Retry/Ignore/AlwaysIgnore? [ariA] :';
+ var reply = window.prompt(str, "i");
+ if (reply === null) {
+ reply = "i";
+ }
+ return allocate(intArrayFromString(reply), 'i8', ALLOC_NORMAL);
+ }, message);
+
+ if (SDL_strcmp(buf, "a") == 0) {
+ state = SDL_ASSERTION_ABORT;
+ /* (currently) no break functionality on Emscripten
+ } else if (SDL_strcmp(buf, "b") == 0) {
+ state = SDL_ASSERTION_BREAK; */
+ } else if (SDL_strcmp(buf, "r") == 0) {
+ state = SDL_ASSERTION_RETRY;
+ } else if (SDL_strcmp(buf, "i") == 0) {
+ state = SDL_ASSERTION_IGNORE;
+ } else if (SDL_strcmp(buf, "A") == 0) {
+ state = SDL_ASSERTION_ALWAYS_IGNORE;
+ } else {
+ okay = SDL_FALSE;
+ }
+ free(buf);
+
+ if (okay) {
+ break;
+ }
+ }
+#elif defined(HAVE_STDIO_H)
/* this is a little hacky. */
for ( ; ; ) {
char buf[32];
@@ -250,8 +295,8 @@ SDL_PromptAssertion(const SDL_assert_data *data, void *userdata)
break;
}
}
- }
#endif /* HAVE_STDIO_H */
+ }
/* Re-enter fullscreen mode */
if (window) {