I’m pleased to announce the joint release of js_of_ocaml 6.0.1 and wasm_of_ocaml.
Js_of_ocaml is a compiler from OCaml bytecode to JavaScript. It makes it possible to run pure OCaml programs in JavaScript environment like browsers and Node.js.
Wasm_of_ocaml is a compiler from OCaml bytecode to WebAssembly. It is highly compatible with Js_of_ocaml, so you can compile your programs with wasm_of_ocaml instead of js_of_ocaml and experience overall better performance. It is supported by Dune 3.17, making the switch very easy.
Most significant changes in js_of_ocaml:
The conversion between Javascript numbers and OCaml floats is now explicit, using functions Js.float and Js.to_float (this is necessary for wasm_of_ocaml which does not use the same representation for JS numbers and OCaml floats)
Dom_html has been modernized, removing some no longer relevant Js.optdef type annotations
Effects:
add an optional feature of “dynamic switching” between CPS and direct style, resulting in better performance when no effect handler is installed
Regarding wasm_of_ocaml, Tarides also just posted a blog post with some more details about its recent developments, and what kind of performance gains have been observed with it.
I want to insist that building your project to Wasm can be as simple as enabling the wasm mode in dune. To quote the documentation page cited by Jérôme:
(executable (name foo) (modes wasm))
And then request the .wasm.js target:
$ dune build ./foo.bc.wasm.js
$ node _build/default/foo.bc.wasm.js
hello from wasm
Congratz on the release!
I can’t find anything about the state of wasm and ctypes. Is it (will it be) possible to link ocaml code to wasm-c-code, compiled through emscripten? I’m thinking specifically about something like raylib.
I would be somewhat surprised if it supported direct C-to-WASM interop. Most other WASM toolchains require you to go through JS (by including module imports when instantiating WASM). As far as I’m aware, there’s not even a WASM spec for “linking” against other WASM libs, so you’d be reverse engineering the details of Emscripten, etc.