Skip to content

Commit fe47e11

Browse files
authored
Merge pull request #3802 from BuckleScript/simplify_lam_compile_env
simplify get_package_path_from_cmj
2 parents 27fb7e6 + 09e822c commit fe47e11

File tree

8 files changed

+234
-186
lines changed

8 files changed

+234
-186
lines changed

jscomp/core/js_cmj_load.ml

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,35 @@
2626
If not installed, use the distributed [cmj] files,
2727
make sure that the distributed files are platform independent
2828
*)
29-
let find_cmj file : string * Js_cmj_format.t =
29+
30+
31+
32+
#if BS_COMPILER_IN_BROWSER then
33+
let find_cmj_exn file : string * Js_cmj_format.t =
34+
let target = Ext_string.uncapitalize_ascii (Filename.basename file) in
35+
match String_map.find_exn !Js_cmj_datasets.data_sets target with
36+
| v
37+
->
38+
begin match Lazy.force v with
39+
| exception _
40+
->
41+
Ext_log.warn __LOC__
42+
"@[%s corrupted in database, when looking %s while compiling %s please update @]" file target !Location.input_name ;
43+
Bs_exception.error (Cmj_not_found file)
44+
| v -> "BROWSER", v
45+
(* see {!Js_packages_info.string_of_module_id} *)
46+
end
47+
| exception Not_found
48+
->
49+
Bs_exception.error (Cmj_not_found file)
50+
#else
51+
let find_cmj_exn file : string * Js_cmj_format.t =
3052
match Config_util.find_opt file with
3153
| Some f
3254
->
3355
f, Js_cmj_format.from_file f
3456
| None ->
3557
(* ONLY read the stored cmj data in browser environment *)
36-
#if BS_COMPILER_IN_BROWSER then
37-
"BROWSER", (
38-
let target = Ext_string.uncapitalize_ascii (Filename.basename file) in
39-
match String_map.find_exn !Js_cmj_datasets.data_sets target with
40-
| v
41-
->
42-
begin match Lazy.force v with
43-
| exception _
44-
->
45-
Ext_log.warn __LOC__
46-
"@[%s corrupted in database, when looking %s while compiling %s please update @]" file target !Location.input_name ;
47-
Js_cmj_format.no_pure_dummy; (* FIXME *)
48-
| v -> v
49-
(* see {!Js_packages_info.string_of_module_id} *)
50-
end
51-
| exception Not_found
52-
->
53-
Ext_log.warn __LOC__ "@[%s not found @]" file ;
54-
Js_cmj_format.no_pure_dummy )
55-
#else
56-
Bs_exception.error (Cmj_not_found file)
57-
#end
58+
Bs_exception.error (Cmj_not_found file)
5859

60+
#end

jscomp/core/js_cmj_load.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030

3131

3232
(** return path and meta data *)
33-
val find_cmj : string -> string * Js_cmj_format.t
33+
val find_cmj_exn : string -> string * Js_cmj_format.t

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: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type ml_module_info = {
4141

4242
type env_value =
4343
| Visit of ml_module_info
44-
| Runtime of bool * path * Js_cmj_format.t
44+
| Runtime of ml_module_info
4545
(**
4646
[Runtime (pure, path, cmj_format)]
4747
A built in module probably from our runtime primitives,
@@ -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,14 +118,14 @@ 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
122125
match Lam_module_ident.Hash.find_opt cached_tbl oid with
123126
| None ->
124127
let cmj_path, cmj_table =
125-
Js_cmj_load.find_cmj (module_id.name ^ Literals.suffix_cmj) in
128+
Js_cmj_load.find_cmj_exn (module_id.name ^ Literals.suffix_cmj) in
126129
oid +> Visit { cmj_table ; cmj_path } ;
127130
let arity, closed_lambda =
128131
Js_cmj_format.query_by_name cmj_table name
@@ -149,15 +152,13 @@ let cached_find_ml_id_pos (module_id : Ident.t) name : ident_info =
149152

150153

151154

152-
type module_info = {
153-
pure : bool
154-
}
155+
155156
(* TODO: it does not make sense to cache
156157
[Runtime]
157158
and [externals]*)
158159
type _ t =
159160
| No_env : (path * Js_cmj_format.t) t
160-
| Has_env : Env.t -> module_info t
161+
| Has_env : Env.t -> bool t (* Indicate it is pure or not *)
161162

162163

163164
(* -FIXME:
@@ -171,25 +172,25 @@ let query_and_add_if_not_exist
171172
begin match oid.kind with
172173
| Runtime ->
173174
let (cmj_path, cmj_table) as cmj_info =
174-
Js_cmj_load.find_cmj (Lam_module_ident.name oid ^ Literals.suffix_cmj) in
175-
oid +> Runtime (true,cmj_path,cmj_table) ;
175+
Js_cmj_load.find_cmj_exn (Lam_module_ident.name oid ^ Literals.suffix_cmj) in
176+
oid +> Runtime {cmj_path;cmj_table} ;
176177
(match env with
177178
| Has_env _ ->
178-
found { pure = true}
179+
found true
179180
| No_env ->
180181
found cmj_info)
181182
| Ml
182183
->
183184
let (cmj_path, cmj_table) as cmj_info =
184-
Js_cmj_load.find_cmj (Lam_module_ident.name oid ^ Literals.suffix_cmj) in
185+
Js_cmj_load.find_cmj_exn (Lam_module_ident.name oid ^ Literals.suffix_cmj) in
185186
( match env with
186187
| Has_env env ->
187188
begin match
188189
Ocaml_types.find_serializable_signatures_by_path oid.id env with
189190
| None -> not_found () (* actually when [not_found] in the call site, we throw... *)
190-
| Some signature ->
191+
| Some _ ->
191192
oid +> Visit {cmj_table;cmj_path } ;
192-
found { pure = Js_cmj_format.is_pure cmj_table}
193+
found (Js_cmj_format.is_pure cmj_table)
193194
end
194195
| No_env ->
195196
found cmj_info)
@@ -203,7 +204,7 @@ let query_and_add_if_not_exist
203204
begin match env with
204205
| Has_env _
205206
->
206-
found { pure = false}
207+
found false
207208
| No_env ->
208209
found (Ext_string.empty, Js_cmj_format.no_pure_dummy)
209210
(* FIXME: #154, it come from External, should be okay *)
@@ -213,21 +214,21 @@ let query_and_add_if_not_exist
213214
| Some (Visit { cmj_table; cmj_path}) ->
214215
begin match env with
215216
| Has_env _ ->
216-
found { pure = Js_cmj_format.is_pure cmj_table}
217+
found (Js_cmj_format.is_pure cmj_table)
217218
| No_env -> found (cmj_path,cmj_table)
218219
end
219220

220-
| Some (Runtime (pure, cmj_path,cmj_table)) ->
221+
| Some (Runtime {cmj_path; cmj_table}) ->
221222
begin match env with
222223
| Has_env _ ->
223-
found {pure }
224+
found true
224225
| No_env ->
225226
found (cmj_path, cmj_table)
226227
end
227228
| Some External ->
228229
begin match env with
229230
| Has_env _ ->
230-
found {pure = false}
231+
found false
231232
| No_env ->
232233
found (Ext_string.empty, Js_cmj_format.no_pure_dummy) (* External is okay *)
233234
end
@@ -237,26 +238,47 @@ let query_and_add_if_not_exist
237238

238239
let get_package_path_from_cmj
239240
( 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-
241+
=
242+
match Lam_module_ident.Hash.find_opt cached_tbl id with
243+
| Some (Visit {cmj_table ; cmj_path}) ->
244+
(cmj_path,
245+
Js_cmj_format.get_npm_package_path cmj_table,
246+
Js_cmj_format.get_cmj_case cmj_table )
247+
| Some (
248+
External |
249+
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_exn (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: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@ type path = string
3737

3838

3939

40-
type module_info = {
41-
pure : bool
42-
}
4340

4441
type _ t =
4542
| No_env : (path * Js_cmj_format.t) t
46-
| Has_env : Env.t -> module_info t
43+
| Has_env : Env.t -> bool t
4744

4845

4946
type ident_info = {
@@ -109,7 +106,7 @@ val is_pure_module : Lam_module_ident.t -> bool
109106

110107
val get_package_path_from_cmj :
111108
Lam_module_ident.t ->
112-
(string * Js_packages_info.t * Js_cmj_format.cmj_case) option
109+
(string * Js_packages_info.t * Js_cmj_format.cmj_case)
113110

114111

115112

jscomp/core/lam_stats_export.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ let get_dependent_module_effect
113113
Lam_compile_env.query_and_add_if_not_exist id
114114
(Has_env meta.env )
115115
~not_found:(fun _ -> false )
116-
~found:(fun {pure} -> pure)
116+
~found:(fun pure -> pure)
117117
) in
118118
Ext_option.map non_pure_module (fun x -> Lam_module_ident.name x)
119119
else

0 commit comments

Comments
 (0)