Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here

js_of_ocaml

Compiler from OCaml to Javascript.

Subscribe to updates I use js_of_ocaml


Statistics on js_of_ocaml

Number of watchers on Github 395
Number of open issues 48
Average time to close an issue 18 days
Main language OCaml
Average time to merge a PR 9 days
Open pull requests 32+
Closed pull requests 10+
Last commit over 1 year ago
Repo Created about 6 years ago
Repo Last Updated over 1 year ago
Size 114 MB
Homepage http://ocsigen.or...
Organization / Authorocsigen
Latest Release3.0.0
Contributors45
Page Updated
Do you use js_of_ocaml? Leave a review!
View open issues (48)
View js_of_ocaml activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating js_of_ocaml for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

Build Status

Js_of_ocaml (jsoo)

Js_of_ocaml is a compiler from OCaml bytecode to JavaScript. It makes it possible to run pure OCaml programs in JavaScript environment like browsers and Node.js.

  • It is easy to install and use as it works with an existing installation of OCaml, with no need to recompile any library.
  • It comes with bindings for a large part of the browser APIs.
  • According to our benchmarks, the generated programs runs typically faster than with the OCaml bytecode interpreter.
  • We believe this compiler will prove much easier to maintain than a retargeted OCaml compiler, as the bytecode provides a very stable API.

Requirements

Findlib, cppo See opam file for version constraints.

optional

Toplevel requirements

  • base64, tyxml, reactiveData
  • ocp-indent: needed to support indentation in the toplevel
  • higlo: needed to support Syntax highlighting in the toplevel
  • cohttp: needed to build the toplevel webserver

Installation

Opam

opam install js_of_ocaml js_of_ocaml-ppx

Manual

  • edit Makefile.conf to change the default configuration
  • run make all to compile
  • run make install as root to install the compiler and its libraries
  • run make uninstall as root to uninstall them

You can run make toplevel-examples if you want to build a Web-based OCaml toplevel as well. Try the toplevel

Usage

Your program must first be compiled using the OCaml bytecode compiler ocamlc. JavaScript bindings are provided by the js_of_ocaml package. The syntax extension is provided by js_of_ocaml.syntax package.

ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml.ppx -linkpkg -o cubes.byte cubes.ml

Then, run the js_of_ocaml compiler to produce JavaScript code:

js_of_ocaml cubes.byte

Features

Most of the OCaml standard library is supported. However,

  • Weak semantic cannot be implemented using JavaScript. A dummy implementation is provided.
  • Most of the Sys module is not supported.

Extra libraries distributed with Ocaml (such as Thread or Str) are not supported in general. However,

  • Bigarray: bigarrays are supported using Typed Arrays
  • Num: supported using +nat.js option
  • Graphics: partially supported using canvas (see js_of_ocaml.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.

Data representation differs from the usual one. Most notably, integers are 32 bits (rather than 31 bits or 63 bits), which is their natural size in JavaScript, and floats are not boxed. As a consequence, marshalling, polymorphic comparison, and hashing functions can yield results different from usual:

  • 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.

Toplevel

Contents of the distribution

Filename Description
LICENSE license and copyright notice
README this file
compiler/ compiler
examples/ small examples
lib/ library for interfacing with JavaScript APIs
ppx/ ppx syntax extensions
camlp4/ camlp4 syntax extensions
runtime/ runtime system
toplevel/ web-based OCaml toplevel
ocamlbuild/ ocamlbuild plugin for js_of_ocaml
js_of_ocaml open issues Ask a question     (View All Issues)
  • almost 3 years Build system does not recompile executables when code in compiler/lib changes
  • almost 3 years Use .exe extension when using native Windows ports
  • almost 3 years Cannot disable findlib support
  • almost 3 years README does not mention dependency on Yojson
  • almost 3 years Evaluation ocaml code in the browser
  • almost 3 years Possible wrong code generated with Js.Optdef.map and localStorage
  • almost 3 years `make -C tests` fails
  • almost 3 years Svg elements or not compatible with eventTarget
  • about 3 years Discussion: raise exception and catch exception
  • about 3 years escape vs encodeURIComponent
  • about 3 years ES6 Support
  • about 3 years Labelled arguments
  • about 3 years large memory usage on 4.03.0 compilation
  • about 3 years suggestion: use `#=` instead of `:=` for js property set
  • about 3 years `Json.unsafe_input` and MlStrings
  • about 3 years Better runtime support for nodejs
  • over 3 years [syntax] ideas: having `#?` property ready
  • over 3 years Why are window##.{inner,outer}{Width,Height} optdef?
  • over 3 years Add selection* attributes to appropriate input elements
  • over 3 years XmlHTTPRequest and POST requests to send raw data
  • over 3 years Run tests from the ocaml compiler testsuite
  • over 3 years Stdio not handled well
  • almost 4 years Distribute the ocamlbuild plugin separately
  • almost 4 years Marshaling loops on cyclic values
  • about 4 years Better inlining ?
  • about 4 years Implement jsoo constructor deriver.
  • about 4 years did js_of_ocaml try to explore google closure compiler advanced mode?
  • about 4 years indexedDB API
  • about 4 years The values of mouse_x and mouse_y in Graphics_js.status are not correct on Chrome
  • about 4 years fix nat.js
js_of_ocaml open pull requests (View All Pulls)
  • add origin method to Dom_html.location
  • store/access javascript error (with backtrace) within ocaml exceptions.
  • Add Tyxml_js.register
  • XmlHttpRequest: API change for perform functions.
  • Initial support for async (WIP)
  • Switch to ocamlbuild (and topkg)
  • Switch to OASIS
  • Prevent inlining to loop on a block that immediately branches to itself
  • Fix `Js.string` for deserialized strings
  • Added Lwt_js_events.{aborts,errors,loads}.
  • Alternative interface for JsooTop.
  • Dom_html: Add selection attributes to textarea.
  • Compat with Tyxml 4.0
  • Small changes wrt compiler-libs.
  • Dom_html: add crossorigin to linkElement.
  • Add is_null function
  • Build deriving_[Jj]son.cm* even without deriving package
  • (wip) Import new ppx scheme from let-def
  • fixed a typo
  • Export: echo --> cat
  • (WIP)
  • windo##open_ returns possibly null value
  • Split js_of_ocaml (WIP)
  • 2.8.4
  • rework version handling
  • Lib: split js_of_ocaml-core (EXPERIMENT)
  • Change type of Dom_html.storageEvent
  • Compiler: support for OCaml GPR#1568
  • Build: wrap js_of_ocaml lib
  • Add progressive web app support
  • Compiler: improve arity approximation
  • Internationalization API: https://www.ecma-international.org/ecma-402…
js_of_ocaml questions on Stackoverflow (View All Questions)
  • How to use properly compile OCaml programs with js_of_ocaml?
  • Meaning of # character in front of js_of_ocaml dom types
  • A canvas with a horizontal scroll bar in js_of_ocaml
  • How can I add a onclick method using js_of_ocaml?
  • Js_of_ocaml - Error when get cookie
  • Js_of_ocaml : missing primitive when calling ocurl and str
  • How to call a js_of_ocaml method?
  • How to construct a js object and call its methods with js_of_ocaml?
  • js_of_ocaml and Deriving_Json
  • How to use modules with js_of_ocaml?
  • js_of_ocaml and Core
  • Can js_of_ocaml compile in-memory data-structures from the OCaml compiler?
  • js_of_ocaml and event_listener
  • Code completion not working with OcaIde + js_of_ocaml
  • Code completion not working for js_of_ocaml in typerex
  • Marshal and magic_copy in js_of_ocaml
js_of_ocaml list of languages used
js_of_ocaml latest release notes
3.0.0 Js_of_ocaml version 3.0.0

Version 3.0.0

Features/Changes

  • Misc: switch to ppx instead of camlp4
  • Misc: always install deriving_json
  • Misc: remove support for async_kernel, bin_prot
  • Misc: remove support for ppx_driver
  • Misc: split js_of_ocaml in multiple packages
  • Misc: switch to jbuilder
  • Misc: new pseudo filesystem support with better node support
  • Compiler: sourcemap support is now optional
  • Compiler: no more strong dep on yojson, b64, menhir, ocamllex
  • Compiler: improve separate compilation workflow
  • Runtime: add support for weakdef
  • Syntaxes: add expect tests
  • Syntaxes: switch to ocaml-migrate-parsetree and ppx_tools_versioned
  • Lib: Add primitives in [Js.Unsafe]
  • Lib: remove keycode module, replaced by Dom_html.Keyboard_code
  • Lib: XmlHttpRequest, API change for perform functions.

Bug fixes:

  • Runtime: fix many stubs
  • Lib: fix many signatures
  • Compiler: fix performance of compilation passes (simpl,clean)
  • Compiler: fix compat with OCaml 4.06
  • Toplevel: flush channels after execution.
  • Toplevel: fix toplevel generation when using -export-unit
2.8.3 Js_of_ocaml version 2.8.3
2.8.2 Js_of_ocaml version 2.8.2
Other projects in OCaml