Skip to content

Wasm: dispatch lifecycle events when async instantiation completes#2232

Open
JasonGross wants to merge 3 commits into
ocsigen:masterfrom
JasonGross:wasm-lifecycle-events
Open

Wasm: dispatch lifecycle events when async instantiation completes#2232
JasonGross wants to merge 3 commits into
ocsigen:masterfrom
JasonGross:wasm-lifecycle-events

Conversation

@JasonGross
Copy link
Copy Markdown
Contributor

@JasonGross JasonGross commented May 6, 2026

Summary

Test plan

  • make lint-js passes (only an unrelated biome.json schema-version notice)
  • dune build runtime/wasm succeeds
  • dune build compiler/lib compiler/lib-wasm compiler/bin-wasm_of_ocaml compiler/bin-js_of_ocaml runtime succeeds (verifies the free-variable check accepts CustomEvent)
  • make tests — could not run locally; this switch lacks ppx_expect and other test-only dependencies. CI should cover it.
  • In a browser, a wasmoocaml:loaded event fires on globalThis after the Wasm runtime finishes initializing
  • When instantiation throws, a wasmoocaml:error event fires before the error propagates

🤖 Generated with Claude Code

@JasonGross
Copy link
Copy Markdown
Contributor Author

I assume all of the CI failures are spurious?

JasonGross and others added 2 commits May 6, 2026 15:01
Compiling and instantiating the generated WebAssembly is asynchronous,
but the launcher only returns a Promise that callers typically don't
await. Surrounding JavaScript that runs on DOMContentLoaded therefore
has no way to tell when the OCaml exports are actually ready. Dispatch
'wasmoocaml:loaded' (and 'wasmoocaml:error' on failure) CustomEvents
on globalThis so external code can wait for the runtime to be ready.

Ported from ocaml-wasm/wasm_of_ocaml#143.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The free-variable check in compiler/bin-wasm_of_ocaml/gen/gen.ml flagged
the new CustomEvent reference in runtime.js. CustomEvent is a standard
DOM/Node global comparable to XMLHttpRequest and DOMException already in
this list.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@hhugo hhugo force-pushed the wasm-lifecycle-events branch from b0ea27b to e5fc28c Compare May 6, 2026 13: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.

[BUG] wasm is not ready when DOMContentLoaded fires and does not trigger an event when it is loaded

3 participants