Skip to content

Commit 60d6aab

Browse files
committed
CR
Signed-off-by: Olivier Nicole <[email protected]>
1 parent cd27732 commit 60d6aab

File tree

1 file changed

+85
-81
lines changed

1 file changed

+85
-81
lines changed

src/dune_rules/jsoo/jsoo_rules.ml

Lines changed: 85 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,50 @@ let js_env = compute_env ~mode:JS
4141
let wasm_env = compute_env ~mode:Wasm
4242
let jsoo_env ~dir ~mode = (Js_of_ocaml.Mode.select ~mode ~js:js_env ~wasm:wasm_env) ~dir
4343

44+
module Version = struct
45+
type t = int * int
46+
47+
let of_string s : t option =
48+
let s =
49+
match
50+
String.findi s ~f:(function
51+
| '+' | '-' | '~' -> true
52+
| _ -> false)
53+
with
54+
| None -> s
55+
| Some i -> String.take s i
56+
in
57+
try
58+
match String.split s ~on:'.' with
59+
| [] -> None
60+
| [ major ] -> Some (int_of_string major, 0)
61+
| major :: minor :: _ -> Some (int_of_string major, int_of_string minor)
62+
with
63+
| _ -> None
64+
;;
65+
66+
let compare (ma1, mi1) (ma2, mi2) =
67+
match Int.compare ma1 ma2 with
68+
| Eq -> Int.compare mi1 mi2
69+
| n -> n
70+
;;
71+
72+
let impl_version bin =
73+
let* _ = Build_system.build_file bin in
74+
Memo.of_reproducible_fiber
75+
@@ Process.run_capture_line ~display:Quiet Strict bin [ "--version" ]
76+
|> Memo.map ~f:of_string
77+
;;
78+
79+
let version_memo = Memo.create "jsoo-version" ~input:(module Path) impl_version
80+
81+
let jsoo_version jsoo =
82+
match jsoo with
83+
| Ok jsoo_path -> Memo.exec version_memo jsoo_path
84+
| Error e -> Action.Prog.Not_found.raise e
85+
;;
86+
end
87+
4488
module Config : sig
4589
type t
4690

@@ -49,8 +93,7 @@ module Config : sig
4993
val of_string : string -> t
5094
val of_flags : string list -> t
5195

52-
(** [recent] should be true if jsoo version is 6.0 or higher. *)
53-
val to_flags : recent:bool -> t -> string list
96+
val to_flags : jsoo_version:Version.t option -> t -> string list
5497
end = struct
5598
type effects_backend =
5699
| Cps
@@ -72,21 +115,23 @@ end = struct
72115
List.concat_map bool_opt ~f:(fun toplevel -> [ { js_string; effects; toplevel } ])))
73116
;;
74117

75-
let set acc name v =
76-
match name, v with
77-
| "use-js-string", `True -> { acc with js_string = Some true }
78-
| "use-js-string", `False -> { acc with js_string = Some false }
79-
| "effects", `Effects backend -> { acc with effects = Some backend }
80-
| "effects", `False ->
81-
(* [--disable effects] *)
82-
{ acc with effects = None }
83-
| "effects", `True ->
118+
let enable name acc =
119+
match name with
120+
| "use-js-string" -> { acc with js_string = Some true }
121+
| "effects" ->
84122
(* [--enable effects], used alone, implies [--effects=cps] *)
85123
(match acc.effects with
86124
| None -> { acc with effects = Some Cps }
87125
| Some _ -> acc)
88-
| "toplevel", `True -> { acc with toplevel = Some true }
89-
| "toplevel", `False -> { acc with toplevel = Some false }
126+
| "toplevel" -> { acc with toplevel = Some true }
127+
| _ -> acc
128+
;;
129+
130+
let disable name acc =
131+
match name with
132+
| "use-js-string" -> { acc with js_string = Some false }
133+
| "effects" -> { acc with effects = None }
134+
| "toplevel" -> { acc with toplevel = Some false }
90135
| _ -> acc
91136
;;
92137

@@ -126,64 +171,73 @@ end = struct
126171
match
127172
String.drop_prefix ~prefix:"!" name, String.drop_prefix ~prefix:"effects=" name
128173
with
129-
| Some name, _ -> set acc name `False
130-
| None, None -> set acc name `True
174+
| Some name, _ -> disable name acc
175+
| None, None -> enable name acc
131176
| None, Some backend ->
132177
(match effects_of_string backend with
133-
| Some backend -> set acc "effects" (`Effects backend)
178+
| Some backend -> { acc with effects = Some backend }
134179
| None -> acc))
135180
;;
136181

137182
let of_flags l =
138183
let rec loop acc = function
139184
| [] -> acc
140-
| "--enable" :: name :: rest -> loop (set acc name `True) rest
185+
| "--enable" :: name :: rest -> loop (enable name acc) rest
141186
| maybe_enable :: rest when String.is_prefix maybe_enable ~prefix:"--enable=" ->
142187
(match String.drop_prefix maybe_enable ~prefix:"--enable=" with
143-
| Some name -> loop (set acc name `True) rest
188+
| Some name -> loop (enable name acc) rest
144189
| _ -> assert false)
145-
| "--disable" :: name :: rest -> loop (set acc name `False) rest
190+
| "--disable" :: name :: rest -> loop (disable name acc) rest
146191
| maybe_disable :: rest when String.is_prefix maybe_disable ~prefix:"--disable=" ->
147192
(match String.drop_prefix maybe_disable ~prefix:"--disable=" with
148-
| Some name -> loop (set acc name `False) rest
193+
| Some name -> loop (disable name acc) rest
149194
| _ -> assert false)
150-
| "--toplevel" :: rest -> loop (set acc "toplevel" `True) rest
151-
| "--effects" :: "cps" :: rest -> loop (set acc "effects" `Cps) rest
195+
| "--toplevel" :: rest -> loop (enable "toplevel" acc) rest
196+
| "--effects" :: "cps" :: rest -> loop { acc with effects = Some Cps } rest
152197
| "--effects" :: "double-translation" :: rest ->
153-
loop (set acc "effects" `Double_translation) rest
198+
loop { acc with effects = Some Double_translation } rest
154199
| maybe_effects :: rest when String.is_prefix maybe_effects ~prefix:"--effects=" ->
155200
let backend =
156201
Option.bind
157202
(String.drop_prefix maybe_effects ~prefix:"--effects=")
158203
~f:effects_of_string
159204
in
160205
(match backend with
161-
| Some backend -> set acc "effects" (`Effects backend)
206+
| Some backend -> loop { acc with effects = Some backend } rest
162207
| None -> loop acc rest)
163208
| _ :: rest -> loop acc rest
164209
in
165210
loop default l
166211
;;
167212

168-
let backward_compatible_effects ~recent str =
213+
let backward_compatible_effects ~jsoo_version str =
169214
match str with
170215
| None ->
171216
(* For jsoo, this means unsupported effects. For wasmoo, this means effects go
172217
through the Javascript Promise API. *)
173218
None
174-
| Some Cps -> if recent then Some "--effects=cps" else Some "--enable=effects"
219+
| Some Cps ->
220+
let v6_or_higher =
221+
match jsoo_version with
222+
| Some v ->
223+
(match Version.compare v (6, 0) with
224+
| Gt | Eq -> true
225+
| Lt -> false)
226+
| None -> false
227+
in
228+
if v6_or_higher then Some "--effects=cps" else Some "--enable=effects"
175229
| Some Double_translation ->
176230
(* For js_of_ocaml < 6.0, this flag does not exist and will raise an error,
177231
which is fine. *)
178232
Some "--effects=double-translation"
179233
;;
180234

181-
let to_flags ~recent t =
235+
let to_flags ~jsoo_version t =
182236
List.filter_opt
183237
[ (match t.toplevel with
184238
| Some true -> Some "--toplevel"
185239
| _ -> None)
186-
; backward_compatible_effects ~recent t.effects
240+
; backward_compatible_effects ~jsoo_version t.effects
187241
; (match t.js_string with
188242
| Some true -> Some "--enable=use-js-string"
189243
| Some false -> Some "--disable=use-js-string"
@@ -192,50 +246,6 @@ end = struct
192246
;;
193247
end
194248

195-
module Version = struct
196-
type t = int * int
197-
198-
let of_string s : t option =
199-
let s =
200-
match
201-
String.findi s ~f:(function
202-
| '+' | '-' | '~' -> true
203-
| _ -> false)
204-
with
205-
| None -> s
206-
| Some i -> String.take s i
207-
in
208-
try
209-
match String.split s ~on:'.' with
210-
| [] -> None
211-
| [ major ] -> Some (int_of_string major, 0)
212-
| major :: minor :: _ -> Some (int_of_string major, int_of_string minor)
213-
with
214-
| _ -> None
215-
;;
216-
217-
let compare (ma1, mi1) (ma2, mi2) =
218-
match Int.compare ma1 ma2 with
219-
| Eq -> Int.compare mi1 mi2
220-
| n -> n
221-
;;
222-
223-
let impl_version bin =
224-
let* _ = Build_system.build_file bin in
225-
Memo.of_reproducible_fiber
226-
@@ Process.run_capture_line ~display:Quiet Strict bin [ "--version" ]
227-
|> Memo.map ~f:of_string
228-
;;
229-
230-
let version_memo = Memo.create "jsoo-version" ~input:(module Path) impl_version
231-
232-
let jsoo_version jsoo =
233-
match jsoo with
234-
| Ok jsoo_path -> Memo.exec version_memo jsoo_path
235-
| Error e -> Action.Prog.Not_found.raise e
236-
;;
237-
end
238-
239249
let install_jsoo_hint = "opam install js_of_ocaml-compiler"
240250

241251
let in_build_dir (ctx : Build_context.t) ~config args =
@@ -342,16 +352,10 @@ let js_of_ocaml_rule
342352
let* jsoo = jsoo in
343353
Action_builder.of_memo (Version.jsoo_version jsoo)
344354
in
345-
let recent =
346-
match jsoo_version with
347-
| Some v ->
348-
(match Version.compare v (6, 0) with
349-
| Gt | Eq -> true
350-
| Lt -> false)
351-
| None -> false
352-
in
353355
Command.Args.S
354-
(List.map (Config.to_flags ~recent config) ~f:(fun x -> Command.Args.A x))))
356+
(List.map
357+
(Config.to_flags ~jsoo_version config)
358+
~f:(fun x -> Command.Args.A x))))
355359
; A "-o"
356360
; Target target
357361
; spec

0 commit comments

Comments
 (0)