From d89db979b79909ed1fba28317f3b0e237304737e Mon Sep 17 00:00:00 2001 From: Jim Huang Date: Sat, 7 Mar 2026 02:50:11 +0800 Subject: [PATCH] Fix WASM mouse by restoring g_wasm_ctx each frame The g_wasm_ctx global (BSS-resident, initialized to NULL) was lost across Emscripten's main-loop unwind transition when emscripten_set_main_loop_arg() exits main() via throw "unwind" with -sASYNCIFY. This caused all exported input injection functions (iui_wasm_mouse_button, iui_wasm_mouse_motion, etc.) called from JavaScript event handlers to silently bail out, making mouse interaction non-functional. The ctx pointer passed through the port function table remains valid (heap-allocated via calloc), so re-establish g_wasm_ctx at the start of wasm_poll_events() each frame before JS event callbacks can reference it. --- ports/wasm.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ports/wasm.c b/ports/wasm.c index 7e705fd..d56fed8 100644 --- a/ports/wasm.c +++ b/ports/wasm.c @@ -288,6 +288,15 @@ static bool wasm_poll_events(iui_port_ctx *ctx) if (!ctx) return false; + /* Re-establish global context each frame. + * After emscripten_set_main_loop_arg() exits main() via throw "unwind" + * with -sASYNCIFY, the g_wasm_ctx global (BSS-resident, initialized to + * NULL) is lost across the main-loop unwind transition. The ctx passed + * through the port function table remains valid (heap-allocated), so we + * restore the global here before JS event callbacks reference it. + */ + g_wasm_ctx = ctx; + /* Update delta time */ double now = emscripten_get_now(); ctx->delta_time = (float) (now - ctx->last_frame_time) / 1000.f;