assert: Better Emscripten support.
authorRyan C. Gordon <icculus@icculus.org>
Fri, 19 May 2017 14:51:03 -0400
changeset 11017411bf9f7f908
parent 11016 5326f7660eee
child 11018 1f151426c2ca
assert: Better Emscripten support.

(Better than nothing, that is.)

Fixes Bugzilla #3459.
src/SDL_assert.c
     1.1 --- a/src/SDL_assert.c	Fri May 19 14:49:16 2017 -0400
     1.2 +++ b/src/SDL_assert.c	Fri May 19 14:51:03 2017 -0400
     1.3 @@ -44,6 +44,11 @@
     1.4  #endif
     1.5  #endif
     1.6  
     1.7 +#if defined(__EMSCRIPTEN__)
     1.8 +#include <emscripten.h>
     1.9 +#endif
    1.10 +
    1.11 +
    1.12  static SDL_assert_state
    1.13  SDL_PromptAssertion(const SDL_assert_data *data, void *userdata);
    1.14  
    1.15 @@ -119,6 +124,10 @@
    1.16  {
    1.17  #ifdef __WIN32__
    1.18      ExitProcess(exitcode);
    1.19 +#elif defined(__EMSCRIPTEN__)
    1.20 +    emscripten_cancel_main_loop();  /* this should "kill" the app. */
    1.21 +    emscripten_force_exit(exitcode);  /* this should "kill" the app. */
    1.22 +    exit(exitcode);
    1.23  #else
    1.24      _exit(exitcode);
    1.25  #endif
    1.26 @@ -221,9 +230,45 @@
    1.27              state = (SDL_assert_state)selected;
    1.28          }
    1.29      }
    1.30 -#ifdef HAVE_STDIO_H
    1.31 +
    1.32      else
    1.33      {
    1.34 +#if defined(__EMSCRIPTEN__)
    1.35 +        /* This is nasty, but we can't block on a custom UI. */
    1.36 +        for ( ; ; ) {
    1.37 +            SDL_bool okay = SDL_TRUE;
    1.38 +            char *buf = (char *) EM_ASM_INT({
    1.39 +                var str =
    1.40 +                    Pointer_stringify($0) + '\n\n' +
    1.41 +                    'Abort/Retry/Ignore/AlwaysIgnore? [ariA] :';
    1.42 +                var reply = window.prompt(str, "i");
    1.43 +                if (reply === null) {
    1.44 +                    reply = "i";
    1.45 +                }
    1.46 +                return allocate(intArrayFromString(reply), 'i8', ALLOC_NORMAL);
    1.47 +            }, message);
    1.48 +
    1.49 +            if (SDL_strcmp(buf, "a") == 0) {
    1.50 +                state = SDL_ASSERTION_ABORT;
    1.51 +            /* (currently) no break functionality on Emscripten
    1.52 +            } else if (SDL_strcmp(buf, "b") == 0) {
    1.53 +                state = SDL_ASSERTION_BREAK; */
    1.54 +            } else if (SDL_strcmp(buf, "r") == 0) {
    1.55 +                state = SDL_ASSERTION_RETRY;
    1.56 +            } else if (SDL_strcmp(buf, "i") == 0) {
    1.57 +                state = SDL_ASSERTION_IGNORE;
    1.58 +            } else if (SDL_strcmp(buf, "A") == 0) {
    1.59 +                state = SDL_ASSERTION_ALWAYS_IGNORE;
    1.60 +            } else {
    1.61 +                okay = SDL_FALSE;
    1.62 +            }
    1.63 +            free(buf);
    1.64 +
    1.65 +            if (okay) {
    1.66 +                break;
    1.67 +            }
    1.68 +        }
    1.69 +#elif defined(HAVE_STDIO_H)
    1.70          /* this is a little hacky. */
    1.71          for ( ; ; ) {
    1.72              char buf[32];
    1.73 @@ -250,8 +295,8 @@
    1.74                  break;
    1.75              }
    1.76          }
    1.77 +#endif /* HAVE_STDIO_H */
    1.78      }
    1.79 -#endif /* HAVE_STDIO_H */
    1.80  
    1.81      /* Re-enter fullscreen mode */
    1.82      if (window) {