Js_of_ocaml is composed of multiple packages:
- js_of_ocaml-compiler, the compiler.
- js_of_ocaml-ppx, a ppx syntax extension.
- js_of_ocaml, the base library.
- js_of_ocaml-lwt, lwt support.
- js_of_ocaml-tyxml, tyxml support.
Note: All code examples in this manual use Js_of_ocaml's ppx syntax. It is, however, possible to use Js_of_ocaml purely as a compiler while using a different package (e.g. gen_js_api, brr) to provide bindings to the browser APIs.
The easiest way to install js_of_ocaml is to use opam. opam install js_of_ocaml js_of_ocaml-ppx js_of_ocaml-lwt
For alternatives, see Install.
ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml-ppx \ -linkpkg -o cubes.byte cubes.ml
with ocamlbuild and oasis
Js_of_ocaml also provide an ocamlbuild plugin. See js_of_ocaml-ocamlbuild>>.
You can find an OCaml toplevel running in the browser here.
Most of the OCaml standard library is supported. However,
- Most of Sys module is not supported.
Extra libraries distributed with Ocaml (such as Thread) are not supported in general. However,
- Bigarray: bigarray are supported using Typed Arrays
- Num: supported
- Str: supported
- Graphics: partially supported using canvas (see also js_of_ocaml-lwt.graphics)
- Unix: time related functions are supported
Tail call is not optimized in general. However, mutually recursive functions are optimized:
- self recursive functions (when the tail calls are the function itself) are compiled using a loop.
- trampolines are used otherwise. More about tail call optimization.
Effect handlers are fully supported with the --enable=effects flag. This is not the default for now since effects are not widely used at the moment and the generated code can be slower, larger and less readable.
- marshalling of floats is not supported (unmarshalling works);
- the polymorphic hash function will not give the same results on datastructures containing floats;
- these functions may be more prone to stack overflow.