I have defined an OCaml function manipulate: string -> string
. And I wrap it to make a JS function jsManipulate
:
let () =
Js.Unsafe.global##.jsManipulate := Js.wrap_callback
(fun s -> Js.string (manipulate (Js.to_string s)))
Now, I want to make manipulate
return more information than just a string; I want it to return a record: manipulate: string -> myrecord
where myrecord = { result: string; info_1: int; info_2: bool }
in OCaml format.
In this case, does anyone know how to wrap manipulate
to make a JS function that returns also a JS record (or object?) from a JS string?
PS: I want to call manipulate
only one time (rather than 3 times) to build the JS object.
Regular javascript is not going to be able to understand OCaml's record, even when code is compiled in javascript with js_of_ocaml.
What you should use instead is a javascript object (There is no difference between a record and an object in javascript). You can read about creating literal objects at the bottom of this page: http://ocsigen.org/js_of_ocaml/2.7/api/Ppx_js
You can create a function to_object
of type myrecord -> < .. > Js.t
that returns an object. Then you can use it for wrapping instead of Js.to_string
.