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

@JasonGross JasonGross commented May 6, 2026

Copy link
Copy Markdown
Contributor

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?

@hhugo hhugo force-pushed the wasm-lifecycle-events branch from b0ea27b to e5fc28c Compare May 6, 2026 13:01
@hhugo

hhugo commented Jul 11, 2026

Copy link
Copy Markdown
Member

re-looking at this, I wonder if we should have a symmetric behavior in the js_of_ocaml side so that it's easier to switch between backend

JasonGross and others added 2 commits July 11, 2026 16:46
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>
@JasonGross JasonGross force-pushed the wasm-lifecycle-events branch from 2b9ee02 to cedc3a4 Compare July 11, 2026 17:05
Mirror the Wasm launcher: JavaScript executables now dispatch
jsoocaml:loaded / jsoocaml:error CustomEvents on globalThis when their
toplevel initialization completes or fails, so surrounding JavaScript
can wait for the program the same way with either backend. Separate
compilation units, --wrap-with-fun output, build-runtime and build-fs
scripts are unaffected.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KW1ybsago5w9iAgbjah2kf
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

4 participants