[ANN] Wasicaml - code emitter for WebAssembly

Hello,

just want to repeat here what I already wrote in caml-list ([Caml-list] [ANN] wasicaml - a code emitter for OCaml targeting WebAssembly) - there is now a code generator for WebAssembly at

which is highly experimental. Any feedback welcome.

Gerd

12 Likes

This looks extremely cool. Does this support (or plan to support) calling into the generated module from js, or calling external functions from OCaml?

I went ahead and made a minimal demo for browsers (copy-pasting the JavaScript from the repository plus a small wasi shim): GitHub - copy/wasicaml-demo

You can see it in action here: https://copy.sh/wasicaml-demo/

1 Like

For interfacing with JS (or the host environment in general) it is best to have some minimal C wrapper, e.g. put this into mylib.c:

__attribute__((import_module("env")))
__attribute__((import_name("myFunction")))
value myFunction(value arg1);

and then declare on the OCaml side as usual, e.g. external myFunction : int -> int = "myFunction".

You can link mylib.o into the final executable, e.g. wasicaml ... -cclib mylib.o.

If you want to call OCaml from JS, you’d have to register callbacks.

Hope this helps (I can make a bigger and more complete example if needed).

Nice demo - but it took a while until I understood that the only effect is that it writes something into the console.

@gstolpmann Thanks. I got both OCaml-to-JS and JS-to-OCaml calls working now. Code is in the repo if anybody is interested.

I’ve pushed a simple canvas demo (a naive implementation of conway’s game of life): https://copy.sh/wasicaml-demo/ (code).

And also a jsoo version for comparison: https://copy.sh/wasicaml-demo/jsoo.html

At the moment the jsoo version seems to be a bit faster and significantly smaller (not surprising considering this is still experimental). Still quite impressive, especially the fact that C code can be linked and works as expected.

1 Like