Skip to content

Commit c9b63e1

Browse files
committed
simplify get_package_path_from_cmj
1 parent 27fb7e6 commit c9b63e1

File tree

5 files changed

+126
-66
lines changed

5 files changed

+126
-66
lines changed

jscomp/core/js_name_of_module_id.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ let string_of_module_id
113113
module_system
114114
in
115115
match Lam_compile_env.get_package_path_from_cmj dep_module_id with
116-
| None ->
117-
Bs_exception.error (Missing_ml_dependency dep_module_id.id.name)
118-
| Some (cmj_path, dep_package_info, little) ->
116+
| (cmj_path, dep_package_info, little) ->
119117
let js_file = Ext_namespace.js_name_of_modulename dep_module_id.id.name little in
120118
let dep_info_query =
121119
Js_packages_info.query_package_infos dep_package_info module_system

jscomp/core/lam_compile_env.ml

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ type ident_info = {
6969

7070

7171

72-
let cached_tbl = Lam_module_ident.Hash.create 31
72+
let cached_tbl : env_value Lam_module_ident.Hash.t
73+
= Lam_module_ident.Hash.create 31
74+
let (+>) = Lam_module_ident.Hash.add cached_tbl
75+
7376

7477
(* For each compilation we need reset to make it re-entrant *)
7578
let reset () =
@@ -115,7 +118,7 @@ let add_js_module
115118

116119

117120

118-
let (+>) = Lam_module_ident.Hash.add cached_tbl
121+
119122

120123
let cached_find_ml_id_pos (module_id : Ident.t) name : ident_info =
121124
let oid = Lam_module_ident.of_ml module_id in
@@ -187,7 +190,7 @@ let query_and_add_if_not_exist
187190
begin match
188191
Ocaml_types.find_serializable_signatures_by_path oid.id env with
189192
| None -> not_found () (* actually when [not_found] in the call site, we throw... *)
190-
| Some signature ->
193+
| Some _ ->
191194
oid +> Visit {cmj_table;cmj_path } ;
192195
found { pure = Js_cmj_format.is_pure cmj_table}
193196
end
@@ -237,26 +240,45 @@ let query_and_add_if_not_exist
237240

238241
let get_package_path_from_cmj
239242
( id : Lam_module_ident.t)
240-
: _ option =
241-
query_and_add_if_not_exist id No_env
242-
~not_found:(fun _ ->
243-
None
244-
(*
245-
So after querying, it should return
246-
[Js_packages_info.Package_not_found]
247-
*)
248-
)
249-
~found:(fun (cmj_path,x) ->
250-
Some (cmj_path,
251-
Js_cmj_format.get_npm_package_path x,
252-
Js_cmj_format.get_cmj_case x )
253-
)
254-
243+
=
244+
match Lam_module_ident.Hash.find_opt cached_tbl id with
245+
| Some (Visit {cmj_table ; cmj_path}) ->
246+
(cmj_path,
247+
Js_cmj_format.get_npm_package_path cmj_table,
248+
Js_cmj_format.get_cmj_case cmj_table )
249+
| Some (External | Runtime _ ) ->
250+
assert false
251+
(* called by {!Js_name_of_module_id.string_of_module_id}
252+
can not be External
253+
*)
254+
| None ->
255+
begin match id.kind with
256+
| Runtime
257+
| External _ -> assert false
258+
| Ml ->
259+
let (cmj_path, cmj_table) =
260+
Js_cmj_load.find_cmj (Lam_module_ident.name id ^ Literals.suffix_cmj) in
261+
id +> Visit {cmj_table;cmj_path };
262+
(cmj_path,
263+
Js_cmj_format.get_npm_package_path cmj_table,
264+
Js_cmj_format.get_cmj_case cmj_table )
265+
end
255266

256267
let add = Lam_module_ident.Hash_set.add
257268

258269

259270

271+
(* let is_pure_module (id : Lam_module_ident.t) =
272+
match id.kind with
273+
| Runtime -> true
274+
| External _ -> false
275+
| Ml ->
276+
match Lam_module_ident.Hash.find_opt cached_tbl id with
277+
| Some (Visit {cmj_table = {pure}}) -> pure
278+
| Some _ -> assert false
279+
| None -> *)
280+
281+
260282

261283
(* Conservative interface *)
262284
let is_pure_module (id : Lam_module_ident.t) =

jscomp/core/lam_compile_env.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ val is_pure_module : Lam_module_ident.t -> bool
109109

110110
val get_package_path_from_cmj :
111111
Lam_module_ident.t ->
112-
(string * Js_packages_info.t * Js_cmj_format.cmj_case) option
112+
(string * Js_packages_info.t * Js_cmj_format.cmj_case)
113113

114114

115115

lib/4.02.3/unstable/js_compiler.ml

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96561,7 +96561,7 @@ val is_pure_module : Lam_module_ident.t -> bool
9656196561

9656296562
val get_package_path_from_cmj :
9656396563
Lam_module_ident.t ->
96564-
(string * Js_packages_info.t * Js_cmj_format.cmj_case) option
96564+
(string * Js_packages_info.t * Js_cmj_format.cmj_case)
9656596565

9656696566

9656796567

@@ -96648,7 +96648,10 @@ type ident_info = {
9664896648

9664996649

9665096650

96651-
let cached_tbl = Lam_module_ident.Hash.create 31
96651+
let cached_tbl : env_value Lam_module_ident.Hash.t
96652+
= Lam_module_ident.Hash.create 31
96653+
let (+>) = Lam_module_ident.Hash.add cached_tbl
96654+
9665296655

9665396656
(* For each compilation we need reset to make it re-entrant *)
9665496657
let reset () =
@@ -96694,7 +96697,7 @@ let add_js_module
9669496697

9669596698

9669696699

96697-
let (+>) = Lam_module_ident.Hash.add cached_tbl
96700+
9669896701

9669996702
let cached_find_ml_id_pos (module_id : Ident.t) name : ident_info =
9670096703
let oid = Lam_module_ident.of_ml module_id in
@@ -96766,7 +96769,7 @@ let query_and_add_if_not_exist
9676696769
begin match
9676796770
Ocaml_types.find_serializable_signatures_by_path oid.id env with
9676896771
| None -> not_found () (* actually when [not_found] in the call site, we throw... *)
96769-
| Some signature ->
96772+
| Some _ ->
9677096773
oid +> Visit {cmj_table;cmj_path } ;
9677196774
found { pure = Js_cmj_format.is_pure cmj_table}
9677296775
end
@@ -96816,26 +96819,45 @@ let query_and_add_if_not_exist
9681696819

9681796820
let get_package_path_from_cmj
9681896821
( id : Lam_module_ident.t)
96819-
: _ option =
96820-
query_and_add_if_not_exist id No_env
96821-
~not_found:(fun _ ->
96822-
None
96823-
(*
96824-
So after querying, it should return
96825-
[Js_packages_info.Package_not_found]
96826-
*)
96827-
)
96828-
~found:(fun (cmj_path,x) ->
96829-
Some (cmj_path,
96830-
Js_cmj_format.get_npm_package_path x,
96831-
Js_cmj_format.get_cmj_case x )
96832-
)
96833-
96822+
=
96823+
match Lam_module_ident.Hash.find_opt cached_tbl id with
96824+
| Some (Visit {cmj_table ; cmj_path}) ->
96825+
(cmj_path,
96826+
Js_cmj_format.get_npm_package_path cmj_table,
96827+
Js_cmj_format.get_cmj_case cmj_table )
96828+
| Some (External | Runtime _ ) ->
96829+
assert false
96830+
(* called by {!Js_name_of_module_id.string_of_module_id}
96831+
can not be External
96832+
*)
96833+
| None ->
96834+
begin match id.kind with
96835+
| Runtime
96836+
| External _ -> assert false
96837+
| Ml ->
96838+
let (cmj_path, cmj_table) =
96839+
Js_cmj_load.find_cmj (Lam_module_ident.name id ^ Literals.suffix_cmj) in
96840+
id +> Visit {cmj_table;cmj_path };
96841+
(cmj_path,
96842+
Js_cmj_format.get_npm_package_path cmj_table,
96843+
Js_cmj_format.get_cmj_case cmj_table )
96844+
end
9683496845

9683596846
let add = Lam_module_ident.Hash_set.add
9683696847

9683796848

9683896849

96850+
(* let is_pure_module (id : Lam_module_ident.t) =
96851+
match id.kind with
96852+
| Runtime -> true
96853+
| External _ -> false
96854+
| Ml ->
96855+
match Lam_module_ident.Hash.find_opt cached_tbl id with
96856+
| Some (Visit {cmj_table = {pure}}) -> pure
96857+
| Some _ -> assert false
96858+
| None -> *)
96859+
96860+
9683996861

9684096862
(* Conservative interface *)
9684196863
let is_pure_module (id : Lam_module_ident.t) =
@@ -97016,9 +97038,7 @@ let string_of_module_id
9701697038
module_system
9701797039
in
9701897040
match Lam_compile_env.get_package_path_from_cmj dep_module_id with
97019-
| None ->
97020-
Bs_exception.error (Missing_ml_dependency dep_module_id.id.name)
97021-
| Some (cmj_path, dep_package_info, little) ->
97041+
| (cmj_path, dep_package_info, little) ->
9702297042
let js_file = Ext_namespace.js_name_of_modulename dep_module_id.id.name little in
9702397043
let dep_info_query =
9702497044
Js_packages_info.query_package_infos dep_package_info module_system

lib/4.02.3/whole_compiler.ml

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84073,7 +84073,7 @@ val is_pure_module : Lam_module_ident.t -> bool
8407384073

8407484074
val get_package_path_from_cmj :
8407584075
Lam_module_ident.t ->
84076-
(string * Js_packages_info.t * Js_cmj_format.cmj_case) option
84076+
(string * Js_packages_info.t * Js_cmj_format.cmj_case)
8407784077

8407884078

8407984079

@@ -84160,7 +84160,10 @@ type ident_info = {
8416084160

8416184161

8416284162

84163-
let cached_tbl = Lam_module_ident.Hash.create 31
84163+
let cached_tbl : env_value Lam_module_ident.Hash.t
84164+
= Lam_module_ident.Hash.create 31
84165+
let (+>) = Lam_module_ident.Hash.add cached_tbl
84166+
8416484167

8416584168
(* For each compilation we need reset to make it re-entrant *)
8416684169
let reset () =
@@ -84206,7 +84209,7 @@ let add_js_module
8420684209

8420784210

8420884211

84209-
let (+>) = Lam_module_ident.Hash.add cached_tbl
84212+
8421084213

8421184214
let cached_find_ml_id_pos (module_id : Ident.t) name : ident_info =
8421284215
let oid = Lam_module_ident.of_ml module_id in
@@ -84278,7 +84281,7 @@ let query_and_add_if_not_exist
8427884281
begin match
8427984282
Ocaml_types.find_serializable_signatures_by_path oid.id env with
8428084283
| None -> not_found () (* actually when [not_found] in the call site, we throw... *)
84281-
| Some signature ->
84284+
| Some _ ->
8428284285
oid +> Visit {cmj_table;cmj_path } ;
8428384286
found { pure = Js_cmj_format.is_pure cmj_table}
8428484287
end
@@ -84328,26 +84331,45 @@ let query_and_add_if_not_exist
8432884331

8432984332
let get_package_path_from_cmj
8433084333
( id : Lam_module_ident.t)
84331-
: _ option =
84332-
query_and_add_if_not_exist id No_env
84333-
~not_found:(fun _ ->
84334-
None
84335-
(*
84336-
So after querying, it should return
84337-
[Js_packages_info.Package_not_found]
84338-
*)
84339-
)
84340-
~found:(fun (cmj_path,x) ->
84341-
Some (cmj_path,
84342-
Js_cmj_format.get_npm_package_path x,
84343-
Js_cmj_format.get_cmj_case x )
84344-
)
84345-
84334+
=
84335+
match Lam_module_ident.Hash.find_opt cached_tbl id with
84336+
| Some (Visit {cmj_table ; cmj_path}) ->
84337+
(cmj_path,
84338+
Js_cmj_format.get_npm_package_path cmj_table,
84339+
Js_cmj_format.get_cmj_case cmj_table )
84340+
| Some (External | Runtime _ ) ->
84341+
assert false
84342+
(* called by {!Js_name_of_module_id.string_of_module_id}
84343+
can not be External
84344+
*)
84345+
| None ->
84346+
begin match id.kind with
84347+
| Runtime
84348+
| External _ -> assert false
84349+
| Ml ->
84350+
let (cmj_path, cmj_table) =
84351+
Js_cmj_load.find_cmj (Lam_module_ident.name id ^ Literals.suffix_cmj) in
84352+
id +> Visit {cmj_table;cmj_path };
84353+
(cmj_path,
84354+
Js_cmj_format.get_npm_package_path cmj_table,
84355+
Js_cmj_format.get_cmj_case cmj_table )
84356+
end
8434684357

8434784358
let add = Lam_module_ident.Hash_set.add
8434884359

8434984360

8435084361

84362+
(* let is_pure_module (id : Lam_module_ident.t) =
84363+
match id.kind with
84364+
| Runtime -> true
84365+
| External _ -> false
84366+
| Ml ->
84367+
match Lam_module_ident.Hash.find_opt cached_tbl id with
84368+
| Some (Visit {cmj_table = {pure}}) -> pure
84369+
| Some _ -> assert false
84370+
| None -> *)
84371+
84372+
8435184373

8435284374
(* Conservative interface *)
8435384375
let is_pure_module (id : Lam_module_ident.t) =
@@ -87647,9 +87669,7 @@ let string_of_module_id
8764787669
module_system
8764887670
in
8764987671
match Lam_compile_env.get_package_path_from_cmj dep_module_id with
87650-
| None ->
87651-
Bs_exception.error (Missing_ml_dependency dep_module_id.id.name)
87652-
| Some (cmj_path, dep_package_info, little) ->
87672+
| (cmj_path, dep_package_info, little) ->
8765387673
let js_file = Ext_namespace.js_name_of_modulename dep_module_id.id.name little in
8765487674
let dep_info_query =
8765587675
Js_packages_info.query_package_infos dep_package_info module_system

0 commit comments

Comments
 (0)