Skip to content

Commit 61f788a

Browse files
committed
Compiler/Toplevel: restore runtime aliases inside toplevel
1 parent e780e71 commit 61f788a

16 files changed

+41
-5
lines changed

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/parse_bytecode.ml

Lines changed: 4 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 =
@@ -3073,10 +3075,12 @@ let link_info ~symbols ~primitives ~crcs =
30733075
let body =
30743076
(* Include linking information *)
30753077
let sections = { symb = symbols; crcs; prim = primitives; dlpt = [] } in
3078+
let aliases = Primitive.aliases () in
30763079
let infos =
30773080
[ "sections", Constants.parse (Obj.repr sections)
30783081
; "symbols", Constants.parse (Obj.repr symbols_array)
30793082
; "prim_count", Int (Targetint.of_int_exn (List.length primitives))
3083+
; "aliases", Constants.parse (Obj.repr aliases)
30803084
]
30813085
in
30823086
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/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

0 commit comments

Comments
 (0)