You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR adds the possibility of annotating an OCaml type expression (string * ty) list with [@js.dict] to indicate that values of this type should be mapped to JS objects in the obvious way (and vice-versa).
The generated javascript contains require("crypto") (both in the dev and release profiles) with a literal string argument (which seems important, see #66 (comment), cc @jchavarri), but only if the string "crypto" does not appear elsewhere in the file (because JSOO shares constants whenever it can), opened ocsigen/js_of_ocaml#1619 about this.
The third (and last!) commit adds the possibility of telling JSOO to use CamelCase instead of camelCase for the generated JS names, which is useful to bind certain APIs and much cleaner than using [@js "CamelCase"] everywhere. (The same could be achieved by naming the OCaml labels _camel_case, but it is not very nice...).
nojb
changed the title
Add support for [@js.dict] and [@@@js.require]
Add [@js.dict], [@@@js.require], [@@js.capitalize]Jun 3, 2024
I am not sure we want to add the [@js.dict] mainly because I am not super fond of the "Ojs.iter_properties" (I would love to deprecate it actually).
To implement your dictionnaries, you should be able to do what you want by creating a module like:
module Dict = struct
type 'a t = (string * 'a) list
let t_of_js f t =
let l = ref [] in
iter_properties t (fun k -> l := (k, f (get_prop_ascii t k)) :: !l);
!l
let t_to_js f x =
let t = empty_obj () in
List.iter (fun (k, v) -> set_prop_ascii t k (f v)) x;
t
end
Then you can replace ((string * t) list [@js.dict] by t Dict.t.
I've just realized that this example was mentioned in "LOW_LEVEL_BINDING.md .
@js.require
The current way to live without require is to add a custom JavaScript stub that enrich the global object with some object imported from modules (using one of the various module system found in the JavaScript ecosystem). I am not completely against providing an out-of-the box support a require annotation but it will be better to check if it works not only with node but also with bundling tools like browserify and webpack.
@js.capitialize
Do you think it would be interesting to support a module annotation (ie. @@@) for that ?
Thanks for the review! The changes here are not critical; I suggest we punt this and revisit it later if/as needed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds the possibility of annotating an OCaml type expression
(string * ty) listwith[@js.dict]to indicate that values of this type should be mapped to JS objects in the obvious way (and vice-versa).