LexiFi / gen_js_api

Easy OCaml bindings for Javascript libraries
MIT License
177 stars 31 forks source link

Add support for float enum & float union discriminator #162

Closed cannorin closed 2 years ago

cannorin commented 2 years ago

Related: https://github.com/ocsigen/ts2ocaml/issues/39 Closes #108.

This PR allows float values to be used inside [@js] attribute, which also allows binding to float enums and discriminated union using float value as a discriminator.

This PR also optimizes the generated match statements when they only have a default case with no binding.

  match Ojs.type_of (Ojs.get_prop_ascii x168 "kind") with
- | "number" ->
-    (match Ojs.int_of_js (Ojs.get_prop_ascii x168 "kind") with
-    | _ -> Unknown x168)
+ | "number" -> Unknown x168
cannorin commented 2 years ago

js_of_ocaml-compiler seems to be failing to compile on older versions of OCaml in *nix?

smorimoto commented 2 years ago

Build failure is related to https://github.com/ocsigen/js_of_ocaml/pull/1186 and can be ignored, at least for now.

smorimoto commented 2 years ago

Could you please check this PR? @mlasson

mlasson commented 2 years ago

Ok, will do as soon as possible. Do you need a new release as well ?

cannorin commented 2 years ago

Yes, new release please too 🙂

mlasson commented 2 years ago

I tweaked your "duplicate checks" to forbid:

 type t =
    | A [@js 3]
    | B [@js 3.0]
   [@@js.enum]

and

 type t =
    | A [@js 3]
    | B [@js 3.0000000000000000001]
   [@@js.enum]

but also :

 type t =
    | A [@js 3.0]
    | B [@js 3.0000000000000000001]
   [@@js.enum]
smorimoto commented 2 years ago

Thanks @mlasson @cannorin ❤️