This file provides guidance for Claude Code when working with the js_of_ocaml codebase.
Js_of_ocaml (jsoo) is a compiler from OCaml bytecode to JavaScript, allowing pure OCaml programs to run in browsers and Node.js. It also includes wasm_of_ocaml, a compiler targeting WebAssembly.
- Repository: https://github.com/ocsigen/js_of_ocaml
- Maintainers: Ocsigen team
- License: GPL-2.0-or-later AND LGPL-2.1-or-later WITH OCaml-LGPL-linking-exception
# Build everything
dune build @all
# Run all tests
make tests
# Run WebAssembly tests
make tests-wasm
# Format OCaml code
make fmt
# Format JavaScript code
make fmt-js
# Lint JavaScript
make lint-js
# Generate documentation
make doc
# Clean build artifacts
make clean
# Run benchmarks
make bench-
/compiler/- Compiler implementationbin-js_of_ocaml/- js_of_ocaml command entry pointbin-wasm_of_ocaml/- wasm_of_ocaml entry pointbin-jsoo_minify/- JavaScript minifier toollib/- Core compiler librarylib-wasm/- WebAssembly backendlib-cmdline/- Command-line handlingtests-*/- Test suites (jsoo, wasm, compiler, effects, ocaml, full)
-
/lib/- Library packagesjs_of_ocaml/- Main JavaScript binding librarylwt/- Lwt async supporttyxml/- TyXML integrationruntime/- OCaml runtime for JavaScript
-
/ppx/- PPX syntax extensionsppx_js/- JavaScript PPX extensionppx_deriving_json/- JSON derivation PPX
-
/runtime/- JavaScript/WebAssembly runtimejs/- JavaScript runtime modules (.jsfiles with//Provides:///Requires:///If:///Alias:headers;//Provides:accepts flagsconst,mutable,pure,shallowthat affect optimization)wasm/- WebAssembly runtime modules (.wat); JS and Wasm runtimes are parallel — primitives in one usually need a counterpart in the other
-
/examples/- Example projects -
/toplevel/- Web-based OCaml toplevel -
/benchmarks/- Performance benchmarks
opam install --deps-only -t js_of_ocaml js_of_ocaml-lwt \
js_of_ocaml-compiler js_of_ocaml-toplevel js_of_ocaml-ppx \
js_of_ocaml-ppx_deriving_json js_of_ocaml-tyxml
opam install odoc yojson ocp-indent graphics higloRequirements:
- OCaml 4.13 to 5.5
- Dune 3.19+
- For wasm_of_ocaml: Binaryen 119+
- Formatter: ocamlformat 0.29.0 (config in
.ocamlformat) - Line margin: 90 characters
- All compiler files use
open! Stdlibexplicitly - Comprehensive
.mlimodule signatures
- Formatter/Linter: Biome (config in
biome.json)
- GPL-LGPL license header on all source files
- Heavy use of polymorphic variants and GADTs
- Tests use
ppx_expectfor inline snapshot testing and Cram tests (.tfiles)
# Full test suite
make tests
# WebAssembly tests (requires WASM_OF_OCAML=true)
make tests-wasm
# Run specific test
dune runtest compiler/tests-jsoo
# Accept test output changes
dune promotemake tests requires a recent Node.js to be available on PATH.
Test directories:
compiler/tests-jsoo/- JavaScript output testscompiler/tests-wasm_of_ocaml/- WebAssembly testscompiler/tests-compiler/- Compiler unit testscompiler/tests-ocaml/- OCaml compatibility testscompiler/tests-full/- Full integration tests
--effects={disabled,cps,double-translation}- Effect handler support (JS); wasm accepts{disabled,cps,jspi}--target-env={isomorphic,browser,nodejs}- Runtime target (defaultisomorphic)--source-map/--debug-info/--pretty- Debug-friendly output--opt {1,2,3}- Optimization profile (default 1; 3 iterates to fix-point)--enable=OPT/--disable=OPT- Toggle individual options (names incompiler/lib/config.ml), e.g.--enable es6to emit ES6 syntax in generated code--toplevel- Compile a toplevel (link againstjs_of_ocaml-toplevel)
The js_of_ocaml CLI has subcommands compile (default), build-runtime, and link. Whole-program compilation runs compile end-to-end; separate compilation builds the runtime, compiles each .cmo/.cma independently, then links the pieces. Dune picks whole-program for --profile release and separate for dev. See manual/compilation-modes.wiki.
The compiler works by:
- Reading OCaml bytecode (
.cmo/.cmafiles) - Converting to an intermediate representation
- Optimizing and transforming
- Generating JavaScript or WebAssembly output
Key modules in compiler/lib/:
parse_bytecode.ml- Bytecode parsingcode.ml- Internal code representationgenerate.ml- JavaScript code generationdriver.ml- Compilation driver
Wasm backend lives in compiler/lib-wasm/ (e.g. generate.ml, code_generation.ml, link.ml).
- Create branch from
master - Write tests for new code
- Run
make testsbefore submitting - Add a
CHANGES.mdentry under the# devheading, in an appropriate subsection (Features/Changes,Bug fixes, etc.) with a(#NNNN)PR reference - Discuss significant changes via issues first