Skip to content

Commit 4174f52

Browse files
committed
JavaScript linker: make it easier to add flags
1 parent d4853b3 commit 4174f52

File tree

1 file changed

+28
-38
lines changed

1 file changed

+28
-38
lines changed

compiler/lib/linker.ml

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,17 @@ module Fragment = struct
177177
; weakdef : bool
178178
; always : bool
179179
; code : Javascript.program pack
180-
; js_string : bool option
181-
; effects : bool option
180+
; conditions : bool StringMap.t
182181
; fragment_target : Target_env.t option
183182
; aliases : StringSet.t
184183
}
185184

185+
let allowed_flags =
186+
List.fold_left
187+
~f:(fun m (k, v) -> StringMap.add k v m)
188+
~init:StringMap.empty
189+
[ "js-string", Config.Flag.use_js_string; "effects", Config.Flag.effects ]
190+
186191
type t =
187192
| Always_include of Javascript.program pack
188193
| Fragment of fragment_
@@ -247,8 +252,7 @@ module Fragment = struct
247252
; always = false
248253
; has_macro = false
249254
; code = Ok code
250-
; js_string = None
251-
; effects = None
255+
; conditions = StringMap.empty
252256
; fragment_target = None
253257
; aliases = StringSet.empty
254258
}
@@ -281,31 +285,24 @@ module Fragment = struct
281285
| `Always -> { fragment with always = true }
282286
| `Alias name ->
283287
{ fragment with aliases = StringSet.add name fragment.aliases }
284-
| (`Ifnot "js-string" | `If "js-string") as i ->
285-
let b =
286-
match i with
287-
| `If _ -> true
288-
| `Ifnot _ -> false
289-
in
290-
if Option.is_some fragment.js_string
291-
then Format.eprintf "Duplicated js-string in %s\n" (loc pi);
292-
{ fragment with js_string = Some b }
293-
| (`Ifnot "effects" | `If "effects") as i ->
288+
| `If name when Option.is_some (Target_env.of_string name) ->
289+
if Option.is_some fragment.fragment_target
290+
then Format.eprintf "Duplicated target_env in %s\n" (loc pi);
291+
{ fragment with fragment_target = Target_env.of_string name }
292+
| (`Ifnot v | `If v) when not (StringMap.mem v allowed_flags) ->
293+
Format.eprintf "Unkown flag %S in %s\n" v (loc pi);
294+
fragment
295+
| (`Ifnot v | `If v) as i ->
296+
if StringMap.mem v fragment.conditions
297+
then Format.eprintf "Duplicated %s in %s\n" v (loc pi);
294298
let b =
295299
match i with
296300
| `If _ -> true
297301
| `Ifnot _ -> false
298302
in
299-
if Option.is_some fragment.effects
300-
then Format.eprintf "Duplicated effects in %s\n" (loc pi);
301-
{ fragment with effects = Some b }
302-
| `If name when Option.is_some (Target_env.of_string name) ->
303-
if Option.is_some fragment.fragment_target
304-
then Format.eprintf "Duplicated target_env in %s\n" (loc pi);
305-
{ fragment with fragment_target = Target_env.of_string name }
306-
| `If name | `Ifnot name ->
307-
Format.eprintf "Unkown flag %S in %s\n" name (loc pi);
308-
fragment)
303+
{ fragment with
304+
conditions = StringMap.add v b fragment.conditions
305+
})
309306
in
310307
Fragment fragment)
311308
in
@@ -451,25 +448,18 @@ let load_fragment ~ignore_always_annotation ~target_env ~filename (f : Fragment.
451448
; weakdef
452449
; always
453450
; code
454-
; js_string
455-
; effects
456451
; fragment_target
457452
; aliases
458453
; has_macro
454+
; conditions
459455
} -> (
460-
let ignore_because_of_js_string =
461-
match js_string, Config.Flag.use_js_string () with
462-
| Some true, false | Some false, true -> true
463-
| None, _ | Some true, true | Some false, false -> false
464-
in
465-
let ignore_because_of_effects =
466-
match effects, Config.Flag.effects () with
467-
| Some true, false | Some false, true -> true
468-
| None, _ | Some true, true | Some false, false -> false
456+
let should_ignore =
457+
StringMap.exists
458+
(fun flag b ->
459+
not (Bool.equal b (StringMap.find flag Fragment.allowed_flags ())))
460+
conditions
469461
in
470-
if (not version_constraint_ok)
471-
|| ignore_because_of_js_string
472-
|| ignore_because_of_effects
462+
if (not version_constraint_ok) || should_ignore
473463
then `Ignored
474464
else
475465
match provides with

0 commit comments

Comments
 (0)