State of OCaml and web assembly

wasm

#1

I spent some time studying various ways the OCaml compiler can produce web assembly and interpret or depend on other web assembly libraries. The landscape is quite confusing. Does anyone have experience with OCaml and web assembly? Is there a reliable project to bet on and start developing with? What about interop with other libraries that compile to wasm and you want to reference in OCaml? Here’s a list of the projects I have discovered which seem relevant. Would appreciate insights on whether they are being maintained, or if anybody has noteworthy experience with them:

This is a build script for building the OCaml bytecode interpreter for WebAssembly using emscripten.

An OCaml library to read and write Web Assembly (wasm) files and manipulate their AST.

From WebAssembly to Native Code via the OCaml Backend.

A fork of the ocaml compiler with a web assembly compile target.

Grain is a language built for the modern web by leveraging the brilliant work done by the WebAssembly project.

While grain is not actually OCaml it seems to be a fork of the OCaml codebase and thus worthy of mention.


#2

This is actually the reference implementation of the interpreter/encoder/decoder/validator. Repo: https://github.com/WebAssembly/spec


#3

@ostera Does this imply, as least in theory, you could use the forked ocaml compiler that targets web assembly in combination with the wasm ocaml reference implementation to bind to existing arbitrary web assembly libraries?

I assume the answer is yes but it would be cumbersome without some ctypes-esque abstraction built on top of it.


#4

@struktured: my understanding is that wasm, besides a reference parser/printer of wasm code, provides a wasm interpreter, that is a slow-ish implementation of the language. It does allow to do what you say (run a mix of OCaml and wasm code), but by interpreting the wasm fragments, which is probably not the approach you had in mind.

If you want to compile OCaml to wasm and link to wasm code and have both run on a fast (eg. jit-ed) environment, then the intepreter in wasm won’t be of use. The code-inspection aspect may be useful for some sort of binding generation. But the biggest building block you need is a solid webassembly backend for the OCaml compiler, and I don’t know how mature the existing one is.


#5

@gasche I see, thanks for the clarity.


#6

For now I would recommend against using the wasm backend that I’m working on. It’s still a work in progress. The goal is definitely to merge with trunk at some point - but it’s not there yet.

The backend uses LLVM’s LLD to make it possible to link to other wasm object files. I’m definitely not an expert at making OCaml backends, so I wouldn’t be surprised if there are things that I’m doing that aren’t correct. I definitely try though (in spare time). Just recently I managed to correctly compile the stdlib to wasm without type errors, and actually run very basic applications.

However for now I would recommend against using OCaml when targeting webassembly as certain features: exceptions, tail calls, and gc are not yet supported. I have my eye on these features though, and will add support when these come available.

People who want to help out are of course welcome.


#7

Btw. the correct branch is: