github.com/ocsigen/js_of_ocaml

Compiler from OCaml to Javascript.

Open this visualization on its own page →

Contributors

128

Lines of Code

12,244

From

2010-02-01

To

2020-12-15

About ocsigen/js_of_ocaml

Js_of_ocaml is a compiler that translates OCaml bytecode into JavaScript, enabling pure OCaml programs to run in web browsers and Node.js environments. The compiler integrates seamlessly with existing OCaml installations without requiring libraries to be recompiled, and it includes extensive bindings for browser APIs. Generated JavaScript programs typically execute faster than the OCaml bytecode interpreter, and the approach of compiling from bytecode rather than retargeting the OCaml compiler itself makes the project easier to maintain long-term.

The project is organized as a collection of packages including the core compiler, PPX syntax extensions, a base library, support for popular OCaml libraries like Lwt and Tyxml, and tools for building an interactive OCaml toplevel in JavaScript. A related component called Wasm_of_ocaml compiles OCaml bytecode to WebAssembly. Most of the OCaml standard library is supported, including Bigarray through typed arrays, Str, Num, and partial Graphics support, though threading and some system modules are not available.

Data representation differs from standard OCaml to match JavaScript's native types: integers are 32-bit rather than 31 or 63-bit, floats are unboxed, and aggregate types are represented as arrays with tags. The compiler supports both modern JavaScript (ECMAScript 6 and later) and handles advanced features like effect handlers and tail call optimization for mutually recursive functions using trampolines.

Share this video