package js_of_ocaml

  1. Overview
  2. Docs
Compiler from OCaml bytecode to JavaScript

Install

Dune Dependency

Authors

Maintainers

Sources

js_of_ocaml-6.1.1.tbz
sha256=d31da41aae61c02aaa8b4d504e4e93705233d303cd81a07bb4ac5eedb03d6014
sha512=207c6c80447a79fa13e5dbd6baf2837ec91b9d81952328ae91c3496ca30f095fce49faa09af8125602f1ae6f2f6e0c391178454dcb39b9907b5a3712bad3fcee

doc/README_wasm_of_ocaml.html

Wasm_of_ocaml

Wasm_of_ocaml is an alternative compiler which compiles OCaml bytecode to WebAssembly.

Supported engines

The generated code works with Chrome 119, Node.js 22, Firefox 122, and Safari 18.2 (or more recent versions of these applications).

In particular, the output code requires the following Wasm extensions to run:

OCaml 5.x code using effect handlers can be compiled in two different ways: one can enable the CPS transformation from js_of_ocaml by passing the --effects=cps flag. Without the flag wasm_of_ocaml will instead default to --effects=jspi and emit code utilizing

Installation and usage

Installation and usage documentation can be found in the js_of_ocaml manual.

Running the test suite

The following commands can be used to set up an opam switch and run the test suite.

opam switch create wasm-tests 4.14.0
eval $(opam env --switch=wasm-tests)
opam pin add -n base.v0.16.1 git@github.com:ocaml-wasm/base#wasm
opam pin add -n time_now.v0.16.1 git@github.com:ocaml-wasm/time_now#wasm
opam pin add -n ppx_inline_test.v0.16.1 git@github.com:ocaml-wasm/ppx_inline_test#wasm
opam pin add -n ppx_expect.v0.16.1 git@github.com:ocaml-wasm/ppx_expect#wasm
opam pin add -y -n --with-version 6.0.0 .
opam install . --deps-only --with-test
WASM_OF_OCAML=true dune build @runtest-wasm

Compatibility with Js_of_ocaml

Since the value representation is different, some adaptations are necessary.

The most notable change is that, except for integers, OCaml numbers are no longer mapped to JavaScript numbers. So, explicit conversions Js.to_float and Js.float are now necessary to convert between OCaml floats and JavaScript numbers. The typing of JavaScript Typed Arrays has also been changed to deal with this.

OCaml

Innovation. Community. Security.