Skip to content

Commit 5a10d0e

Browse files
authored
Merge pull request #3814 from BuckleScript/clean_lam_compile_env
[refactor] removing dependencies on typing environment
2 parents ed1b5c3 + 688731a commit 5a10d0e

28 files changed

+455
-3006
lines changed

jscomp/core/js_implementation.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ let after_parsing_impl ppf outputprefix ast =
141141
let env = Compmisc.initial_env() in
142142
Env.set_unit_name modulename;
143143

144-
let (typedtree, coercion, finalenv, current_signature) =
144+
let (typedtree, coercion, _, _) =
145145
ast
146146
|> Typemod.type_implementation_more ?check_exists:(if !Js_config.force_cmi then None else Some ()) !Location.input_name outputprefix modulename env
147147
|> print_if ppf Clflags.dump_typedtree
@@ -155,7 +155,7 @@ let after_parsing_impl ppf outputprefix ast =
155155
|> (fun lambda ->
156156
let js_program =
157157
print_if ppf Clflags.dump_rawlambda Printlambda.lambda (get_lambda lambda)
158-
|> Lam_compile_main.compile outputprefix finalenv in
158+
|> Lam_compile_main.compile outputprefix in
159159
if not !Js_config.cmj_only then
160160
Lam_compile_main.lambda_as_module
161161
js_program

jscomp/core/lam_arity_analysis.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ let rec get_arity (meta : Lam_stats.t) (lam : Lam.t) : Lam_arity.t =
4646
| Llet(_,_,_, l ) -> get_arity meta l
4747
| Lprim {primitive = Pfield (_, Fld_module fld_name);
4848
args = [ Lglobal_module id ]; _} ->
49-
begin match (Lam_compile_env.cached_find_ml_id_pos id fld_name).arity with
49+
begin match (Lam_compile_env.query_external_id_info id fld_name).arity with
5050
| Single x -> x
5151
| Submodule _ -> Lam_arity.na
5252
end
5353
| Lprim {primitive = Pfield (m,_);
5454
args = [ Lprim{primitive = Pfield(n,Fld_module fld_name);
5555
args = [ Lglobal_module id]} ]
5656
; _} ->
57-
begin match (Lam_compile_env.cached_find_ml_id_pos id fld_name ).arity with
57+
begin match (Lam_compile_env.query_external_id_info id fld_name ).arity with
5858
| Submodule subs -> subs.(m) (* TODO: shall we store it as array?*)
5959
| Single _ -> Lam_arity.na
6060
end

jscomp/core/lam_compile.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ let rec
159159
(id : Ident.t)
160160
pos
161161
: Js_output.t =
162-
match Lam_compile_env.cached_find_ml_id_pos id pos with
162+
match Lam_compile_env.query_external_id_info id pos with
163163
| { closed_lambda = Some lam}
164164
when Lam_util.not_function lam
165165
->
@@ -203,7 +203,7 @@ and compile_external_field_apply
203203
(lambda_cxt : Lam_compile_context.t): Js_output.t =
204204

205205
let ident_info =
206-
Lam_compile_env.cached_find_ml_id_pos id pos in
206+
Lam_compile_env.query_external_id_info id pos in
207207
let args_code, args =
208208
let dummy = [], [] in
209209
if args_lambda = [] then dummy

jscomp/core/lam_compile_env.ml

Lines changed: 49 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type ml_module_info = {
4040
}
4141

4242
type env_value =
43-
| Visit of ml_module_info
43+
| Ml of ml_module_info
4444
| Runtime of ml_module_info
4545
(**
4646
[Runtime (pure, path, cmj_format)]
@@ -56,7 +56,6 @@ type env_value =
5656

5757

5858
type ident_info = {
59-
(* id : Ident.t; *)
6059
name : string;
6160
arity : Js_cmj_format.arity;
6261
closed_lambda : Lam.t option
@@ -89,16 +88,16 @@ let reset () =
8988
*)
9089
let add_js_module
9190
(hint_name : External_ffi_types.module_bind_name)
92-
module_name : Ident.t
91+
(module_name : string) : Ident.t
9392
=
9493
let id =
95-
Ident.create @@
94+
Ident.create
9695
(match hint_name with
9796
| Phint_name hint_name ->
98-
Ext_string.capitalize_ascii hint_name
99-
(* make sure the module name is capitalized
100-
TODO: maybe a warning if the user hint is not good
101-
*)
97+
Ext_string.capitalize_ascii hint_name
98+
(* make sure the module name is capitalized
99+
TODO: maybe a warning if the user hint is not good
100+
*)
102101
| Phint_nothing ->
103102
Ext_modulename.js_id_name_of_hint_name module_name
104103
)
@@ -120,35 +119,28 @@ let add_js_module
120119

121120

122121

123-
let cached_find_ml_id_pos (module_id : Ident.t) name : ident_info =
122+
let query_external_id_info (module_id : Ident.t) (name : string) : ident_info =
124123
let oid = Lam_module_ident.of_ml module_id in
125-
match Lam_module_ident.Hash.find_opt cached_tbl oid with
126-
| None ->
127-
let cmj_path, cmj_table =
128-
Js_cmj_load.find_cmj_exn (module_id.name ^ Literals.suffix_cmj) in
129-
oid +> Visit { cmj_table ; cmj_path } ;
130-
let arity, closed_lambda =
131-
Js_cmj_format.query_by_name cmj_table name
132-
in
133-
{
134-
name ;
135-
arity ;
136-
closed_lambda
137-
}
138-
139-
| Some (Visit { cmj_table } )
140-
->
141-
let arity , closed_lambda =
142-
Js_cmj_format.query_by_name cmj_table name
143-
in
144-
{
145-
name;
146-
arity;
147-
closed_lambda
148-
(* TODO shall we cache the arity ?*)
149-
}
150-
| Some (Runtime _) -> assert false
151-
| Some External -> assert false
124+
let cmj_table =
125+
match Lam_module_ident.Hash.find_opt cached_tbl oid with
126+
| None ->
127+
let cmj_path, cmj_table =
128+
Js_cmj_load.find_cmj_exn (module_id.name ^ Literals.suffix_cmj) in
129+
oid +> Ml { cmj_table ; cmj_path } ;
130+
cmj_table
131+
| Some (Ml { cmj_table } )
132+
-> cmj_table
133+
| Some (Runtime _) -> assert false
134+
| Some External -> assert false in
135+
let arity , closed_lambda =
136+
Js_cmj_format.query_by_name cmj_table name
137+
in
138+
{
139+
name;
140+
arity;
141+
closed_lambda
142+
(* TODO shall we cache the arity ?*)
143+
}
152144

153145

154146

@@ -157,81 +149,46 @@ let cached_find_ml_id_pos (module_id : Ident.t) name : ident_info =
157149
[Runtime]
158150
and [externals]*)
159151
type _ t =
160-
| No_env : (path * Js_cmj_format.t) t
152+
| No_env : bool t
161153
| Has_env : Env.t -> bool t (* Indicate it is pure or not *)
162154

163155

164-
(* -FIXME:
165-
Here [not_found] only means cmi not found, not cmj not found *)
156+
(*
157+
FIXME:
158+
Here [not_found] only means cmi not found, not cmj not found
159+
We do need handle cases when [not_found] hit in a graceful way
160+
*)
166161
let query_and_add_if_not_exist
167162
(type u)
168163
(oid : Lam_module_ident.t)
169-
(env : u t) ~not_found ~(found: u -> _) =
164+
=
170165
match Lam_module_ident.Hash.find_opt cached_tbl oid with
171166
| None ->
172167
begin match oid.kind with
173168
| Runtime ->
174169
let (cmj_path, cmj_table) as cmj_info =
175170
Js_cmj_load.find_cmj_exn (Lam_module_ident.name oid ^ Literals.suffix_cmj) in
176171
oid +> Runtime {cmj_path;cmj_table} ;
177-
(match env with
178-
| Has_env _ ->
179-
found true
180-
| No_env ->
181-
found cmj_info)
172+
Js_cmj_format.is_pure cmj_table
182173
| Ml
183174
->
184175
let (cmj_path, cmj_table) as cmj_info =
185176
Js_cmj_load.find_cmj_exn (Lam_module_ident.name oid ^ Literals.suffix_cmj) in
186-
( match env with
187-
| Has_env env ->
188-
begin match
189-
Ocaml_types.find_serializable_signatures_by_path oid.id env with
190-
| None -> not_found () (* actually when [not_found] in the call site, we throw... *)
191-
| Some _ ->
192-
oid +> Visit {cmj_table;cmj_path } ;
193-
found (Js_cmj_format.is_pure cmj_table)
194-
end
195-
| No_env ->
196-
found cmj_info)
197-
198-
177+
oid +> Ml {cmj_table;cmj_path } ;
178+
Js_cmj_format.is_pure cmj_table
199179
| External _ ->
200180
oid +> External;
201181
(** This might be wrong, if we happen to expand an js module
202182
we should assert false (but this in general should not happen)
183+
FIXME: #154, it come from External, should be okay
203184
*)
204-
begin match env with
205-
| Has_env _
206-
->
207-
found false
208-
| No_env ->
209-
found (Ext_string.empty, Js_cmj_format.no_pure_dummy)
210-
(* FIXME: #154, it come from External, should be okay *)
211-
end
212-
213-
end
214-
| Some (Visit { cmj_table; cmj_path}) ->
215-
begin match env with
216-
| Has_env _ ->
217-
found (Js_cmj_format.is_pure cmj_table)
218-
| No_env -> found (cmj_path,cmj_table)
219-
end
220-
221-
| Some (Runtime {cmj_path; cmj_table}) ->
222-
begin match env with
223-
| Has_env _ ->
224-
found true
225-
| No_env ->
226-
found (cmj_path, cmj_table)
227-
end
228-
| Some External ->
229-
begin match env with
230-
| Has_env _ ->
231-
found false
232-
| No_env ->
233-
found (Ext_string.empty, Js_cmj_format.no_pure_dummy) (* External is okay *)
185+
false
234186
end
187+
| Some (Ml { cmj_table })
188+
| Some (Runtime {cmj_table}) ->
189+
Js_cmj_format.is_pure cmj_table
190+
| Some External -> false
191+
235192

236193

237194

@@ -240,7 +197,7 @@ let get_package_path_from_cmj
240197
( id : Lam_module_ident.t)
241198
=
242199
match Lam_module_ident.Hash.find_opt cached_tbl id with
243-
| Some (Visit {cmj_table ; cmj_path}) ->
200+
| Some (Ml {cmj_table ; cmj_path}) ->
244201
(cmj_path,
245202
Js_cmj_format.get_npm_package_path cmj_table,
246203
Js_cmj_format.get_cmj_case cmj_table )
@@ -258,7 +215,7 @@ let get_package_path_from_cmj
258215
| Ml ->
259216
let (cmj_path, cmj_table) =
260217
Js_cmj_load.find_cmj_exn (Lam_module_ident.name id ^ Literals.suffix_cmj) in
261-
id +> Visit {cmj_table;cmj_path };
218+
id +> Ml {cmj_table;cmj_path };
262219
(cmj_path,
263220
Js_cmj_format.get_npm_package_path cmj_table,
264221
Js_cmj_format.get_cmj_case cmj_table )
@@ -267,26 +224,11 @@ let get_package_path_from_cmj
267224
let add = Lam_module_ident.Hash_set.add
268225

269226

270-
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-
282-
283227
(* Conservative interface *)
284228
let is_pure_module (id : Lam_module_ident.t) =
285229
id.kind = Runtime ||
286-
query_and_add_if_not_exist id No_env
287-
~not_found:(fun _ -> false)
288-
~found:(fun (_,x) ->
289-
Js_cmj_format.is_pure x)
230+
query_and_add_if_not_exist id
231+
290232

291233
let get_required_modules
292234
extras

jscomp/core/lam_compile_env.mli

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type path = string
3939

4040

4141
type _ t =
42-
| No_env : (path * Js_cmj_format.t) t
42+
| No_env : bool t
4343
| Has_env : Env.t -> bool t
4444

4545

@@ -88,18 +88,14 @@ val add_js_module :
8888
pay attention to for those modules are actually used or not
8989
*)
9090
(**
91-
[cached_find_ml_id_pos id pos env found]
91+
[query_external_id_info id pos env found]
9292
will raise if not found
9393
*)
94-
val cached_find_ml_id_pos :
94+
val query_external_id_info :
9595
Ident.t ->
9696
string ->
9797
ident_info
9898

99-
val query_and_add_if_not_exist :
100-
Lam_module_ident.t ->
101-
'a t -> not_found:(unit -> 'b) ->
102-
found:('a -> 'b) -> 'b
10399

104100
val is_pure_module : Lam_module_ident.t -> bool
105101

0 commit comments

Comments
 (0)