Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JasonGross opened this issue Nov 23, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@JasonGross
Copy link
Contributor

Describe the bug
In
fiat-html.tar.gz (unpack the tar.gz, run python -m http.server 8000 in the fiat-html directory, and then navigate to http://localhost:8000/?argv=%5B%22-h%22%5D&interactive&wasm in Chrome), the code fails with "ReferenceError: synthesize is not defined". But if you change the setTimeout in main.js to be 1000 instead of 1, so that it waits a full second, then the code works fine.

The .tar.gz contains the standalone wasm_fiat_crypto.ml used to generate the wasm with -package unix -w -20 -g -package js_of_ocaml -linkpkg and then wasm_of_ocaml --source-map --no-inline.

Expected behavior
All exported functions should be available by the time DOMContentLoaded fires

Versions
Chrome Version 119.0.6045.160 (Official Build) (64-bit)
ocaml-option-fp.1,ocaml-variants.4.14.1+options

$ wasm_of_ocaml --version
5.3.0+git-f517bbca-dirty
$ ocamlc --version
4.14.1
@vouillon
Copy link
Collaborator

I don't think there is any way to fix that since compiling and instantiating WebAssembly code is asynchronous.

Your Wasm code should somehow signal when it is ready.

@JasonGross
Copy link
Contributor Author

Could the generated .js file be made to trigger some event when the wasm code is ready? It seems like the cleanest place to do this is using the Promise of the async function in the generated .js wrapper, you could insert something like

.then((value) => {
  dispatchEvent(new CustomEvent('wasmCodeLoaded', {detail: {src}} ));
}).catch((error) => {
  dispatchEvent(new CustomEvent('wasmCodeLoadingError', {detail: {src, error}}));
})

And perhaps wasm_of_ocaml could take command line arguments for naming these events?

It seems cleaner to me to do it in the autogenerated javascript code wrapper, and matches more closely with js_of_ocaml behavior, rather than requiring the source program to add some extra code to deal with this.

JasonGross added a commit to JasonGross/fiat-crypto that referenced this issue Nov 23, 2023
JasonGross added a commit to JasonGross/fiat-crypto that referenced this issue Nov 23, 2023
JasonGross added a commit to JasonGross/fiat-crypto that referenced this issue Nov 23, 2023
@JasonGross JasonGross changed the title [BUG] wasm is not ready when DOMContentLoaded fires [BUG] wasm is not ready when DOMContentLoaded fires and does not trigger an event when it is loaded Nov 23, 2023
JasonGross added a commit to JasonGross/fiat-crypto that referenced this issue Nov 23, 2023
JasonGross added a commit to JasonGross/fiat-crypto that referenced this issue Nov 24, 2023
JasonGross added a commit to JasonGross/fiat-crypto that referenced this issue Nov 24, 2023
JasonGross added a commit to JasonGross/fiat-crypto that referenced this issue Nov 24, 2023
JasonGross added a commit to JasonGross/fiat-crypto that referenced this issue Nov 24, 2023
JasonGross added a commit to JasonGross/fiat-crypto that referenced this issue Nov 24, 2023
JasonGross added a commit to mit-plv/fiat-crypto that referenced this issue Nov 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants