We are happy to announce the creation of the ocaml-wasm Github organisation which will work towards WebAssembly (Wasm) support for OCaml. The goal of the organisation is to enable users to build high-performance and secure applications for the Wasm platform using the OCaml programming language and the platform tools.
Wasm is a binary instruction format, designed as a portable compilation target for programming languages, enabling deployment on the Web for client and server applications. It provides a sandboxed execution environment, which also makes it attractive for both blockchains and Cloud deployment. For example, Fastly’s Compute@Edge platform is based on WebAssembly.
Compilers
The ocaml-wasm organisation brings together the ongoing efforts at compiling OCaml to Wasm.
wasocaml
OCamlPro has been developing wasocaml, an experimental compiler backend for OCaml that targets Wasm starting from the flambda intermediate representation of the OCaml compiler. OCamlPro engineers have also been contributing to the Wasm GC working group to ensure that the GC and other extensions in Wasm are amenable to targeting OCaml.
wasm_of_ocaml
Alongside this effort, Tarides has been working on implementing wasm_of_ocaml, a toolchain to compile OCaml to WebAssembly based on the battle-tested js_of_ocaml compiler. Both js_of_ocaml and wasm_of_ocaml use OCaml bytecode as the input and emit JavaScript and Wasm respectively.
It is notable that these efforts started off jointly a few years ago with an exploration aimed at adding a Wasm backend to OCaml. At that time, it was clear that Wasm did not have enough features to support OCaml efficiently. With the implementation of several key Wasm extensions in browser engines, the time is right for this effort.
There exist other approaches to running OCaml programs using Wasm runtimes, where the speed of generated code is less of a concern. For example, a relatively straightforward approach is to compile the OCaml bytecode interpreter to Wasm and interpret the bytecode programs. ocamlrun-wasm and wasicaml are ports of the OCaml bytecode interpreter to Wasm. Wasicaml furthermore has a compiler mode somewhat similar to wasm_of_ocaml (but simpler) that parses a bytecode executable and translates it into Wasm.
Evolving Wasm
Wasm is an evolving standard. Wasm 1.0 focuses on low-level support for compiling low-level languages such as C and C++. There are several extensions under development which aim to provide support for targeting high-level languages including garbage collection, tail calls, stack switching and threads. In particular, the Wasm development process is completely open and anyone can participate in the evolution of Wasm.
The OCamlPro engineers contributed to the Wasm GC extension with the help of their experimental flambda to Wasm compiler. Both OCamlPro and Tarides engineers are actively involved with the Wasm development process on multiple fronts. Having more than one approach to compile OCaml to Wasm allows us to build comprehensive evidence and make informed arguments to influence the design of Wasm extensions. In addition, a major implementation effort is the runtime system support for Wasm-compiled OCaml code, which will be shared between the different OCaml to Wasm compilers.
OCaml-Wasm monthly update
We will post a summary of the work done in the ocaml-wasm org on OCaml discuss.
Happy hacking!