Skip to content

Commit 0ca314d

Browse files
committed
Compiler/Toplevel: restore runtime aliases inside toplevel
1 parent 8d807d4 commit 0ca314d

24 files changed

+115
-15
lines changed

compiler/bin-js_of_ocaml/compile.ml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,17 @@ let run
336336
in
337337
let output_partial_runtime ~standalone ~source_map ((_, fmt) as output_file) =
338338
assert (not standalone);
339-
let uinfo =
340-
Unit_info.of_primitives
341-
(Linker.list_all ~from:runtime_files_from_cmdline () |> StringSet.elements)
339+
let primitives, aliases =
340+
let all = Linker.list_all_with_aliases ~from:runtime_files_from_cmdline () in
341+
StringMap.fold
342+
(fun n a (primitives, aliases) ->
343+
let primitives = StringSet.add n primitives in
344+
let aliases = List.map (StringSet.elements a) ~f:(fun a -> a, n) @ aliases in
345+
primitives, aliases)
346+
all
347+
(StringSet.empty, [])
342348
in
349+
let uinfo = Unit_info.of_primitives ~aliases (StringSet.elements primitives) in
343350
Pretty_print.string fmt "\n";
344351
Pretty_print.string fmt (Unit_info.to_string uinfo);
345352
let code =
@@ -358,10 +365,20 @@ let run
358365
in
359366
(match bytecode with
360367
| `None ->
361-
let prims = Linker.list_all () |> StringSet.elements in
362-
assert (List.length prims > 0);
368+
let primitives, aliases =
369+
let all = Linker.list_all_with_aliases () in
370+
StringMap.fold
371+
(fun n a (primitives, aliases) ->
372+
let primitives = StringSet.add n primitives in
373+
let aliases = List.map (StringSet.elements a) ~f:(fun a -> a, n) @ aliases in
374+
primitives, aliases)
375+
all
376+
(StringSet.empty, [])
377+
in
378+
let primitives = StringSet.elements primitives in
379+
assert (List.length primitives > 0);
363380
let code, uinfo = Parse_bytecode.predefined_exceptions () in
364-
let uinfo = { uinfo with primitives = uinfo.primitives @ prims } in
381+
let uinfo = Unit_info.union uinfo (Unit_info.of_primitives ~aliases primitives) in
365382
let code : Parse_bytecode.one =
366383
{ code
367384
; cmis = StringSet.empty

compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type bytecode_sections =
1212

1313
external get_bytecode_sections : unit -> bytecode_sections = "jsoo_get_bytecode_sections"
1414

15+
external get_runtime_aliases : unit -> (string * string) list = "jsoo_get_runtime_aliases"
16+
1517
external toplevel_init_compile :
1618
(string -> Instruct.debug_event list array -> unit -> J.t) -> unit
1719
= "jsoo_toplevel_init_compile"
@@ -37,10 +39,12 @@ let () =
3739
(match Sys.backend_type with
3840
| Sys.Other "js_of_ocaml" -> Config.set_target `JavaScript
3941
| Sys.(Native | Bytecode | Other _) -> failwith "Expected backend `js_of_ocaml`");
42+
let aliases = get_runtime_aliases () in
4043
let global = J.pure_js_expr "globalThis" in
4144
Config.Flag.set "use-js-string" (Jsoo_runtime.Sys.Config.use_js_string ());
4245
Config.set_effects_backend (Jsoo_runtime.Sys.Config.effects ());
4346
Linker.reset ();
47+
List.iter aliases ~f:(fun (a, b) -> Primitive.alias a b);
4448
(* this needs to stay synchronized with toplevel.js *)
4549
let toplevel_compile (s : string) (debug : Instruct.debug_event list array) :
4650
unit -> J.t =

compiler/lib-dynlink/stubs.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ void jsoo_get_bytecode_sections () {
55
exit(1);
66
}
77

8+
void jsoo_get_runtime_aliases () {
9+
fprintf(stderr, "Unimplemented Javascript primitive jsoo_get_runtime_aliases!\n");
10+
exit(1);
11+
}
12+
813
void jsoo_toplevel_init_compile () {
914
fprintf(stderr, "Unimplemented Javascript primitive jsoo_toplevel_init_compile!\n");
1015
exit(1);

compiler/lib-wasm/link.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ end = struct
8989
{ provides = t |> member "provides" |> set empty.provides
9090
; requires = t |> member "requires" |> set empty.requires
9191
; primitives = t |> member "primitives" |> list empty.primitives
92+
; aliases = []
9293
; force_link = t |> member "force_link" |> bool empty.force_link
9394
; effects_without_cps =
9495
t |> member "effects_without_cps" |> bool empty.effects_without_cps

compiler/lib/link_js.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
412412
Build_info.configure bi;
413413
let primitives =
414414
List.fold_left units ~init:StringSet.empty ~f:(fun acc (u : Unit_info.t) ->
415+
List.iter u.aliases ~f:(fun (a, b) -> Primitive.alias a b);
415416
StringSet.union acc (StringSet.of_list u.primitives))
416417
in
417418
let code = Parse_bytecode.link_info ~symbols:!sym ~primitives ~crcs:[] in

compiler/lib/linker.ml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ type provided =
426426
; filename : string
427427
; weakdef : bool
428428
; target_env : Target_env.t
429+
; aliases : StringSet.t
429430
}
430431

431432
let always_included = ref []
@@ -455,6 +456,18 @@ let list_all ?from () =
455456
provided
456457
StringSet.empty
457458

459+
let list_all_with_aliases ?from () =
460+
let include_ =
461+
match from with
462+
| None -> fun _ _ -> true
463+
| Some l -> fun fn _nm -> List.mem fn ~set:l
464+
in
465+
Hashtbl.fold
466+
(fun nm p map ->
467+
if include_ p.filename nm then StringMap.add nm p.aliases map else map)
468+
provided
469+
StringMap.empty
470+
458471
let load_fragment ~target_env ~filename (f : Fragment.t) =
459472
match f with
460473
| Always_include code ->
@@ -559,7 +572,7 @@ let load_fragment ~target_env ~filename (f : Fragment.t) =
559572
Hashtbl.add
560573
provided
561574
name
562-
{ id; pi; filename; weakdef; target_env = fragment_target };
575+
{ id; pi; filename; weakdef; target_env = fragment_target; aliases };
563576
Hashtbl.add provided_rev id (name, pi);
564577
Hashtbl.add code_pieces id (code, has_macro, requires, deprecated);
565578
StringSet.iter (fun alias -> Primitive.alias alias name) aliases;

compiler/lib/linker.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ type output =
5757

5858
val list_all : ?from:string list -> unit -> StringSet.t
5959

60+
val list_all_with_aliases : ?from:string list -> unit -> StringSet.t StringMap.t
61+
6062
val init : ?from:string list -> unit -> state
6163

6264
val resolve_deps : ?check_missing:bool -> state -> StringSet.t -> state * StringSet.t

compiler/lib/parse_bytecode.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,10 +2645,12 @@ let from_exe
26452645
let sections = { symb = symbols; crcs; prim = primitives; dlpt = [] } in
26462646
let gdata = Var.fresh () in
26472647
let need_gdata = ref false in
2648+
let aliases = Primitive.aliases () in
26482649
let infos =
26492650
[ "sections", Constants.parse (Obj.repr sections)
26502651
; "symbols", Constants.parse (Obj.repr symbols_array)
26512652
; "prim_count", Int (Targetint.of_int_exn (Array.length globals.primitives))
2653+
; "aliases", Constants.parse (Obj.repr aliases)
26522654
]
26532655
in
26542656
let body =
@@ -3052,6 +3054,7 @@ let predefined_exceptions () =
30523054
; force_link = true
30533055
; effects_without_cps = false
30543056
; primitives = []
3057+
; aliases = []
30553058
}
30563059
in
30573060
{ start = 0; blocks = Addr.Map.singleton 0 block; free_pc = 1 }, unit_info
@@ -3073,10 +3076,12 @@ let link_info ~symbols ~primitives ~crcs =
30733076
let body =
30743077
(* Include linking information *)
30753078
let sections = { symb = symbols; crcs; prim = primitives; dlpt = [] } in
3079+
let aliases = Primitive.aliases () in
30763080
let infos =
30773081
[ "sections", Constants.parse (Obj.repr sections)
30783082
; "symbols", Constants.parse (Obj.repr symbols_array)
30793083
; "prim_count", Int (Targetint.of_int_exn (List.length primitives))
3084+
; "aliases", Constants.parse (Obj.repr aliases)
30803085
]
30813086
in
30823087
let body =

compiler/lib/primitive.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
*)
2020
open! Stdlib
2121

22-
let aliases = Hashtbl.create 17
22+
let aliases_ = Hashtbl.create 17
2323

24-
let rec resolve nm = try resolve (Hashtbl.find aliases nm) with Not_found -> nm
24+
let rec resolve nm = try resolve (Hashtbl.find aliases_ nm) with Not_found -> nm
2525

2626
(****)
2727

@@ -109,7 +109,9 @@ let register p k kargs arity =
109109
let alias nm nm' =
110110
add_external nm';
111111
add_external nm;
112-
Hashtbl.replace aliases nm nm'
112+
Hashtbl.replace aliases_ nm nm'
113+
114+
let aliases () = Hashtbl.to_seq aliases_ |> List.of_seq
113115

114116
let named_values = ref StringSet.empty
115117

@@ -121,5 +123,5 @@ let reset () =
121123
Hashtbl.clear kinds;
122124
Hashtbl.clear kind_args_tbl;
123125
Hashtbl.clear arities;
124-
Hashtbl.clear aliases;
126+
Hashtbl.clear aliases_;
125127
named_values := StringSet.empty

compiler/lib/primitive.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ val has_arity : string -> int -> bool
6464

6565
val alias : string -> string -> unit
6666

67+
val aliases : unit -> (string * string) list
68+
6769
val resolve : string -> string
6870

6971
val add_external : string -> unit

0 commit comments

Comments
 (0)