I’m unsure how to reference or call the generated javascript. It’s basically gibberish.
Say my ocaml is:
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?:
// something like this
// function sort(arr) {}
sort([3,1,2])
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.
The same way you would in a standard OCaml program:
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.
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's FFI cookbook here.
I have tried to do something similar some time ago for timere, in case you want a slightly more complex example
(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)