Skip to content

Commit 2ac578c

Browse files
author
Hongbo Zhang
committed
check predef exception in the begininng
1 parent 6b59211 commit 2ac578c

File tree

9 files changed

+22
-21
lines changed

9 files changed

+22
-21
lines changed

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_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

jscomp/lam_util.ml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,12 @@ let sort_dag_args param_args =
6565

6666

6767
let add_required_module (x : Ident.t) (meta : Lam_stats.meta) =
68-
if not @@ Ident.is_predef_exn x then
69-
meta.required_modules <- Lam_module_ident.of_ml x :: meta.required_modules
68+
meta.required_modules <- Lam_module_ident.of_ml x :: meta.required_modules
7069

7170
let add_required_modules ( x : Ident.t list) (meta : Lam_stats.meta) =
7271
let required_modules =
73-
Ext_list.filter_map
74-
(fun x ->
75-
if Ident.is_predef_exn x then
76-
None
77-
else Some ( Lam_module_ident.of_ml x)) x
72+
List.map
73+
(fun x -> Lam_module_ident.of_ml x) x
7874
@ meta.required_modules in
7975
meta.required_modules <- required_modules
8076

0 commit comments

Comments
 (0)