aboutsummaryrefslogtreecommitdiff
path: root/src/env.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/env.c')
-rw-r--r--src/env.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/env.c b/src/env.c
index e64b3fb..fd67e98 100644
--- a/src/env.c
+++ b/src/env.c
@@ -116,6 +116,33 @@ bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context) {
return true;
}
+bool pw_env_shutdown(struct w2c_env *env) {
+ if (!env) {
+ return SDL_InvalidParamError("env");
+ }
+
+ if (env->data.state.shutdown) {
+ return true;
+ }
+
+ // HERE BE DRAGONS... Taking a look at the Web Assembly module exported
+ // by PICO-8, it calls `env_emscripten_asm_iiiiiiiii(10, ...)` with the
+ // `request_shutdown` flag read from global memory offset `2220668`. For
+ // whatever reason, the `SHUTDOWN` menu flag is setting this location in
+ // memory to a non-zero value, but without calling the ASM const. In order
+ // to correctly support shutting down, we need to not only read the flag
+ // from the ASM const, but also this memory offset. This is tied to the
+ // PICO-8 version, so whenever it gets updated, just:
+ //
+ // grep -B2 'asm_const_iiiiiiiii' build/system/pico8.c
+ //
+ // And check the address that is being loaded for the last parameter to the
+ // state update ASM const call.
+
+ uint32_t value;
+ return wasm_rt_memory_load32_checked(&env->memory, 2220668, &value) && value;
+}
+
/**
* Convenience macro for writing to wasm memory and moving the wasm pointer
* forward for that amount.