Problem binding bytes with js_of_ocaml

Hi all,

I’ve this problem while creating a binding to a library with external f : Bytes.t -> int type.

The stubs.js function representing f gets called with { t: 2, c: '', l: n } where c is always empty. I was expecting this would work “similarly” to String…

I don’t understand if this my mistake, or Bytes don’t work with js_of_ocaml …

It seems you are applying function f to an uninitialized byte sequence. With Js_of_of_ocaml, byte sequences can be represented in several ways. When the tag t is 2, c contains a prefix of the byte sequence. The remaining bytes are all zeroes.
You should probably call caml_jsbytes_of_string to get the byte sequence as a JavaScript string, caml_array_of_bytes if you prefer an array of bytes, or caml_to_js_string to convert your byte sequence into an UTF-16 JavaScript string.

Is it possible also to update the bytes which are input to the stub? I’ve tried assigning the input var to a new Mlbytes object but this doesn’t seem to work. With a Bigstring it was as simple as looping and updating the contents.

e.g,

function update(dest_bytes, src) {
    dest_bytes = caml_bytes_of_array(src);
    return 0;
}

If you call this function:

    caml_convert_string_to_array (dest_bytes);

then dest_bytes.c is a JavaScript array that contains the byte sequence, and that you can update as you will.

In your case, you can also do the following:

function update(dest_bytes, src) {
    dest_bytes.t = 4 // Array of bytes
    dest_bytes.c = caml_bytes_of_array(src);
    dest_bytes.l = src.l // Make sure the length is correct
    return 0;
}