js-of-ocamlocsigen

Wrap an OCaml function that receives a JS object (record)


I want to write a very simple function in OCaml, and wrap it to make a JS function jsGet by js_of_ocaml, such that jsGet could take a JS object (or record) as input.

I tried the following code:

\\ in Home.js
function testJsGet () {
    var input = {field_1: 5, field_2: 6};
    var output = jsGet(input);
    document.getElementById("result").value += output;
}

In wrap.ml:

type t =
  < field_1: int Js.prop;
    field_2: int Js.prop
  > Js.t

let () =
  Js.Unsafe.global##.jsGet := Js.wrap_callback
      (fun (r:t) -> r##.field_1);

wrap.ml compiles well, however, running Home.js returns undefined as output.

Does anyone know how to correct this?


Solution

  • Remove underscores. See Method name and underscore at http://ocsigen.org/js_of_ocaml/2.8/manual/bindings