I am new to JavaScript, I am trying to use js_of_ocaml
.
I first wrote a very simple cubes.ml
:
let simple (a: int) =
a + 1234
Then complied it:
ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml.syntax \
-syntax camlp4o -linkpkg -o cubes.byte cubes.ml
then generated the JavaScript file:
js_of_ocaml cubes.byte
Here is the generated cubes.js. Note that we could not find 1234
or function name simple
in that file.
I have another JavaScript file Home.js
, where I want the function callSimple
to call what was generated in cubes.js
. But I don't know how to write it. Could anyone help?
(function () {
...
function callSimple(a) {
return ???;
};
...
})();
Edit 1:
I tried the solution proposed by @EdgarAroutiounian :
(* cubes.ml *)
let () =
Js.Unsafe.global##.jscode := (object%js
val simple = Js.wrap_meth_callback
(fun a -> a + 1234)
val speak = Js.wrap_meth_callback
(fun () -> print_endline "hello")
end)
It did compile, but it did not return the right output:
If I write in home.js
:
confirm(jscode.simple(10)); // 1244 is expected
confirm(jscode.speak()); // "hello" as string is expected
the first line returns function (a){return p(c,aM(b,a))}
, and the second line returns 0
. They are not what I expect.