Skip to content

Commit 786428f

Browse files
authored
Merge pull request #612 from bloomberg/fix_string_get
better handling of predefined exception
2 parents 8f47d75 + 6c8f4da commit 786428f

14 files changed

+165
-27
lines changed

jscomp/gen_slots.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ let code_of_array files =
2323
ATTENTION: we need re-run the code when we upgrade the compiler
2424
We do this to avoid dependencies
2525
*)
26-
let _ = print_endline (code_of_array ["pervasives.cmi"; "camlinternalOO.cmi"; "camlinternalMod.cmi"])
26+
let _ = print_endline
27+
(code_of_array
28+
["pervasives.cmi";
29+
"camlinternalOO.cmi";
30+
"camlinternalMod.cmi";
31+
"string.cmi";
32+
"array.cmi";
33+
"list.cmi"
34+
])

jscomp/lam.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type primitive =
4646
(* Globals *)
4747
| Pgetglobal of ident
4848
| Psetglobal of ident
49+
| Pglobal_exception of ident
4950
(* Operations on heap blocks *)
5051
| Pmakeblock of int * tag_info * mutable_flag
5152
| Pfield of int * field_dbg_info
@@ -553,7 +554,11 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args : t =
553554
| _ -> assert false
554555
end
555556
| Ploc loc -> assert false (* already compiled away here*)
556-
| Pgetglobal id -> prim ~primitive:(Pgetglobal id) ~args
557+
| Pgetglobal id ->
558+
if Ident.is_predef_exn id then
559+
prim ~primitive:(Pglobal_exception id) ~args
560+
else
561+
prim ~primitive:(Pgetglobal id) ~args
557562
| Psetglobal id -> prim ~primitive:(Psetglobal id) ~args
558563
| Pmakeblock (tag,info, mutable_flag)
559564
-> prim ~primitive:(Pmakeblock (tag,info,mutable_flag)) ~args
@@ -592,7 +597,7 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args : t =
592597
begin match args with
593598
| [Lprim {primitive = Pmakeblock (0, _, _) ;
594599
args = [
595-
Lprim {primitive = Pgetglobal ({name = "Assert_failure"} as id); args = []};
600+
Lprim {primitive = Pglobal_exception ({name = "Assert_failure"} as id); args = []};
596601
_
597602
]
598603
} ] when Ident.global id

jscomp/lam.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type primitive =
5151
| Pbytes_of_string
5252
| Pgetglobal of ident
5353
| Psetglobal of ident
54+
| Pglobal_exception of ident
5455
| Pmakeblock of int * Lambda.tag_info * Asttypes.mutable_flag
5556
| Pfield of int * Lambda.field_dbg_info
5657
| Psetfield of int * bool * Lambda.set_field_dbg_info

jscomp/lam_analysis.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ let rec no_side_effects (lam : Lam.t) : bool =
7272

7373

7474

75-
| Pgetglobal _
75+
| Pgetglobal _
76+
| Pglobal_exception _
7677
| Pmakeblock _ (* whether it's mutable or not *)
7778
| Pfield _
7879
| Pfloatfield _
@@ -188,7 +189,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
188189
args = [Lconst _]; _},exn,
189190
Lifthenelse(Lprim{args =
190191
[Lvar exn1;
191-
Lprim {primitive = Pgetglobal ({name="Not_found"}); args = []; _}]
192+
Lprim {primitive = Pglobal_exception ({name="Not_found"}); args = []; _}]
192193
; _},
193194
then_, _)) when Ident.same exn1 exn
194195
(** we might put this in an optimization pass

jscomp/lam_beta_reduce.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ let propogate_beta_reduce
232232
end;
233233
arg
234234
| Lprim {primitive = Pgetglobal ident; args = []; _} ->
235-
(* It's not completeness, its to make it sound.. *)
235+
(* It's not completeness, its to make it sound..
236+
Pass global module as an argument
237+
*)
236238
Lam_compile_global.query_lambda ident meta.env
237239
(* alias meta param ident (Module (Global ident)) Strict *)
238240
| Lprim {primitive = Pmakeblock (_, _, Immutable) ;args ; _} ->

jscomp/lam_compile.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ and
14641464
Lifthenelse
14651465
(Lprim{primitive = Pintcomp(Ceq);
14661466
args = [Lvar id2 ;
1467-
Lprim{primitive = Pgetglobal {name = "Not_found"}; _}]},
1467+
Lprim{primitive = Pglobal_exception {name = "Not_found"}; _}]},
14681468
cont, _reraise )
14691469
)
14701470
| Ltrywith(
@@ -1473,7 +1473,7 @@ and
14731473
id,
14741474
Lifthenelse(Lprim{primitive = Pintcomp(Ceq);
14751475
args = [
1476-
Lprim { primitive = Pgetglobal {name = "Not_found"; _}; _}; Lvar id2 ]},
1476+
Lprim { primitive = Pglobal_exception {name = "Not_found"; _}; _}; Lvar id2 ]},
14771477
cont, _reraise )
14781478
)) when Ident.same id id2
14791479
->

jscomp/lam_compile_global.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ let query_lambda id env =
6363
let get_exp (key : Lam_compile_env.key) : J.expression =
6464
match key with
6565
(id, env, expand) ->
66-
if Ident.is_predef_exn id
67-
then Js_of_lam_exception.get_builtin_by_name id.name
68-
else
69-
Lam_compile_env.query_and_add_if_not_exist
66+
Lam_compile_env.query_and_add_if_not_exist
7067
(Lam_module_ident.of_ml id)
7168
(Has_env env)
7269
~not_found:(fun id -> assert false)

jscomp/lam_compile_primitive.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ let translate
5454
| Pjs_fn_runmethod _
5555
-> assert false (* already handled by {!Lam_compile} *)
5656
| Pjs_fn_method _ -> assert false
57+
| Pglobal_exception id ->
58+
Js_of_lam_exception.get_builtin_by_name id.name
5759
| Pstringadd ->
5860
begin match args with
5961
| [a;b] ->

jscomp/lam_pass_collect.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ let collect_helper (meta : Lam_stats.meta) (lam : Lam.t) =
131131

132132
and collect (lam : Lam.t) =
133133
match lam with
134-
(* | Lprim (Pgetglobal ident,[]) *)
135-
(* -> *)
136-
(* if not @@ Ident.is_predef_exn ident then *)
137-
(* Lam_util.add_required_module ident meta *)
134+
138135
(** TODO:
139136
how about module aliases..
140137
record dependency

jscomp/lam_print.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ let primitive ppf (prim : Lam.primitive) = match prim with
111111
| Pjs_fn_runmethod i -> fprintf ppf "js_fn_runmethod_%i" i
112112
| Pdebugger -> fprintf ppf "debugger"
113113
| Pgetglobal id -> fprintf ppf "global %a" Ident.print id
114+
| Pglobal_exception id ->
115+
fprintf ppf "global exception %a" Ident.print id
114116
| Psetglobal id -> fprintf ppf "setglobal %a" Ident.print id
115117
| Pmakeblock(tag, _, Immutable) -> fprintf ppf "makeblock %i" tag
116118
| Pmakeblock(tag, _, Mutable) -> fprintf ppf "makemutable %i" tag

0 commit comments

Comments
 (0)