# Js\_of\_ocaml -\> calling the translated javascript

**URL:** https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603
**Category:** Learning
**Created:** [April 5, 2021, 3:32am UTC](https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603 "2021-04-05T03:32:09Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Geoff\_Langenderfer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/geoff_langenderfer/32/2847_2.png) [@Geoff\_Langenderfer](https://discuss.ocaml.org/u/Geoff_Langenderfer)
#### Post date: [April 5, 2021, 3:32am UTC](https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603/1 "2021-04-05T03:32:09Z")

</div>

I’m unsure how to reference or call the generated javascript. It’s basically gibberish.

Say my ocaml is:

```ocaml
let rec sort = function
   | [] -> []
   | x :: l -> insert x (sort l)
and insert elem = function
   | [] -> [elem]
   | x :: l -> if elem < x then elem :: x :: l
               else x :: insert elem l;;

```

how would I call the translated javascript function?:

```javascript
// something like this
// function sort(arr) {}
sort([3,1,2])

```

---

<div class="post-metadata">

### Author: ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)
#### Post date: [April 5, 2021, 3:48am UTC](https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603/2 "2021-04-05T03:48:26Z")

</div>

~~Js\_of\_ocaml is not really designed to be callable from JavaScript. Its output is a low-level ‘assembly-like’ version of JavaScript. This output is meant to be the final application that runs in the browser, not a library that’s called by other JavaScript consumers.~~

> [@Js\_of\_ocaml -\> calling the translated javascript](https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603/6):
>
> You can definitely call Js\_of\_OCaml code from JavaScript. You just need to wrap it in an exported JS module. See [Export OCaml code to JavaScript](https://ocsigen.org/js_of_ocaml/latest/manual/rev-bindings). I haven’t checked, but you will probably have to add conversions from JS arrays to OCaml lists in your OCaml code.

---

<div class="post-metadata">

### Author: ![silene](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/silene/32/2707_2.png) [@silene](https://discuss.ocaml.org/u/silene)
#### Post date: [April 5, 2021, 3:55am UTC](https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603/3 "2021-04-05T03:55:25Z")

</div>

> [@Geoff\_Langenderfer](#):
>
> how would I call this?:
> 
> ```auto
> sort([3,1,2])
> 
> ```

The same way you would in a standard OCaml program:

```ocaml
let () =
  ignore (sort [3,1,2])

```

Now, for a less toy example, it would depend on what the target is. For example, if the code is meant to be embedded in a webpage, the `let () = ...` code would perhaps take care of registering a callback on an `onclick` event of a button. Again, because everything is performed on the OCaml side, whether the resulting Javascript is gibberish does not matter.

---

<div class="post-metadata">

### Author: ![Geoff\_Langenderfer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/geoff_langenderfer/32/2847_2.png) [@Geoff\_Langenderfer](https://discuss.ocaml.org/u/Geoff_Langenderfer)
#### Post date: [April 5, 2021, 3:59am UTC](https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603/4 "2021-04-05T03:59:36Z")

</div>

so, say I was expecting to have some javascript function:

```auto
function sort(arr) {}

```

There’s no way to call the translated ocaml?

---

<div class="post-metadata">

### Author: ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)
#### Post date: [April 5, 2021, 4:01am UTC](https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603/5 "2021-04-05T04:01:16Z")

</div>

~~From JavaScript? No, not that I know of.~~

> [@Js\_of\_ocaml -\> calling the translated javascript](https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603/6):
>
> You can definitely call Js\_of\_OCaml code from JavaScript. You just need to wrap it in an exported JS module. See [Export OCaml code to JavaScript](https://ocsigen.org/js_of_ocaml/latest/manual/rev-bindings). I haven’t checked, but you will probably have to add conversions from JS arrays to OCaml lists in your OCaml code.

But from another OCaml source file in the same project? That’s trivial, just call it like a normal cross-module call.

---

<div class="post-metadata">

### Author: ![mnxn](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mnxn/32/2445_2.png) [@mnxn](https://discuss.ocaml.org/u/mnxn)
#### Post date: [April 5, 2021, 4:38am UTC](https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603/6 "2021-04-05T04:38:28Z")

</div>

You can definitely call Js\_of\_OCaml code from JavaScript. You just need to wrap it in an exported JS module.  
See [Export OCaml code to JavaScript](https://ocsigen.org/js_of_ocaml/latest/manual/rev-bindings).

I haven’t checked, but you will probably have to add conversions from JS arrays to OCaml lists in your OCaml code.

---

<div class="post-metadata">

### Author: ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)
#### Post date: [April 5, 2021, 6:36am UTC](https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603/7 "2021-04-05T06:36:15Z")

</div>

As other have mentioned that is perfectly possible.

But if the function you export is acting on OCaml values you will have to write a stub to convert from the JavaScript types you’d find natural to provide to the OCaml values your OCaml function expects.

In addition to @mnxn’s reference this is also lightly touched on in [`Brr`](https://erratique.ch/software/brr)'s FFI cookbook [here](https://erratique.ch/software/brr/doc/ffi_cookbook.html#export).

---

<div class="post-metadata">

### Author: ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)
#### Post date: [April 5, 2021, 1:32pm UTC](https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603/8 "2021-04-05T13:32:14Z")

</div>

My mistake. Totally forgot about the export feature.

---

<div class="post-metadata">

### Author: ![Geoff\_Langenderfer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/geoff_langenderfer/32/2847_2.png) [@Geoff\_Langenderfer](https://discuss.ocaml.org/u/Geoff_Langenderfer)
#### Post date: [April 5, 2021, 5:12pm UTC](https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603/9 "2021-04-05T17:12:23Z")

</div>

Thanks so much for your help.

I’m hitting a few potholes along the way. I’m unable to run the example at [http://ocsigen.org/js\_of\_ocaml/dev/manual/rev-bindings:](http://ocsigen.org/js_of_ocaml/dev/manual/rev-bindings:)

```auto
$ cat math.ml
let _ =
  Js.export_all
    (object%js
      method add x y = x +. y
      method abs x = abs_float x
      val zero = 0.
     end)
$ ocamlfind ocamlc -verbose \
  -package js_of_ocaml -package js_of_ocaml-ppx \
  -linkpkg math.ml -o math.byte
Effective set of compiler predicates: pkg_bytes,pkg_uchar,pkg_js_of_ocaml,pkg_js_of_ocaml-ppx,autolink,byte
+ ocamlc.opt -verbose -o math.byte -I /home/g/.opam/default/lib/bytes -I /home/g/.opam/default/lib/uchar -I /home/g/.opam/default/lib/js_of_ocaml -I /home/g/.opam/default/lib/js_of_ocaml-ppx -ppx "/home/g/.opam/default/lib/js_of_ocaml-ppx/./ppx.exe --as-ppx" /home/g/.opam/default/lib/js_of_ocaml/js_of_ocaml.cma math.ml
+ /home/g/.opam/default/lib/js_of_ocaml-ppx/./ppx.exe --as-ppx '/tmp/camlppx16679a' '/tmp/camlppx8a9bc9'
File "math.ml", line 2, characters 2-15:
2 | Js.export_all
      ^^^^^^^^^^^^^
Error: Unbound module Js
ocamlc.opt returned with exit code 2
$ ocaml -version
The OCaml toplevel, version 4.11.1
$ opam --version
2.0.8
$ ocamlc -version
4.11.1
$ uname -a
Linux manjaro 5.9.16-1-MANJARO #1 SMP PREEMPT Mon Dec 21 22:00:46 UTC 2020 x86_64 GNU/Linux

```

---

<div class="post-metadata">

### Author: ![Geoff\_Langenderfer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/geoff_langenderfer/32/2847_2.png) [@Geoff\_Langenderfer](https://discuss.ocaml.org/u/Geoff_Langenderfer)
#### Post date: [April 5, 2021, 5:21pm UTC](https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603/10 "2021-04-05T17:21:17Z")

</div>

> [@Geoff\_Langenderfer](#):
>
> `Error: Unbound module Js`

I changed `math.ml` in the following way to resolve this:

```auto
(*math.ml*)
-
+ open Js_of_ocaml

let _ =
  Js.export_all
    (object%js
      method add x y = x +. y
      method abs x = abs_float x
      val zero = 0.
     end)

```

```auto
// index.js
var math = require('./math.js');
console.log(math.add(2,3))

```

It now compiles:

```auto
$ node index.js
5

```

---

<div class="post-metadata">

### Author: ![darrenldl](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/darrenldl/32/291_2.png) [@darrenldl](https://discuss.ocaml.org/u/darrenldl)
#### Post date: [April 6, 2021, 7:06am UTC](https://discuss.ocaml.org/t/js-of-ocaml-calling-the-translated-javascript/7603/11 "2021-04-06T07:06:06Z")

</div>

I have tried to do something similar some time ago for timere, in case you want a slightly more complex [example](https://github.com/daypack-dev/timere/blob/main/export-js-tzdb-full/core.ml)

(I guess the main takeaway would be if you want to expose a `Seq.t`, it’s easier to make it into a generator function, and store the `Seq.t` in a `ref`, see `method resolve`)
