Skip to content

Commit 38a0dd1

Browse files
committed
rough implemetation for dynamic import for module
1 parent 6524cb2 commit 38a0dd1

File tree

7 files changed

+334
-13
lines changed

7 files changed

+334
-13
lines changed

jscomp/frontend/ast_await.ml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,74 @@ let create_await_expression (e : Parsetree.expression) =
44
in
55
let pexp_desc = Parsetree.Pexp_ident { txt; loc = e.pexp_loc } in
66
{ e with pexp_desc = Pexp_apply ({ e with pexp_desc }, [ (Nolabel, e) ]) }
7+
8+
let create_await_module_expression (e : Parsetree.module_expr) =
9+
let txt = Longident.Ldot (Lident "Js", "import") in
10+
let _module_lid =
11+
match e with { pmod_desc = Pmod_ident lid } -> lid | _ -> assert false
12+
in
13+
let module_expr_without_attr = { e with pmod_attributes = [] } in
14+
let import_pexp_desc = Parsetree.Pexp_ident { txt; loc = e.pmod_loc } in
15+
let txt =
16+
Longident.Ldot (Longident.Ldot (Lident "Js", "Promise"), "unsafe_await")
17+
in
18+
let await_pexp_desc = Parsetree.Pexp_ident { txt; loc = e.pmod_loc } in
19+
{
20+
module_expr_without_attr with
21+
pmod_desc =
22+
Pmod_unpack
23+
{
24+
pexp_desc =
25+
Pexp_apply
26+
( {
27+
pexp_desc = await_pexp_desc;
28+
pexp_attributes = [];
29+
pexp_loc = e.pmod_loc;
30+
},
31+
[
32+
( Nolabel,
33+
{
34+
pexp_desc =
35+
Pexp_apply
36+
( {
37+
pexp_desc = import_pexp_desc;
38+
pexp_attributes = [];
39+
pexp_loc = e.pmod_loc;
40+
},
41+
[
42+
( Nolabel,
43+
{
44+
pexp_desc =
45+
Pexp_constraint
46+
( {
47+
pexp_desc =
48+
Pexp_pack module_expr_without_attr;
49+
pexp_loc = e.pmod_loc;
50+
pexp_attributes = [];
51+
},
52+
{
53+
ptyp_desc =
54+
Ptyp_package
55+
( {
56+
txt = Lident "BeltList";
57+
loc = e.pmod_loc;
58+
},
59+
[] );
60+
ptyp_loc = e.pmod_loc;
61+
ptyp_attributes = [];
62+
} );
63+
pexp_attributes = [];
64+
pexp_loc = e.pmod_loc;
65+
} );
66+
] );
67+
pexp_attributes =
68+
[
69+
({ txt = "res.await"; loc = Location.none }, PStr []);
70+
];
71+
pexp_loc = e.pmod_loc;
72+
} );
73+
] );
74+
pexp_loc = e.pmod_loc;
75+
pexp_attributes = [];
76+
};
77+
}

jscomp/frontend/bs_builtin_ppx.ml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type mapper = Bs_ast_mapper.mapper
6363
let default_mapper = Bs_ast_mapper.default_mapper
6464
let default_expr_mapper = Bs_ast_mapper.default_mapper.expr
6565
let default_pat_mapper = Bs_ast_mapper.default_mapper.pat
66+
let default_module_expr_mapper = Bs_ast_mapper.default_mapper.module_expr
6667

6768
let pat_mapper (self : mapper) (e : Parsetree.pattern) =
6869
match e.ppat_desc with
@@ -235,11 +236,17 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
235236
match Ast_attributes.has_await_payload e.pexp_attributes with
236237
| None -> result
237238
| Some _ ->
238-
if !async_context = false then
239-
Location.raise_errorf ~loc:e.pexp_loc
240-
"Await on expression not in an async context";
239+
(* if !async_context = false then
240+
Location.raise_errorf ~loc:e.pexp_loc
241+
"Await on expression not in an async context"; *)
241242
Ast_await.create_await_expression result
242243

244+
let module_expr_mapper (self : mapper) (e : Parsetree.module_expr) =
245+
let result = default_module_expr_mapper self e in
246+
match Ast_attributes.has_await_payload e.pmod_attributes with
247+
| None -> result
248+
| Some _ -> Ast_await.create_await_module_expression result
249+
243250
let typ_mapper (self : mapper) (typ : Parsetree.core_type) =
244251
Ast_core_type_class_type.typ_mapper self typ
245252

@@ -547,6 +554,7 @@ let mapper : mapper =
547554
{
548555
default_mapper with
549556
expr = expr_mapper ~async_context:(ref false) ~in_function_def:(ref false);
557+
module_expr = module_expr_mapper;
550558
pat = pat_mapper;
551559
typ = typ_mapper;
552560
class_type = class_type_mapper;

jscomp/test/Import.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ eachIntAsync({
4646

4747
var beltAsModule = import("../../lib/js/belt_List.js");
4848

49-
var M;
49+
var M = await import("../../lib/js/belt_List.js");
50+
51+
var each = M.forEach;
5052

5153
exports.eachIntAsync = eachIntAsync;
5254
exports.eachIntLazy = eachIntLazy;
5355
exports.beltAsModule = beltAsModule;
5456
exports.M = M;
57+
exports.each = each;
5558
/* Not a pure module */

jscomp/test/Import.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ let _ = list{1, 2, 3}->eachIntAsync(n => Js.log2("async", n))
1111
module type BeltList = module type of Belt.List
1212
let beltAsModule = Js.import(module(Belt.List: BeltList))
1313

14+
// module M = unpack(@res.await Js.import(module(Belt.List: BeltList)))
1415
module M = @res.await Belt.List
16+
let each = M.forEach

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264810,6 +264810,77 @@ let create_await_expression (e : Parsetree.expression) =
264810264810
let pexp_desc = Parsetree.Pexp_ident { txt; loc = e.pexp_loc } in
264811264811
{ e with pexp_desc = Pexp_apply ({ e with pexp_desc }, [ (Nolabel, e) ]) }
264812264812

264813+
let create_await_module_expression (e : Parsetree.module_expr) =
264814+
let txt = Longident.Ldot (Lident "Js", "import") in
264815+
let _module_lid =
264816+
match e with { pmod_desc = Pmod_ident lid } -> lid | _ -> assert false
264817+
in
264818+
let module_expr_without_attr = { e with pmod_attributes = [] } in
264819+
let import_pexp_desc = Parsetree.Pexp_ident { txt; loc = e.pmod_loc } in
264820+
let txt =
264821+
Longident.Ldot (Longident.Ldot (Lident "Js", "Promise"), "unsafe_await")
264822+
in
264823+
let await_pexp_desc = Parsetree.Pexp_ident { txt; loc = e.pmod_loc } in
264824+
{
264825+
module_expr_without_attr with
264826+
pmod_desc =
264827+
Pmod_unpack
264828+
{
264829+
pexp_desc =
264830+
Pexp_apply
264831+
( {
264832+
pexp_desc = await_pexp_desc;
264833+
pexp_attributes = [];
264834+
pexp_loc = e.pmod_loc;
264835+
},
264836+
[
264837+
( Nolabel,
264838+
{
264839+
pexp_desc =
264840+
Pexp_apply
264841+
( {
264842+
pexp_desc = import_pexp_desc;
264843+
pexp_attributes = [];
264844+
pexp_loc = e.pmod_loc;
264845+
},
264846+
[
264847+
( Nolabel,
264848+
{
264849+
pexp_desc =
264850+
Pexp_constraint
264851+
( {
264852+
pexp_desc =
264853+
Pexp_pack module_expr_without_attr;
264854+
pexp_loc = e.pmod_loc;
264855+
pexp_attributes = [];
264856+
},
264857+
{
264858+
ptyp_desc =
264859+
Ptyp_package
264860+
( {
264861+
txt = Lident "BeltList";
264862+
loc = e.pmod_loc;
264863+
},
264864+
[] );
264865+
ptyp_loc = e.pmod_loc;
264866+
ptyp_attributes = [];
264867+
} );
264868+
pexp_attributes = [];
264869+
pexp_loc = e.pmod_loc;
264870+
} );
264871+
] );
264872+
pexp_attributes =
264873+
[
264874+
({ txt = "res.await"; loc = Location.none }, PStr []);
264875+
];
264876+
pexp_loc = e.pmod_loc;
264877+
} );
264878+
] );
264879+
pexp_loc = e.pmod_loc;
264880+
pexp_attributes = [];
264881+
};
264882+
}
264883+
264813264884
end
264814264885
module Bs_ast_mapper : sig
264815264886
#1 "bs_ast_mapper.mli"
@@ -271291,6 +271362,7 @@ type mapper = Bs_ast_mapper.mapper
271291271362
let default_mapper = Bs_ast_mapper.default_mapper
271292271363
let default_expr_mapper = Bs_ast_mapper.default_mapper.expr
271293271364
let default_pat_mapper = Bs_ast_mapper.default_mapper.pat
271365+
let default_module_expr_mapper = Bs_ast_mapper.default_mapper.module_expr
271294271366

271295271367
let pat_mapper (self : mapper) (e : Parsetree.pattern) =
271296271368
match e.ppat_desc with
@@ -271463,11 +271535,17 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
271463271535
match Ast_attributes.has_await_payload e.pexp_attributes with
271464271536
| None -> result
271465271537
| Some _ ->
271466-
if !async_context = false then
271467-
Location.raise_errorf ~loc:e.pexp_loc
271468-
"Await on expression not in an async context";
271538+
(* if !async_context = false then
271539+
Location.raise_errorf ~loc:e.pexp_loc
271540+
"Await on expression not in an async context"; *)
271469271541
Ast_await.create_await_expression result
271470271542

271543+
let module_expr_mapper (self : mapper) (e : Parsetree.module_expr) =
271544+
let result = default_module_expr_mapper self e in
271545+
match Ast_attributes.has_await_payload e.pmod_attributes with
271546+
| None -> result
271547+
| Some _ -> Ast_await.create_await_module_expression result
271548+
271471271549
let typ_mapper (self : mapper) (typ : Parsetree.core_type) =
271472271550
Ast_core_type_class_type.typ_mapper self typ
271473271551

@@ -271775,6 +271853,7 @@ let mapper : mapper =
271775271853
{
271776271854
default_mapper with
271777271855
expr = expr_mapper ~async_context:(ref false) ~in_function_def:(ref false);
271856+
module_expr = module_expr_mapper;
271778271857
pat = pat_mapper;
271779271858
typ = typ_mapper;
271780271859
class_type = class_type_mapper;

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266273,6 +266273,77 @@ let create_await_expression (e : Parsetree.expression) =
266273266273
let pexp_desc = Parsetree.Pexp_ident { txt; loc = e.pexp_loc } in
266274266274
{ e with pexp_desc = Pexp_apply ({ e with pexp_desc }, [ (Nolabel, e) ]) }
266275266275

266276+
let create_await_module_expression (e : Parsetree.module_expr) =
266277+
let txt = Longident.Ldot (Lident "Js", "import") in
266278+
let _module_lid =
266279+
match e with { pmod_desc = Pmod_ident lid } -> lid | _ -> assert false
266280+
in
266281+
let module_expr_without_attr = { e with pmod_attributes = [] } in
266282+
let import_pexp_desc = Parsetree.Pexp_ident { txt; loc = e.pmod_loc } in
266283+
let txt =
266284+
Longident.Ldot (Longident.Ldot (Lident "Js", "Promise"), "unsafe_await")
266285+
in
266286+
let await_pexp_desc = Parsetree.Pexp_ident { txt; loc = e.pmod_loc } in
266287+
{
266288+
module_expr_without_attr with
266289+
pmod_desc =
266290+
Pmod_unpack
266291+
{
266292+
pexp_desc =
266293+
Pexp_apply
266294+
( {
266295+
pexp_desc = await_pexp_desc;
266296+
pexp_attributes = [];
266297+
pexp_loc = e.pmod_loc;
266298+
},
266299+
[
266300+
( Nolabel,
266301+
{
266302+
pexp_desc =
266303+
Pexp_apply
266304+
( {
266305+
pexp_desc = import_pexp_desc;
266306+
pexp_attributes = [];
266307+
pexp_loc = e.pmod_loc;
266308+
},
266309+
[
266310+
( Nolabel,
266311+
{
266312+
pexp_desc =
266313+
Pexp_constraint
266314+
( {
266315+
pexp_desc =
266316+
Pexp_pack module_expr_without_attr;
266317+
pexp_loc = e.pmod_loc;
266318+
pexp_attributes = [];
266319+
},
266320+
{
266321+
ptyp_desc =
266322+
Ptyp_package
266323+
( {
266324+
txt = Lident "BeltList";
266325+
loc = e.pmod_loc;
266326+
},
266327+
[] );
266328+
ptyp_loc = e.pmod_loc;
266329+
ptyp_attributes = [];
266330+
} );
266331+
pexp_attributes = [];
266332+
pexp_loc = e.pmod_loc;
266333+
} );
266334+
] );
266335+
pexp_attributes =
266336+
[
266337+
({ txt = "res.await"; loc = Location.none }, PStr []);
266338+
];
266339+
pexp_loc = e.pmod_loc;
266340+
} );
266341+
] );
266342+
pexp_loc = e.pmod_loc;
266343+
pexp_attributes = [];
266344+
};
266345+
}
266346+
266276266347
end
266277266348
module Bs_ast_mapper : sig
266278266349
#1 "bs_ast_mapper.mli"
@@ -272754,6 +272825,7 @@ type mapper = Bs_ast_mapper.mapper
272754272825
let default_mapper = Bs_ast_mapper.default_mapper
272755272826
let default_expr_mapper = Bs_ast_mapper.default_mapper.expr
272756272827
let default_pat_mapper = Bs_ast_mapper.default_mapper.pat
272828+
let default_module_expr_mapper = Bs_ast_mapper.default_mapper.module_expr
272757272829

272758272830
let pat_mapper (self : mapper) (e : Parsetree.pattern) =
272759272831
match e.ppat_desc with
@@ -272926,11 +272998,17 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
272926272998
match Ast_attributes.has_await_payload e.pexp_attributes with
272927272999
| None -> result
272928273000
| Some _ ->
272929-
if !async_context = false then
272930-
Location.raise_errorf ~loc:e.pexp_loc
272931-
"Await on expression not in an async context";
273001+
(* if !async_context = false then
273002+
Location.raise_errorf ~loc:e.pexp_loc
273003+
"Await on expression not in an async context"; *)
272932273004
Ast_await.create_await_expression result
272933273005

273006+
let module_expr_mapper (self : mapper) (e : Parsetree.module_expr) =
273007+
let result = default_module_expr_mapper self e in
273008+
match Ast_attributes.has_await_payload e.pmod_attributes with
273009+
| None -> result
273010+
| Some _ -> Ast_await.create_await_module_expression result
273011+
272934273012
let typ_mapper (self : mapper) (typ : Parsetree.core_type) =
272935273013
Ast_core_type_class_type.typ_mapper self typ
272936273014

@@ -273238,6 +273316,7 @@ let mapper : mapper =
273238273316
{
273239273317
default_mapper with
273240273318
expr = expr_mapper ~async_context:(ref false) ~in_function_def:(ref false);
273319+
module_expr = module_expr_mapper;
273241273320
pat = pat_mapper;
273242273321
typ = typ_mapper;
273243273322
class_type = class_type_mapper;

0 commit comments

Comments
 (0)