Skip to content

Commit e864128

Browse files
committed
fix lazy evaluation of dynamic import
1 parent 504adfc commit e864128

14 files changed

+886
-616
lines changed

jscomp/core/js_implementation.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ let after_parsing_impl ppf outputprefix (ast : Parsetree.structure) =
163163
in
164164
let js_program =
165165
print_if_pipe ppf Clflags.dump_rawlambda Printlambda.lambda lambda
166-
|> Lam_compile_main.compile outputprefix exports
166+
|> Lam_compile_main.compile ~output_prefix:outputprefix exports
167167
in
168168
if not !Js_config.cmj_only then
169169
Lam_compile_main.lambda_as_module js_program outputprefix);

jscomp/core/lam_compile.ml

Lines changed: 200 additions & 136 deletions
Large diffs are not rendered by default.

jscomp/core/lam_compile.mli

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
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 ->
29+
Lam_compile_context.t ->
30+
(Ident.t * Lam.t) list ->
31+
Js_output.t
2932

3033
val compile_lambda :
31-
?output_prefix:string -> Lam_compile_context.t -> Lam.t -> Js_output.t
34+
output_prefix:string -> Lam_compile_context.t -> Lam.t -> Js_output.t

jscomp/core/lam_compile_main.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ let compile_group ~output_prefix (meta : Lam_stats.t)
6767
} lam
6868

6969
| Recursive id_lams ->
70-
Lam_compile.compile_recursive_lets
70+
Lam_compile.compile_recursive_lets ~output_prefix
7171
{ continuation = EffectCall Not_tail;
7272
jmp_table = Lam_compile_context.empty_handler_map;
7373
meta
@@ -125,7 +125,7 @@ let _j = Js_pass_debug.dump
125125
it's used or not
126126
*)
127127
let compile
128-
(output_prefix : string)
128+
~output_prefix
129129
export_idents
130130
(lam : Lambda.lambda) =
131131
let export_ident_sets = Set_ident.of_list export_idents in

jscomp/core/lam_compile_main.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
(** Compile and register the hook of function to compile a lambda to JS IR
2828
*)
2929

30-
val compile : string -> Ident.t list -> Lambda.lambda -> J.deps_program
30+
val compile :
31+
output_prefix:string -> Ident.t list -> Lambda.lambda -> J.deps_program
3132
(** For toplevel, [filename] is [""] which is the same as
3233
{!Env.get_unit_name ()}
3334
*)

jscomp/core/lam_compile_main.pp.ml

Lines changed: 6 additions & 6 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
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

jscomp/main/builtin_cmi_datasets.ml

Lines changed: 1 addition & 3 deletions
Large diffs are not rendered by default.

jscomp/main/builtin_cmj_datasets.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(* 407f68510a4eaffe1f5cc00fb994c5d4 *)
1+
(* 640a8f7e09c7798341a26633c103d6f4 *)
22
let module_names : string array = Obj.magic (
33
"Js" (* 23 *),
44
"Arg" (* 217 *),
@@ -89,7 +89,6 @@ let module_names : string array = Obj.magic (
8989
"BytesLabels" (* 878 *),
9090
"Dom_storage" (* 386 *),
9191
"Js_mapperRt" (* 87 *),
92-
"Js_promise2" (* 50 *),
9392
"JsxDOMStyle" (* 23 *),
9493
"Node_buffer" (* 23 *),
9594
"Node_module" (* 23 *),
@@ -225,7 +224,6 @@ let module_data : string array = Obj.magic (
225224
(* BytesLabels *)"\132\149\166\190\000\000\003Z\000\000\000\231\000\000\003\012\000\000\002\216\160\b\000\000\152\000\176#cat\144\160\160B@@@\176#map\144\160\160B@@@\176#sub\144\160\160C@@@\176$blit\144\160\160E@@@\176$copy\144\160\160A@@@\176$fill\144\160\160D@@@\176$init\144\160\160B@@@\176$iter\144\160\160B@@@\176$make\144\160\160B@@@\176$mapi\144\160\160B@@@\176$trim\144\160\160A@@@\176%equal\144\160\160B@@\144\148\192B\160\176\001\005z!x@\160\176\001\005{!y@@\151\176\1470caml_bytes_equal\160\144\004\011\160\144\004\n@\176\1929stdlib-406/bytesLabels.ml\001\001\171\0011\127\0011\155\192\004\002\001\001\171\0011\127\0011\160@\208B@@@@\176%index\144\160\160B@@@\176%iteri\144\160\160B@@@\176&concat\144\160\160B@@@\176&extend\144\160\160C@@@\176&rindex\144\160\160B@@@\176'compare\144\160\160B@@\144\148\192B\160\176\001\005w!x@\160\176\001\005x!y@@\151\176\1472caml_bytes_compare\160\144\004\011\160\144\004\n@\176\192\0044\001\001\170\0011L\0011h\192\0045\001\001\170\0011L\0011~@\208B@@@@\176'escaped\144\160\160A@@@\176(contains\144\160\160B@@@\176)index_opt\144\160\160B@@@\176)of_string\144\160\160A@@@\176)to_string\144\160\160A@@@\176*index_from\144\160\160C@@@\176*rindex_opt\144\160\160B@@@\176*sub_string\144\160\160C@@@\176+blit_string\144\160\160E@@@\176+rindex_from\144\160\160C@@@\176-contains_from\144\160\160C@@@\176.index_from_opt\144\160\160C@@@\176.rcontains_from\144\160\160C@@@\176/lowercase_ascii\144\160\160A@@@\176/rindex_from_opt\144\160\160C@@@\176/uppercase_ascii\144\160\160A@@@\1760capitalize_ascii\144\160\160A@@@\1760unsafe_of_string\144\004F@\1760unsafe_to_string\144\004D@\1762uncapitalize_ascii\144\160\160A@@@A",
226225
(* Dom_storage *)"\132\149\166\190\000\000\001n\000\000\000k\000\000\001^\000\000\001T\160\192\176#key\144\160\160B@@\144\148\192B\160\176\001\004\004!i@\160\176\001\004\005#obj@@\151\176\000B\160\151\176\180#key\160\160AA\160\004\002@\181#key@@\160\144\004\016\160\144\004\021@\176\1925others/dom_storage.mlR\001\003\022\001\0036\192\004\002R\001\003\022\001\003B@@\004\004\208B@@@@\176'getItem\144\160\160B@@\144\148\192B\160\176\001\003\246!s@\160\176\001\003\247#obj@@\151\176\000B\160\151\176\180'getItem\160\004#\160\004$@\181'getItem@@\160\144\004\015\160\144\004\020@\176\192\004\"D\000x\001\000\140\192\004#D\000x\001\000\156@@\004\003\208B@@@@\176'setItem\144\160\160C@@@\176*removeItem\144\160\160B@@\144\148\192B\160\176\001\003\255!s@\160\176\001\004\000#obj@@\174\151\176\180*removeItem\160\004G\160\004H@\181*removeItem@@\160\144\004\r\160\144\004\018@\176\192\004FL\001\001\208\001\001\238\192\004GL\001\001\208\001\002\001@\146A\208B@@A@A",
227226
(* Js_mapperRt *)"\132\149\166\190\000\000\000C\000\000\000\017\000\000\0009\000\000\0004\160\176\176'fromInt\144\160\160C@@@\176-fromIntAssert\144\160\160C@@@\1761raiseWhenNotFound\144\160\160A@@@A",
228-
(* Js_promise2 *)"\132\149\166\190\000\000\000\030\000\000\000\012\000\000\000$\000\000\000\"\160\160\176$then\144\160\160B@@@\176%catch\144\160\160B@@@A",
229227
(* JsxDOMStyle *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",
230228
(* Node_buffer *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",
231229
(* Node_module *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",

jscomp/main/jsoo_main.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ let implementation ~use_super_errors impl str : Js.Unsafe.obj =
7878
let () =
7979
Js_dump_program.pp_deps_program ~output_prefix:""
8080
(* does not matter here *) NodeJS
81-
(Lam_compile_main.compile "" exports lam)
81+
(Lam_compile_main.compile ~output_prefix:"" exports lam)
8282
(Ext_pp.from_buffer buffer)
8383
in
8484
let v = Buffer.contents buffer in
@@ -94,13 +94,11 @@ let implementation ~use_super_errors impl str : Js.Unsafe.obj =
9494
obj [| ("js_error_msg", inject @@ Js.string (Printexc.to_string e)) |]))
9595

9696
let compile impl ~use_super_errors = implementation ~use_super_errors impl
97-
9897
let export (field : string) v = Js.Unsafe.set Js.Unsafe.global field v
9998

10099
(* To add a directory to the load path *)
101100

102101
let dir_directory d = Config.load_path := d :: !Config.load_path
103-
104102
let () = dir_directory "/static"
105103

106104
let make_compiler name impl =

jscomp/main/jsoo_playground_main.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ module Compile = struct
484484
let () = Js_dump_program.pp_deps_program
485485
~output_prefix:"" (* does not matter here *)
486486
module_system
487-
(Lam_compile_main.compile "" exports lam)
487+
(Lam_compile_main.compile ~output_prefix:"" exports lam)
488488
(Ext_pp.from_buffer buffer) in
489489
let v = Buffer.contents buffer in
490490
let typeHints = collectTypeHints typed_tree in

jscomp/test/Import.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,26 @@ async function eachIntAsync(list, f) {
2020
return Curry._2(each$1, list, f);
2121
}
2222

23+
function eachLazy(param) {
24+
return import("../../lib/js/belt_List.js").then(function (m) {
25+
return m.forEach;
26+
});
27+
}
28+
29+
function eachIntLazy(list, f) {
30+
var obj = import("../../lib/js/belt_List.js").then(function (m) {
31+
return m.forEach;
32+
});
33+
var arg1 = function (each) {
34+
return Promise.resolve(Curry._2(each, list, f));
35+
};
36+
return obj.then(arg1);
37+
}
38+
2339
exports.each = each;
2440
exports.eachInt = eachInt;
2541
exports.beltAsModule = beltAsModule;
2642
exports.eachIntAsync = eachIntAsync;
43+
exports.eachLazy = eachLazy;
44+
exports.eachIntLazy = eachIntLazy;
2745
/* No side effect */

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 216 additions & 154 deletions
Large diffs are not rendered by default.

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 216 additions & 152 deletions
Large diffs are not rendered by default.

lib/4.06.1/whole_compiler.ml

Lines changed: 216 additions & 152 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)