Wasm: dispatch lifecycle events when async instantiation completes#143
Closed
JasonGross wants to merge 1 commit into
Closed
Wasm: dispatch lifecycle events when async instantiation completes#143JasonGross wants to merge 1 commit into
JasonGross wants to merge 1 commit into
Conversation
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. Fixes ocaml-wasm#11. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
3 tasks
JasonGross
added a commit
to mit-plv/fiat-crypto
that referenced
this pull request
May 5, 2026
) The wasm_of_ocaml launcher loads and instantiates the wasm module asynchronously, then runs the OCaml entry point that calls `Js.export "synthesize" ...`. The previous fixed 1s setTimeout in the worker raced against this and produced `ReferenceError: synthesize is not defined` on the deployed site (the wasm blob is now ~13.8 MB and routinely takes longer to load than 1s). Replace the timeout with a 50ms poll for `self.synthesize`, and also subscribe to the `wasmoocaml:loaded` / `wasmoocaml:error` events introduced in ocaml-wasm/wasm_of_ocaml#143 so we install the handler immediately on success and surface load failures back to the page instead of looping forever. The loaded handler logs a note that the polling fallback can be removed once the deployed wasm_of_ocaml ships these events.
Contributor
|
You probably want to open a PR against https://github.com/ocsigen/js_of_ocaml |
Contributor
Author
|
Thank you, doing that now |
6 tasks
Contributor
Author
|
Superseded by ocsigen/js_of_ocaml#2232, which ports this change to the merged js_of_ocaml repository. Closing here. |
hhugo
pushed a commit
to JasonGross/js_of_ocaml
that referenced
this pull request
May 6, 2026
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DOMContentLoadedcan race against the OCaml exports being defined.init_fun(primitives)(runtime_arguments)and discards the returned Promise, so callers can't simply await it. Instead, the runtime now dispatches awasmoocaml:loadedCustomEventonglobalThisafter_initialize()resolves, and awasmoocaml:errorevent (with the thrown error indetail.error) if anything in the instantiation pipeline rejects.detail.src(the assets directory name passed inargs.src) so listeners can disambiguate when multiple Wasm modules are loaded on the same page.Test plan
node --check runtime/wasm/runtime.jspasses (syntax check).npx -p @biomejs/biome@1.9.1 biome check runtime/wasm/runtime.jspasses.fiat-htmlreproducer from [BUG] wasm is not ready when DOMContentLoaded fires and does not trigger an event when it is loaded #11: replace thesetTimeout(..., 1)with awasmoocaml:loadedlistener and confirmsynthesizeis defined when the handler fires.🤖 Generated with Claude Code