Skip to content

Instantly share code, notes, and snippets.

@mooreryan
Last active April 29, 2022 23:50
Show Gist options
  • Save mooreryan/b9c9236343d2e1b567941f629a213ee7 to your computer and use it in GitHub Desktop.
Save mooreryan/b9c9236343d2e1b567941f629a213ee7 to your computer and use it in GitHub Desktop.
Export OCaml to JavaScript
(executable
(name hello)
(libraries js_of_ocaml)
(modes js)
(preprocess
(pps js_of_ocaml-ppx)))
$ node test.js
11.5
3.5
open Js_of_ocaml
let add1 x = x +. 1.0
let () = Js.export "add1" add1
let () =
Js.export "lib"
(object%js
method add x y = x +. y
end)
// Assuming you run this from the dune-project root.
const hello = require("./_build/default/hello.bc");
console.log(hello.add1(10.5));
console.log(hello.lib.add(1, 2.5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment