Summer and ICFP has passed since we announced the ocaml-wasm
organization in July. Here we provide an update from the wasocaml
and wasm_of_ocaml
compiler projects since then.
Introduction
WebAssembly (Wasm) is an attractive compilation target for OCaml, as it holds promises of better web-performance than compiling to JavaScript and furthermore opens up for running OCaml on new platforms. The ocaml-wasm
organization is a joint effort between two Wasm-targeting compiler projects for OCaml:
wasocaml
- an FLambda-based backend for OCaml’s native code compilerwasm_of_ocaml
- a fork ofjs_of_ocaml
that consumes OCaml bytecode and emits Wasm instead of JavaScript
Both compilers depend on the proposed garbage collection (GC) extension to Wasm, which is available behind a flag in development versions of Chrome, Firefox, and Node.js. As a first news item, we are excited to share that the Wasm GC proposal has moved to stage 4. As a consequence Chrome is planning to enable the WasmGC extension in version 119, scheduled to release in November!
wasocaml
- @chambart presented
wasocaml
at the ML workshop in Seattle in early September in connection with ICFP’23- Here’s a video link to the recorded presentation
- The slides and the workshop paper are available from this page with more details about the compiler
- After having benchmarked
wasocaml
, it was noticed that V8 exceptions are too slow right now. The OCaml exception compilation strategy has been changed to avoid using the native Wasm ones for now. This will be turned back into proper Wasm exceptions when the engines improve.
wasm_of_ocaml
@vouillon has made solid progress on implementing run-time bindings, which means that
wasm_of_ocaml
can now
- compile
ocamlc
itself into Wasm - run the Bonsai tests and examples
Early benchmarks results are encouraging: Compiled programs typically run around 10-20% faster compared to the js_of_ocaml
equivalent.
A more detailed status:
- Lots of runtime bindings have been implemented
- for even more parts of the OCaml
Stdlib
: bigarrays, weak pointers, ephemerons, marshaling/unmarshaling - for external packages:
base
,core
,ppx_expect
,ppx_inline_test
,time_now
,incr_dom
,alcotest
,base_bigstring
,core_kernel
,string_dict
,bigstringaf
,js_of_ocaml_patches
,ocaml-cstruct
,Zarith
- for even more parts of the OCaml
- A number of PRs to get
Wasm_of_ocaml
supported:- in
dune
ocaml/dune#8278 - in
gen_js_api
LexiFi/gen_js_api#173 - in
Brr
dbuenzli/brr#51 and - to improve the
js_of_ocaml
-wasm_of_ocaml
compatibility ocsigen/js_of_ocaml#1492
- in
- It is now possible to include JavaScript runtime files and to use JavaScript primitives from Wasm runtime code, reusing the
js_of_ocaml
linker. - Implemented Effect support through CPS transformation for comparison with the current JS promise integration implementation
- Updates contributing to the on-going Wasm GC ecosystem development:
- Make the legacy parser follow more closely the standard GC text format WebAssembly/binaryen#5889
- Update stringref text format WebAssembly/binaryen#5891
- Use the standard syntax for ref.cast, ref.test and array.new_fixed WebAssembly/binaryen#5894
- Fixed a V8 crash https://chromium-review.googlesource.com/c/v8/v8/+/4660286
and implemented a workaround in binaryen tools WebAssembly/binaryen#5799 - Fixed the binaryen linker
wasm-merge
to support exporting an imported module entry WebAssembly/binaryen#5813