brr icon indicating copy to clipboard operation
brr copied to clipboard

Compatibility with Wasm_of_ocaml

Open vouillon opened this issue 2 years ago • 9 comments

Wasm_of_ocaml uses a different representation for OCaml floats (and 32-bit integers) and JavaScript numbers, so we need explicit conversions (which are still optimized away by Js_of_ocaml).

Also, since JavaScript values get boxed, physical equality can no longer be used to test whether a value is undefined.

This relies on additional runtime primitives introduced in https://github.com/ocsigen/js_of_ocaml/pull/1492.

The changes are rather simple and backward compatible, except for typed arrays.

vouillon avatar Jul 25 '23 12:07 vouillon

Thanks!

I'm not sure I got the change on typed array. Does it mean we now need to use Jv.t values to access most of the types (e.g. on a Tarray.fill). That feels a bit wrong why do we loose the OCaml types (The former 'a parameter now shifted to 'b) ?

Also just noting for myself that this bit of doc regarding equalities will need updating.

dbuenzli avatar Jul 26 '23 21:07 dbuenzli

I'm not very happy with the change on typed array, but I'm not sure how to do any better.

OCaml numbers and JavaScript numbers do not coincide, except for 31-bit integers. Tarray.fill takes a JavaScript number as argument, so in most cases, the best type we have for this argument is Jv.t.

vouillon avatar Jul 27 '23 09:07 vouillon

Right but I mean we do have the types in Bigarray which are implemented via typed arrays. So I don't understand why we can't have them in Brr.Tarray like before.

dbuenzli avatar Jul 27 '23 09:07 dbuenzli

One could dispatch on the constructor's name to perform the appropriate conversion. But this is going to add a lot of overhead to Tarray.get and Tarray.set. But maybe we can add specialized variants?

vouillon avatar Jul 27 '23 10:07 vouillon

But maybe we can add specialized variants?

That could be an option, or maybe simply deprecate these function and ask people to convert to bigarrays for operations involving elements. Presumably this will then use bigarray primitives which have no overhead ? And we can then avoid adding this new parameter to Tarray.t ?

dbuenzli avatar Jul 27 '23 20:07 dbuenzli

The bigarray primitives have less overhead, indeed. They make a dispatch based on the kind of the bigarray (which is an integer), rather that on the name of the typed array constructor.

vouillon avatar Jul 31 '23 10:07 vouillon

So, I have inserted appropriate conversions instead of changing the types.

vouillon avatar Aug 01 '23 15:08 vouillon

@vouillon what's the status of this ? I might do a release in the upcoming weeks.

dbuenzli avatar May 14 '24 08:05 dbuenzli

@dbuenzli You need to review the changes regarding typed arrays. It should be ready to merge otherwise.

vouillon avatar May 23 '24 12:05 vouillon