Skip to content

Commit 1ec3a41

Browse files
mununkicristianoc
andauthored
Dynamic import (#5703)
Co-authored-by: Cristiano Calcagno <cristianoc@users.noreply.github.com>
1 parent 2b6df7b commit 1ec3a41

26 files changed

+440
-100
lines changed

jscomp/core/js_packages_info.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ let is_runtime_package (x : t) = x.name = Pkg_runtime
8484

8585
let iter (x : t) cb = Ext_list.iter x.module_systems cb
8686

87+
let map (x : t) cb = Ext_list.map x.module_systems cb
88+
8789
(* let equal (x : t) ({name; module_systems}) =
8890
x.name = name &&
8991
Ext_list.for_all2_no_exn

jscomp/core/js_packages_info.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ val same_package_by_name : t -> t -> bool
4646

4747
val iter : t -> (package_info -> unit) -> unit
4848

49+
val map : t -> (package_info -> 'a) -> 'a list
50+
4951
val empty : t
5052

5153
val from_name : string -> t

jscomp/core/lam_analysis.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
6060
| Pcreate_extension _ | Pjs_typeof | Pis_null | Pis_not_none | Psome
6161
| Psome_not_nest | Pis_undefined | Pis_null_undefined | Pnull_to_opt
6262
| Pundefined_to_opt | Pnull_undefined_to_opt | Pjs_fn_make _ | Pjs_fn_make_unit
63-
| Pjs_object_create _
63+
| Pjs_object_create _ | Pimport
6464
(* TODO: check *)
6565
| Pbytes_to_string | Pmakeblock _
6666
(* whether it's mutable or not *)

jscomp/core/lam_compile.ml

Lines changed: 68 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ let has_null_undefined_other (sw_names : Ast_untagged_variants.switch_names opti
193193
let no_effects_const = lazy true
194194
(* let has_effects_const = lazy false *)
195195

196-
(** We drop the ability of cross-compiling
196+
(* We drop the ability of cross-compiling
197197
the compiler has to be the same running
198198
*)
199199

@@ -224,6 +224,9 @@ type initialization = J.block
224224
-: we should not do functor application inlining in a
225225
non-toplevel, it will explode code very quickly
226226
*)
227+
228+
let compile output_prefix =
229+
227230
let rec compile_external_field (* Like [List.empty]*)
228231
(lamba_cxt : Lam_compile_context.t) (id : Ident.t) name : Js_output.t =
229232
match Lam_compile_env.query_external_id_info id name with
@@ -249,7 +252,7 @@ let rec compile_external_field (* Like [List.empty]*)
249252
@param args arguments
250253
*)
251254

252-
(** This can not happen since this id should be already consulted by type checker
255+
(* This can not happen since this id should be already consulted by type checker
253256
Worst case
254257
{[
255258
E.array_index_by_int m pos
@@ -304,7 +307,7 @@ and compile_external_field_apply (appinfo : Lam.apply) (module_id : Ident.t)
304307
Js_output.output_of_block_and_expression lambda_cxt.continuation args_code
305308
expression
306309

307-
(**
310+
(*
308311
The second return values are values which need to be wrapped using
309312
[update_dummy]
310313
@@ -500,29 +503,29 @@ and compile_recursive_lets cxt id_args : Js_output.t =
500503

501504
and compile_general_cases :
502505
'a .
503-
('a -> Ast_untagged_variants.literal option) ->
504-
('a -> J.expression) ->
505-
('a option -> J.expression -> 'a option -> J.expression -> J.expression) ->
506-
Lam_compile_context.t ->
507-
(?default:J.block ->
508-
?declaration:Lam_compat.let_kind * Ident.t ->
509-
_ ->
510-
('a * J.case_clause) list ->
511-
J.statement) ->
512-
_ ->
513-
('a * Lam.t) list ->
514-
default_case ->
506+
get_cstr_name: ('a -> Ast_untagged_variants.literal option) ->
507+
make_exp: ('a -> J.expression) ->
508+
eq_exp: ('a option -> J.expression -> 'a option -> J.expression -> J.expression) ->
509+
cxt: Lam_compile_context.t ->
510+
switch: (?default:J.block -> ?declaration:Lam_compat.let_kind * Ident.t ->
511+
_ -> ('a * J.case_clause) list -> J.statement) ->
512+
switch_exp: J.expression ->
513+
cases: ('a * Lam.t) list ->
514+
default: default_case ->
515515
J.block =
516-
fun (get_cstr_name : _ -> Ast_untagged_variants.literal option) (make_exp : _ -> J.expression)
517-
(eq_exp : 'a option -> J.expression -> 'a option -> J.expression -> J.expression)
518-
(cxt : Lam_compile_context.t)
519-
(switch :
520-
?default:J.block ->
521-
?declaration:Lam_compat.let_kind * Ident.t ->
522-
_ ->
523-
(_ * J.case_clause) list ->
524-
J.statement) (switch_exp : J.expression) (cases : (_ * Lam.t) list)
525-
(default : default_case) ->
516+
fun (type a)
517+
~(get_cstr_name : a -> Ast_untagged_variants.literal option)
518+
~(make_exp : a -> J.expression)
519+
~(eq_exp : a option -> J.expression -> a option -> J.expression -> J.expression)
520+
~(cxt : Lam_compile_context.t)
521+
~(switch :
522+
?default:J.block ->
523+
?declaration:Lam_compat.let_kind * Ident.t ->
524+
_ -> (a * J.case_clause) list -> J.statement
525+
)
526+
~(switch_exp : J.expression)
527+
~(cases : (a * Lam.t) list)
528+
~(default : default_case) ->
526529
match (cases, default) with
527530
| [], Default lam -> Js_output.output_as_block (compile_lambda cxt lam)
528531
| [], (Complete | NonComplete) -> []
@@ -538,6 +541,7 @@ and compile_general_cases :
538541
morph_declare_to_assign cxt (fun cxt define ->
539542
[
540543
S.if_ ?declaration:define
544+
541545
(eq_exp None switch_exp (Some id) (make_exp id))
542546
(Js_output.output_as_block (compile_lambda cxt lam));
543547
])
@@ -624,22 +628,26 @@ and use_compile_literal_cases table get_name =
624628
| Some {name; literal_type = None}, Some string_table -> Some ((String name, lam) :: string_table)
625629
| _, _ -> None
626630
) table (Some [])
627-
and compile_cases ?(untagged=false) cxt (switch_exp : E.t) table default get_name =
631+
and compile_cases ?(untagged=false) cxt (switch_exp : E.t) table default get_name : initialization =
628632
match use_compile_literal_cases table get_name with
629633
| Some string_table ->
630634
if untagged
631635
then compile_untagged_cases cxt switch_exp string_table default
632636
else compile_string_cases cxt switch_exp string_table default
633637
| None ->
634-
compile_general_cases get_name
635-
(fun i -> match get_name i with
638+
compile_general_cases
639+
~get_cstr_name:get_name
640+
~make_exp:(fun i -> match get_name i with
636641
| None -> E.small_int i
637642
| Some {literal_type = Some(String s)} -> E.str s
638643
| Some {name} -> E.str name)
639-
(fun _ x _ y -> E.int_equal x y) cxt
640-
(fun ?default ?declaration e clauses ->
644+
~eq_exp: (fun _ x _ y -> E.int_equal x y)
645+
~cxt
646+
~switch: (fun ?default ?declaration e clauses ->
641647
S.int_switch ?default ?declaration e clauses)
642-
switch_exp table default
648+
~switch_exp
649+
~cases:table
650+
~default
643651

644652
and compile_switch (switch_arg : Lam.t) (sw : Lam.lambda_switch)
645653
(lambda_cxt : Lam_compile_context.t) =
@@ -691,6 +699,7 @@ and compile_switch (switch_arg : Lam.t) (sw : Lam.lambda_switch)
691699
else
692700
(* [e] will be used twice *)
693701
let dispatch e =
702+
694703
let is_a_literal_case =
695704
if block_cases <> []
696705
then
@@ -728,22 +737,22 @@ and compile_switch (switch_arg : Lam.t) (sw : Lam.lambda_switch)
728737
:: compile_whole { lambda_cxt with continuation = Assign id })
729738
| EffectCall _ | Assign _ -> Js_output.make (compile_whole lambda_cxt)
730739

731-
and compile_string_cases cxt switch_exp table default =
732-
let literal = function
740+
741+
and compile_string_cases cxt switch_exp cases default : initialization =
742+
let literal = function
733743
| literal -> E.literal literal
734744
in
735745
compile_general_cases
736-
(fun _ -> None)
737-
literal
738-
(fun _ x _ y -> E.string_equal x y)
739-
cxt
740-
(fun ?default ?declaration e clauses ->
746+
~get_cstr_name:(fun _ -> None)
747+
~make_exp:literal
748+
~eq_exp: (fun _ x _ y -> E.string_equal x y)
749+
~cxt
750+
~switch: (fun ?default ?declaration e clauses ->
741751
S.string_switch ?default ?declaration e clauses)
742-
switch_exp table default
743-
and compile_untagged_cases cxt switch_exp table default =
744-
let literal = function
745-
| literal -> E.literal literal
746-
in
752+
~switch_exp
753+
~cases
754+
~default
755+
and compile_untagged_cases cxt switch_exp cases default =
747756
let add_runtime_type_check (literal: Ast_untagged_variants.literal_type) x y = match literal with
748757
| Block IntType
749758
| Block StringType
@@ -762,7 +771,7 @@ and compile_untagged_cases cxt switch_exp table default =
762771
| _ -> E.string_equal x y
763772
in
764773
let is_array (l, _) = l = Ast_untagged_variants.Block Array in
765-
let body ?default ?declaration e clauses =
774+
let switch ?default ?declaration e clauses =
766775
let array_clauses = Ext_list.filter clauses is_array in
767776
match array_clauses with
768777
| [(l, {J.switch_body})] when List.length clauses > 1 ->
@@ -774,12 +783,14 @@ and compile_untagged_cases cxt switch_exp table default =
774783
| _ ->
775784
S.string_switch ?default ?declaration (E.typeof e) clauses in
776785
compile_general_cases
777-
(fun _ -> None)
778-
literal
779-
mk_eq
780-
cxt
781-
body
782-
switch_exp table default
786+
~get_cstr_name:(fun _ -> None)
787+
~make_exp:E.literal
788+
~eq_exp: mk_eq
789+
~cxt
790+
~switch
791+
~switch_exp
792+
~cases
793+
~default
783794

784795
and compile_stringswitch l cases default (lambda_cxt : Lam_compile_context.t) =
785796
(* TODO might better optimization according to the number of cases
@@ -1077,7 +1088,7 @@ and compile_while (predicate : Lam.t) (body : Lam.t)
10771088
Js_output.output_of_block_and_expression lambda_cxt.continuation block
10781089
E.unit
10791090

1080-
(** all non-tail
1091+
(* all non-tail
10811092
TODO: check semantics should start, finish be executed each time in both
10821093
ocaml and js?, also check evaluation order..
10831094
in ocaml id is not in the scope of finish, so it should be safe here
@@ -1661,7 +1672,7 @@ and compile_prim (prim_info : Lam.prim_info)
16611672
let args_code : J.block = List.concat args_block in
16621673
let exp =
16631674
(* TODO: all can be done in [compile_primitive] *)
1664-
Lam_compile_primitive.translate loc lambda_cxt primitive args_expr
1675+
Lam_compile_primitive.translate output_prefix loc lambda_cxt primitive args_expr
16651676
in
16661677
Js_output.output_of_block_and_expression lambda_cxt.continuation args_code
16671678
exp
@@ -1758,3 +1769,8 @@ and compile_lambda (lambda_cxt : Lam_compile_context.t) (cur_lam : Lam.t) :
17581769
| Ltrywith (lam, id, catch) ->
17591770
(* generate documentation *)
17601771
compile_trywith lam id catch lambda_cxt
1772+
1773+
in compile_recursive_lets, compile_lambda
1774+
1775+
let compile_recursive_lets ~output_prefix = fst (compile output_prefix)
1776+
let compile_lambda ~output_prefix = snd (compile output_prefix)

jscomp/core/lam_compile.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
(** Compile single lambda IR to JS IR *)
2626

2727
val compile_recursive_lets :
28-
Lam_compile_context.t -> (Ident.t * Lam.t) list -> Js_output.t
28+
output_prefix:string -> Lam_compile_context.t -> (Ident.t * Lam.t) list -> Js_output.t
2929

30-
val compile_lambda : Lam_compile_context.t -> Lam.t -> Js_output.t
30+
val compile_lambda : output_prefix:string -> Lam_compile_context.t -> Lam.t -> Js_output.t

jscomp/core/lam_compile_main.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
(* module S = Js_stmt_make *)
3434

3535

36-
let compile_group (meta : Lam_stats.t)
36+
let compile_group output_prefix (meta : Lam_stats.t)
3737
(x : Lam_group.t) : Js_output.t =
3838
match x with
3939
(*
@@ -60,20 +60,20 @@ let compile_group (meta : Lam_stats.t)
6060
(* let lam = Optimizer.simplify_lets [] lam in *)
6161
(* can not apply again, it's wrong USE it with care*)
6262
(* ([Js_stmt_make.comment (Gen_of_env.query_type id env )], None) ++ *)
63-
Lam_compile.compile_lambda { continuation = Declare (kind, id);
63+
Lam_compile.compile_lambda ~output_prefix { continuation = Declare (kind, id);
6464
jmp_table = Lam_compile_context.empty_handler_map;
6565
meta
6666
} lam
6767

6868
| Recursive id_lams ->
69-
Lam_compile.compile_recursive_lets
69+
Lam_compile.compile_recursive_lets ~output_prefix
7070
{ continuation = EffectCall Not_tail;
7171
jmp_table = Lam_compile_context.empty_handler_map;
7272
meta
7373
}
7474
id_lams
7575
| Nop lam -> (* TODO: Side effect callls, log and see statistics *)
76-
Lam_compile.compile_lambda {continuation = EffectCall Not_tail;
76+
Lam_compile.compile_lambda ~output_prefix {continuation = EffectCall Not_tail;
7777
jmp_table = Lam_compile_context.empty_handler_map;
7878
meta
7979
} lam
@@ -122,7 +122,7 @@ let _j = Js_pass_debug.dump
122122
it's used or not
123123
*)
124124
let compile
125-
(output_prefix : string)
125+
(output_prefix : string)
126126
export_idents
127127
(lam : Lambda.lambda) =
128128
let export_ident_sets = Set_ident.of_list export_idents in
@@ -222,7 +222,7 @@ let maybe_pure = no_side_effects groups in
222222
let () = Ext_log.dwarn ~__POS__ "\n@[[TIME:]Pre-compile: %f@]@." (Sys.time () *. 1000.) in
223223
#endif
224224
let body =
225-
Ext_list.map groups (fun group -> compile_group meta group)
225+
Ext_list.map groups (fun group -> compile_group output_prefix meta group)
226226
|> Js_output.concat
227227
|> Js_output.output_as_block
228228
in
@@ -292,13 +292,13 @@ let lambda_as_module
292292
: unit =
293293
let package_info = Js_packages_state.get_packages_info () in
294294
if Js_packages_info.is_empty package_info && !Js_config.js_stdout then begin
295-
Js_dump_program.dump_deps_program ~output_prefix NodeJS lambda_output stdout
295+
Js_dump_program.dump_deps_program ~output_prefix NodeJS (lambda_output) stdout
296296
end else
297297
Js_packages_info.iter package_info (fun {module_system; path; suffix} ->
298298
let output_chan chan =
299299
Js_dump_program.dump_deps_program ~output_prefix
300300
module_system
301-
lambda_output
301+
(lambda_output)
302302
chan in
303303
let basename =
304304
Ext_namespace.change_ext_ns_suffix

0 commit comments

Comments
 (0)