Skip to content

Fix WASM mouse by restoring g_wasm_ctx each frame#25

Merged
jserv merged 1 commit intomainfrom
fix-wasm
Mar 6, 2026
Merged

Fix WASM mouse by restoring g_wasm_ctx each frame#25
jserv merged 1 commit intomainfrom
fix-wasm

Conversation

@jserv
Copy link
Contributor

@jserv jserv commented Mar 6, 2026

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.


Summary by cubic

Restore mouse input in WASM by re-establishing g_wasm_ctx at the start of each frame in wasm_poll_events. This fixes the lost global after Emscripten’s ASYNCIFY main-loop unwind, which caused JS-driven input functions to no-op.

Written for commit d89db97. Summary will update on new commits.

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.
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

@jserv jserv merged commit 5cf8eb0 into main Mar 6, 2026
12 checks passed
@jserv jserv deleted the fix-wasm branch March 6, 2026 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant