Skip to content

Commit 607a089

Browse files
committed
Compiler/Toplevel: restore runtime aliases inside toplevel
1 parent 293c7b1 commit 607a089

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

compiler/lib/unit_info.ml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,24 @@ type t =
2323
{ provides : StringSet.t
2424
; requires : StringSet.t
2525
; primitives : string list
26+
; aliases : (string * string) list
2627
; force_link : bool
2728
; effects_without_cps : bool
2829
}
2930

3031
let empty =
3132
{ provides = StringSet.empty
3233
; requires = StringSet.empty
34+
; aliases = []
3335
; primitives = []
3436
; force_link = false
3537
; effects_without_cps = false
3638
}
3739

38-
let of_primitives l =
40+
let of_primitives ~aliases l =
3941
{ provides = StringSet.empty
4042
; requires = StringSet.empty
43+
; aliases
4144
; primitives = l
4245
; force_link = true
4346
; effects_without_cps = false
@@ -58,16 +61,18 @@ let of_cmo (cmo : Cmo_format.compilation_unit) =
5861
| _ -> false)
5962
in
6063
let force_link = Cmo_format.force_link cmo in
61-
{ provides; requires; primitives = []; force_link; effects_without_cps }
64+
{ provides; requires; aliases = []; primitives = []; force_link; effects_without_cps }
6265

6366
let union t1 t2 =
6467
let provides = StringSet.union t1.provides t2.provides in
6568
let requires = StringSet.union t1.requires t2.requires in
6669
let requires = StringSet.diff requires provides in
6770
let primitives = t1.primitives @ t2.primitives in
71+
let aliases = t1.aliases @ t2.aliases in
6872
{ provides
6973
; requires
7074
; primitives
75+
; aliases
7176
; force_link = t1.force_link || t2.force_link
7277
; effects_without_cps = t1.effects_without_cps || t2.effects_without_cps
7378
}
@@ -82,6 +87,15 @@ let to_string t =
8287
; (if List.equal ~eq:String.equal empty.primitives t.primitives
8388
then []
8489
else [ prefix; "Primitives:"; String.concat ~sep:", " t.primitives ])
90+
; (if List.is_empty t.aliases
91+
then []
92+
else
93+
[ prefix
94+
; "Aliases:"
95+
; String.concat
96+
~sep:", "
97+
(List.map t.aliases ~f:(fun (a, b) -> String.concat ~sep:"=" [ a; b ]))
98+
])
8599
; (if Bool.equal empty.force_link t.force_link
86100
then []
87101
else [ prefix; "Force_link:"; string_of_bool t.force_link ])
@@ -123,6 +137,15 @@ let parse acc s =
123137
}
124138
| Some ("Primitives", primitives) ->
125139
Some { acc with primitives = acc.primitives @ parse_stringlist primitives }
140+
| Some ("Aliases", aliases) ->
141+
let x =
142+
parse_stringlist aliases
143+
|> List.map ~f:(fun s ->
144+
match String.lsplit2 s ~on:'=' with
145+
| None -> assert false
146+
| Some (a, b) -> a, b)
147+
in
148+
Some { acc with aliases = acc.aliases @ x }
126149
| Some ("Force_link", flink) ->
127150
Some
128151
{ acc with force_link = bool_of_string (String.trim flink) || acc.force_link }

compiler/lib/unit_info.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ type t =
2323
{ provides : StringSet.t
2424
; requires : StringSet.t
2525
; primitives : string list
26+
; aliases : (string * string) list
2627
; force_link : bool
2728
; effects_without_cps : bool
2829
}
2930

3031
val of_cmo : Cmo_format.compilation_unit -> t
3132

32-
val of_primitives : string list -> t
33+
val of_primitives : aliases:(string * string) list -> string list -> t
3334

3435
val union : t -> t -> t
3536

compiler/tests-check-prim/main.4.14.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ caml_set_static_env
134134

135135
From +toplevel.js:
136136
caml_dynlink_get_bytecode_sections
137+
jsoo_get_runtime_aliases
137138
jsoo_toplevel_init_compile
138139
jsoo_toplevel_init_reloc
139140

compiler/tests-check-prim/main.5.2.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ caml_sys_const_naked_pointers_checked
130130

131131
From +toplevel.js:
132132
caml_get_section_table
133+
jsoo_get_runtime_aliases
133134
jsoo_toplevel_init_compile
134135
jsoo_toplevel_init_reloc
135136

compiler/tests-check-prim/main.5.3.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ caml_set_static_env
128128
caml_sys_const_naked_pointers_checked
129129

130130
From +toplevel.js:
131+
jsoo_get_runtime_aliases
131132
jsoo_toplevel_init_compile
132133
jsoo_toplevel_init_reloc
133134

compiler/tests-check-prim/unix-Unix.4.14.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ caml_set_static_env
210210

211211
From +toplevel.js:
212212
caml_dynlink_get_bytecode_sections
213+
jsoo_get_runtime_aliases
213214
jsoo_toplevel_init_compile
214215
jsoo_toplevel_init_reloc
215216

compiler/tests-check-prim/unix-Unix.5.2.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ caml_sys_const_naked_pointers_checked
206206

207207
From +toplevel.js:
208208
caml_get_section_table
209+
jsoo_get_runtime_aliases
209210
jsoo_toplevel_init_compile
210211
jsoo_toplevel_init_reloc
211212

compiler/tests-check-prim/unix-Unix.5.3.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ caml_set_static_env
204204
caml_sys_const_naked_pointers_checked
205205

206206
From +toplevel.js:
207+
jsoo_get_runtime_aliases
207208
jsoo_toplevel_init_compile
208209
jsoo_toplevel_init_reloc
209210

compiler/tests-check-prim/unix-Win32.4.14.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ caml_set_static_env
182182

183183
From +toplevel.js:
184184
caml_dynlink_get_bytecode_sections
185+
jsoo_get_runtime_aliases
185186
jsoo_toplevel_init_compile
186187
jsoo_toplevel_init_reloc
187188

compiler/tests-check-prim/unix-Win32.5.2.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ caml_sys_const_naked_pointers_checked
179179

180180
From +toplevel.js:
181181
caml_get_section_table
182+
jsoo_get_runtime_aliases
182183
jsoo_toplevel_init_compile
183184
jsoo_toplevel_init_reloc
184185

compiler/tests-check-prim/unix-Win32.5.3.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ caml_set_static_env
177177
caml_sys_const_naked_pointers_checked
178178

179179
From +toplevel.js:
180+
jsoo_get_runtime_aliases
180181
jsoo_toplevel_init_compile
181182
jsoo_toplevel_init_reloc
182183

compiler/tests-ocaml/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
(executable
2424
(name expect)
2525
(modules expect)
26-
(libraries toplevel_expect_test js_of_ocaml-toplevel)
26+
(libraries toplevel_expect_test js_of_ocaml js_of_ocaml-toplevel)
2727
(flags
2828
(:standard -linkall))
2929
(js_of_ocaml

compiler/tests-ocaml/expect.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11

22
let () = Js_of_ocaml_toplevel.JsooTop.initialize ()
33

4+
let () = Printexc.register_printer (fun x ->
5+
match Js_of_ocaml.Js_error.of_exn x with
6+
| None -> None
7+
| Some e -> Some (Js_of_ocaml.Js_error.message e))
8+
49
let () = Toplevel_expect_test.run (fun _ -> Ast_mapper.default_mapper)
10+

0 commit comments

Comments
 (0)