Skip to content

Commit

Permalink
fix lazy evaluation of dynamic import
Browse files Browse the repository at this point in the history
  • Loading branch information
mununki committed Oct 9, 2022
1 parent 504adfc commit e864128
Show file tree
Hide file tree
Showing 14 changed files with 886 additions and 616 deletions.
2 changes: 1 addition & 1 deletion jscomp/core/js_implementation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ let after_parsing_impl ppf outputprefix (ast : Parsetree.structure) =
in
let js_program =
print_if_pipe ppf Clflags.dump_rawlambda Printlambda.lambda lambda
|> Lam_compile_main.compile outputprefix exports
|> Lam_compile_main.compile ~output_prefix:outputprefix exports
in
if not !Js_config.cmj_only then
Lam_compile_main.lambda_as_module js_program outputprefix);
Expand Down
336 changes: 200 additions & 136 deletions jscomp/core/lam_compile.ml

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions jscomp/core/lam_compile.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
(** Compile single lambda IR to JS IR *)

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

val compile_lambda :
?output_prefix:string -> Lam_compile_context.t -> Lam.t -> Js_output.t
output_prefix:string -> Lam_compile_context.t -> Lam.t -> Js_output.t
4 changes: 2 additions & 2 deletions jscomp/core/lam_compile_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ let compile_group ~output_prefix (meta : Lam_stats.t)
} lam

| Recursive id_lams ->
Lam_compile.compile_recursive_lets
Lam_compile.compile_recursive_lets ~output_prefix
{ continuation = EffectCall Not_tail;
jmp_table = Lam_compile_context.empty_handler_map;
meta
Expand Down Expand Up @@ -125,7 +125,7 @@ let _j = Js_pass_debug.dump
it's used or not
*)
let compile
(output_prefix : string)
~output_prefix
export_idents
(lam : Lambda.lambda) =
let export_ident_sets = Set_ident.of_list export_idents in
Expand Down
3 changes: 2 additions & 1 deletion jscomp/core/lam_compile_main.mli
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
(** Compile and register the hook of function to compile a lambda to JS IR
*)

val compile : string -> Ident.t list -> Lambda.lambda -> J.deps_program
val compile :
output_prefix:string -> Ident.t list -> Lambda.lambda -> J.deps_program
(** For toplevel, [filename] is [""] which is the same as
{!Env.get_unit_name ()}
*)
Expand Down
12 changes: 6 additions & 6 deletions jscomp/core/lam_compile_main.pp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
(* module S = Js_stmt_make *)


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

| Recursive id_lams ->
Lam_compile.compile_recursive_lets
Lam_compile.compile_recursive_lets ~output_prefix
{ continuation = EffectCall Not_tail;
jmp_table = Lam_compile_context.empty_handler_map;
meta
}
id_lams
| Nop lam -> (* TODO: Side effect callls, log and see statistics *)
Lam_compile.compile_lambda {continuation = EffectCall Not_tail;
Lam_compile.compile_lambda ~output_prefix {continuation = EffectCall Not_tail;
jmp_table = Lam_compile_context.empty_handler_map;
meta
} lam
Expand Down Expand Up @@ -122,7 +122,7 @@ let _j = Js_pass_debug.dump
it's used or not
*)
let compile
(output_prefix : string)
~output_prefix
export_idents
(lam : Lambda.lambda) =
let export_ident_sets = Set_ident.of_list export_idents in
Expand Down Expand Up @@ -222,7 +222,7 @@ let maybe_pure = no_side_effects groups in
let () = Ext_log.dwarn ~__POS__ "\n@[[TIME:]Pre-compile: %f@]@." (Sys.time () *. 1000.) in
#endif
let body =
Ext_list.map groups (fun group -> compile_group meta group)
Ext_list.map groups (fun group -> compile_group ~output_prefix meta group)
|> Js_output.concat
|> Js_output.output_as_block
in
Expand Down
4 changes: 1 addition & 3 deletions jscomp/main/builtin_cmi_datasets.ml

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions jscomp/main/builtin_cmj_datasets.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(* 407f68510a4eaffe1f5cc00fb994c5d4 *)
(* 640a8f7e09c7798341a26633c103d6f4 *)
let module_names : string array = Obj.magic (
"Js" (* 23 *),
"Arg" (* 217 *),
Expand Down Expand Up @@ -89,7 +89,6 @@ let module_names : string array = Obj.magic (
"BytesLabels" (* 878 *),
"Dom_storage" (* 386 *),
"Js_mapperRt" (* 87 *),
"Js_promise2" (* 50 *),
"JsxDOMStyle" (* 23 *),
"Node_buffer" (* 23 *),
"Node_module" (* 23 *),
Expand Down Expand Up @@ -225,7 +224,6 @@ let module_data : string array = Obj.magic (
(* 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",
(* 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",
(* 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",
(* 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",
(* JsxDOMStyle *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",
(* Node_buffer *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",
(* Node_module *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",
Expand Down
4 changes: 1 addition & 3 deletions jscomp/main/jsoo_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let implementation ~use_super_errors impl str : Js.Unsafe.obj =
let () =
Js_dump_program.pp_deps_program ~output_prefix:""
(* does not matter here *) NodeJS
(Lam_compile_main.compile "" exports lam)
(Lam_compile_main.compile ~output_prefix:"" exports lam)
(Ext_pp.from_buffer buffer)
in
let v = Buffer.contents buffer in
Expand All @@ -94,13 +94,11 @@ let implementation ~use_super_errors impl str : Js.Unsafe.obj =
obj [| ("js_error_msg", inject @@ Js.string (Printexc.to_string e)) |]))

let compile impl ~use_super_errors = implementation ~use_super_errors impl

let export (field : string) v = Js.Unsafe.set Js.Unsafe.global field v

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

let dir_directory d = Config.load_path := d :: !Config.load_path

let () = dir_directory "/static"

let make_compiler name impl =
Expand Down
2 changes: 1 addition & 1 deletion jscomp/main/jsoo_playground_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ module Compile = struct
let () = Js_dump_program.pp_deps_program
~output_prefix:"" (* does not matter here *)
module_system
(Lam_compile_main.compile "" exports lam)
(Lam_compile_main.compile ~output_prefix:"" exports lam)
(Ext_pp.from_buffer buffer) in
let v = Buffer.contents buffer in
let typeHints = collectTypeHints typed_tree in
Expand Down
18 changes: 18 additions & 0 deletions jscomp/test/Import.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,26 @@ async function eachIntAsync(list, f) {
return Curry._2(each$1, list, f);
}

function eachLazy(param) {
return import("../../lib/js/belt_List.js").then(function (m) {
return m.forEach;
});
}

function eachIntLazy(list, f) {
var obj = import("../../lib/js/belt_List.js").then(function (m) {
return m.forEach;
});
var arg1 = function (each) {
return Promise.resolve(Curry._2(each, list, f));
};
return obj.then(arg1);
}

exports.each = each;
exports.eachInt = eachInt;
exports.beltAsModule = beltAsModule;
exports.eachIntAsync = eachIntAsync;
exports.eachLazy = eachLazy;
exports.eachIntLazy = eachIntLazy;
/* No side effect */
370 changes: 216 additions & 154 deletions lib/4.06.1/unstable/js_compiler.ml

Large diffs are not rendered by default.

368 changes: 216 additions & 152 deletions lib/4.06.1/unstable/js_playground_compiler.ml

Large diffs are not rendered by default.

368 changes: 216 additions & 152 deletions lib/4.06.1/whole_compiler.ml

Large diffs are not rendered by default.

0 comments on commit e864128

Please sign in to comment.