LexiFi / gen_js_api

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

UTF-8 support for (Ojs.get/set/delete) adaptions #135

Closed mlasson closed 3 years ago

mlasson commented 3 years ago

See Issue #104.

The change in semantics happened when ocsigen/js_of_ocaml#997 was merged but they never added the primitive proposed in #104. It means that since 3.7.0 "Ojs.get/set/delete" is broken when applied to non ascii keys (which is not the end of the world; I am not aware of any API with non ascii characters...).

I propose a new solution to be able to close the issue: I deprecated these functions and introduced :

external get_prop_ascii: t -> string -> t = "caml_js_get"
  (** Get the property from an object (only works if the property key is a plain ascii string). *)

external set_prop_ascii: t -> string -> t -> unit = "caml_js_set"
  (** Set an object property (only works if the property key is a plain ascii string). *)

external delete_prop_ascii: t -> string -> unit = "caml_js_delete"
  (** Delete an object property (only works if the property key is a plain ascii string). *)

external get_prop: t -> t -> t = "caml_js_get"
  (** Get the property from an object. *)

external set_prop: t -> t -> t -> unit = "caml_js_set"
  (** Set an object property. *)

external delete_prop: t -> t -> unit = "caml_js_delete"
  (** Delete an object property. *)

The ppx now use the ascii version only on ascii "properties" and fallback to "proper UTF-16 encoding" when needed (through string_to_js !).