aboutsummaryrefslogtreecommitdiff
path: root/src/game.c
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-17 12:48:53 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-17 12:48:53 +0200
commit1aa6444759c2e564f5b99c0cd65f7c2bb134758a (patch)
treea6095c80eed46cc62abf8a709d0a89ff05410466 /src/game.c
parentaa5f6a215d3676a184a5d199acfbe7ee08ba7378 (diff)
Add Trap Handling
We implemented trap handing with setjmp/longjmp. This _should_ be very widely supported, but it was _one_ nicety of WAMR: it did not rely on this mechanism and would instead return errors on runtime calls in case of a trap. The implementation is fairly straightforward: we set a global `jmp_buf` when installing a handler and longjmp in case of a trap. Note that the handler also keeps the trap code, because the C standard only requires `if (setjmp(buf))` to be supported, i.e. "the entire controlling expression of `if`", meaning even if it does return the code passed to `longjmp`, we can't really do anything with it. Added a `Esc` key handler in the PICO-8 stub to check traps work, and they do :).
Diffstat (limited to 'src/game.c')
-rw-r--r--src/game.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/game.c b/src/game.c
index 4803fc0..fa031a0 100644
--- a/src/game.c
+++ b/src/game.c
@@ -126,9 +126,21 @@ bool pw_game_iterate(struct pw_game *game) {
pw_fb_iterate(game->fb);
pw_input_iterate(game->input, game->fb);
+ struct wasm_rt_trap_handler_t handler;
+ if (!wasm_rt_trap_register(&handler)) {
+ return false;
+ }
+
+ if (wasm_rt_trapped(&handler)) {
+ return SDL_SetError("PICO-8 execution trapped: %s",
+ wasm_rt_trap_str(handler.code));
+ }
+
w2c_pico8_dynCall_v(&game->instance, game->env->main_loop);
w2c_pico8_0x5Fmix_0(&game->instance, 42, 1337);
+ wasm_rt_trap_unregister();
+
return true;
}