Skip to content

Commit 9a779e9

Browse files
authored
Fix making the static import for the dynamic import of external ffi (#6664)
* add test * fix dynamic import of external * add test for adding static import * add change log
1 parent 1f2d463 commit 9a779e9

16 files changed

+69
-28
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
# 11.1.0-rc.4 (Unreleased)
1414

15+
#### :bug: Bug Fix
16+
17+
- Fix making the static import for the dynamic import of external ffi https://github.com/rescript-lang/rescript-compiler/pull/6664
18+
1519
#### :nail-care: Polish
1620
- Omit `undefined` in external function calls for trailing optional arguments when not supplied. https://github.com/rescript-lang/rescript-compiler/pull/6653
1721

jscomp/core/lam.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,13 +788,13 @@ let rec transform_uncurried_arg_type loc (arg_types : External_arg_spec.params)
788788
| ([], [] | _ :: _, [] | [], _ :: _) as ok -> ok
789789

790790
let handle_bs_non_obj_ffi (arg_types : External_arg_spec.params)
791-
(result_type : External_ffi_types.return_wrapper) ffi args loc prim_name =
791+
(result_type : External_ffi_types.return_wrapper) ffi args loc prim_name ~dynamic_import =
792792
if no_auto_uncurried_arg_types arg_types then
793793
result_wrap loc result_type
794-
(prim ~primitive:(Pjs_call { prim_name; arg_types; ffi }) ~args loc)
794+
(prim ~primitive:(Pjs_call { prim_name; arg_types; ffi; dynamic_import }) ~args loc)
795795
else
796796
let n_arg_types, n_args = transform_uncurried_arg_type loc arg_types args in
797797
result_wrap loc result_type
798798
(prim
799-
~primitive:(Pjs_call { prim_name; arg_types = n_arg_types; ffi })
799+
~primitive:(Pjs_call { prim_name; arg_types = n_arg_types; ffi; dynamic_import })
800800
~args:n_args loc)

jscomp/core/lam.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ val handle_bs_non_obj_ffi :
9191
t list ->
9292
Location.t ->
9393
string ->
94+
dynamic_import: bool ->
9495
t
9596

9697
(**************************************************************)

jscomp/core/lam_compile_env.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ let reset () =
6060
be escaped quite ugly
6161
*)
6262
let add_js_module ?import_attributes (hint_name : External_ffi_types.module_bind_name)
63-
(module_name : string) default : Ident.t =
63+
(module_name : string) default ~dynamic_import : Ident.t =
6464
let id =
6565
Ident.create
6666
(match hint_name with
@@ -71,7 +71,7 @@ let add_js_module ?import_attributes (hint_name : External_ffi_types.module_bind
7171
| Phint_nothing -> Ext_modulename.js_id_name_of_hint_name module_name)
7272
in
7373
let lam_module_ident : J.module_id =
74-
{ id; kind = External { name = module_name; default; import_attributes }; dynamic_import = false }
74+
{ id; kind = External { name = module_name; default; import_attributes }; dynamic_import }
7575
in
7676
match Lam_module_ident.Hash.find_key_opt cached_tbl lam_module_ident with
7777
| None ->

jscomp/core/lam_compile_env.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
val reset : unit -> unit
2828

2929
val add_js_module :
30-
?import_attributes:External_ffi_types.import_attributes -> External_ffi_types.module_bind_name -> string -> bool -> Ident.t
30+
?import_attributes:External_ffi_types.import_attributes -> External_ffi_types.module_bind_name -> string -> bool -> dynamic_import:bool -> Ident.t
3131
(**
3232
[add_js_module hint_name module_name]
3333
Given a js module name and hint name, assign an id to it

jscomp/core/lam_compile_external_call.ml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ let splice_obj_apply obj name args =
5050
bundle *)
5151

5252
let external_var
53-
({ bundle; module_bind_name; import_attributes } : External_ffi_types.external_module_name) =
54-
let id = Lam_compile_env.add_js_module ?import_attributes module_bind_name bundle false in
53+
({ bundle; module_bind_name; import_attributes } : External_ffi_types.external_module_name) ~dynamic_import =
54+
let id = Lam_compile_env.add_js_module ?import_attributes module_bind_name bundle false ~dynamic_import in
5555
E.external_var ?import_attributes ~external_name:bundle id
5656

5757
(* let handle_external_opt
@@ -216,21 +216,22 @@ let assemble_args_has_splice (arg_types : specs) (args : exprs) :
216216

217217
let translate_scoped_module_val
218218
(module_name : External_ffi_types.external_module_name option) (fn : string)
219-
(scopes : string list) =
219+
(scopes : string list)
220+
~dynamic_import =
220221
match module_name with
221222
| Some { bundle; module_bind_name; import_attributes } -> (
222223
match scopes with
223224
| [] ->
224225
let default = fn = "default" in
225226
let id =
226-
Lam_compile_env.add_js_module ?import_attributes module_bind_name bundle default
227+
Lam_compile_env.add_js_module ?import_attributes module_bind_name bundle default ~dynamic_import
227228
in
228229
E.external_var_field ?import_attributes ~external_name:bundle ~field:fn ~default id
229230
| x :: rest ->
230231
(* TODO: what happens when scope contains "default" ?*)
231232
let default = false in
232233
let id =
233-
Lam_compile_env.add_js_module ?import_attributes module_bind_name bundle default
234+
Lam_compile_env.add_js_module ?import_attributes module_bind_name bundle default ~dynamic_import
234235
in
235236
let start =
236237
E.external_var_field ?import_attributes ~external_name:bundle ~field:x ~default id
@@ -250,10 +251,10 @@ let translate_scoped_access scopes obj =
250251
| x :: xs -> Ext_list.fold_left xs (E.dot obj x) E.dot
251252

252253
let translate_ffi (cxt : Lam_compile_context.t) arg_types
253-
(ffi : External_ffi_types.external_spec) (args : J.expression list) =
254+
(ffi : External_ffi_types.external_spec) (args : J.expression list) ~dynamic_import =
254255
match ffi with
255256
| Js_call { external_module_name; name; splice: _; scopes; tagged_template = true } ->
256-
let fn = translate_scoped_module_val external_module_name name scopes in
257+
let fn = translate_scoped_module_val external_module_name name scopes ~dynamic_import in
257258
(match args with
258259
| [ {expression_desc = Array (strings, _); _}; {expression_desc = Array (values, _); _} ] ->
259260
E.tagged_template fn strings values
@@ -262,7 +263,7 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
262263
(if dynamic then splice_apply fn args
263264
else E.call ~info:{ arity = Full; call_info = Call_na } fn args))
264265
| Js_call { external_module_name = module_name; name = fn; splice; scopes; tagged_template = false } ->
265-
let fn = translate_scoped_module_val module_name fn scopes in
266+
let fn = translate_scoped_module_val module_name fn scopes ~dynamic_import in
266267
if splice then
267268
let args, eff, dynamic = assemble_args_has_splice arg_types args in
268269
add_eff eff
@@ -283,7 +284,7 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
283284
add_eff eff
284285
@@ E.call ~info:{ arity = Full; call_info = Call_na } fn args
285286
| Js_module_as_fn { external_module_name; splice } ->
286-
let fn = external_var external_module_name in
287+
let fn = external_var external_module_name ~dynamic_import in
287288
if splice then
288289
let args, eff, dynamic = assemble_args_has_splice arg_types args in
289290
(* TODO: fix in rest calling convention *)
@@ -313,14 +314,14 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
313314
in
314315
if splice then
315316
let args, eff, dynamic = assemble_args_has_splice arg_types args in
316-
let fn = translate_scoped_module_val module_name fn scopes in
317+
let fn = translate_scoped_module_val module_name fn scopes ~dynamic_import in
317318
add_eff eff
318319
(mark ();
319320
if dynamic then splice_new_apply fn args
320321
else E.new_ fn args)
321322
else
322323
let args, eff = assemble_args_no_splice arg_types args in
323-
let fn = translate_scoped_module_val module_name fn scopes in
324+
let fn = translate_scoped_module_val module_name fn scopes ~dynamic_import in
324325
add_eff eff
325326
(mark (); E.new_ fn args)
326327
| Js_send { splice; name; js_send_scopes } -> (
@@ -346,16 +347,16 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
346347
~info:{ arity = Full; call_info = Call_na }
347348
(E.dot self name) args)
348349
| _ -> assert false)
349-
| Js_module_as_var module_name -> external_var module_name
350+
| Js_module_as_var module_name -> external_var module_name ~dynamic_import
350351
| Js_var { name; external_module_name; scopes } ->
351352
(* TODO #11
352353
1. check args -- error checking
353354
2. support [@@scope "window"]
354355
we need know whether we should call [add_js_module] or not
355356
*)
356-
translate_scoped_module_val external_module_name name scopes
357+
translate_scoped_module_val external_module_name name scopes ~dynamic_import
357358
| Js_module_as_class module_name ->
358-
let fn = external_var module_name in
359+
let fn = external_var module_name ~dynamic_import in
359360
let args, eff = assemble_args_no_splice arg_types args in
360361
(* TODO: fix in rest calling convention *)
361362
add_eff eff

jscomp/core/lam_compile_external_call.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ val translate_ffi :
3434
External_arg_spec.params ->
3535
External_ffi_types.external_spec ->
3636
J.expression list ->
37+
dynamic_import:bool ->
3738
J.expression
3839

3940
(** TODO: document supported attributes

jscomp/core/lam_compile_primitive.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
337337
(* Lam_compile_external_call.translate loc cxt prim args *)
338338
(* Test if the argument is a block or an immediate integer *)
339339
| Pjs_object_create _ -> assert false
340-
| Pjs_call { arg_types; ffi } ->
341-
Lam_compile_external_call.translate_ffi cxt arg_types ffi args
340+
| Pjs_call { arg_types; ffi; dynamic_import } ->
341+
Lam_compile_external_call.translate_ffi cxt arg_types ffi args ~dynamic_import
342342
(* FIXME, this can be removed later *)
343343
| Pisint -> E.is_type_number (Ext_list.singleton_exn args)
344344
| Pis_poly_var_block -> E.is_type_object (Ext_list.singleton_exn args)

jscomp/core/lam_convert.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
380380
let may_depends = Lam_module_ident.Hash_set.create 0 in
381381

382382
let rec convert_ccall (a_prim : Primitive.description)
383-
(args : Lambda.lambda list) loc : Lam.t =
383+
(args : Lambda.lambda list) loc ~dynamic_import : Lam.t =
384384
let prim_name = a_prim.prim_name in
385385
let prim_name_len = String.length prim_name in
386386
match External_ffi_types.from_string a_prim.prim_native_name with
@@ -400,7 +400,7 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
400400
| Param_number i -> Ext_list.init i (fun _ -> External_arg_spec.dummy)
401401
in
402402
let args = Ext_list.map args convert_aux in
403-
Lam.handle_bs_non_obj_ffi arg_types result_type ffi args loc prim_name
403+
Lam.handle_bs_non_obj_ffi arg_types result_type ffi args loc prim_name ~dynamic_import
404404
| Ffi_inline_const i -> Lam.const i
405405
and convert_js_primitive (p : Primitive.description)
406406
(args : Lambda.lambda list) loc : Lam.t =
@@ -542,7 +542,7 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
542542
convert_pipe f x outer_loc
543543
| Lprim (Prevapply, _, _) -> assert false
544544
| Lprim (Pdirapply, _, _) -> assert false
545-
| Lprim (Pccall a, args, loc) -> convert_ccall a args loc
545+
| Lprim (Pccall a, args, loc) -> convert_ccall a args loc ~dynamic_import
546546
| Lprim (Pgetglobal id, args, _) ->
547547
let args = Ext_list.map args convert_aux in
548548
if Ident.is_predef_exn id then

jscomp/core/lam_primitive.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type t =
4848
prim_name : string;
4949
arg_types : External_arg_spec.params;
5050
ffi : External_ffi_types.external_spec;
51+
dynamic_import: bool;
5152
}
5253
| Pjs_object_create of External_arg_spec.obj_params
5354
(* Exceptions *)
@@ -249,11 +250,11 @@ let eq_primitive_approx (lhs : t) (rhs : t) =
249250
i0 = i1 && flag0 = flag1 && eq_tag_info info0 info1
250251
| _ -> false)
251252
| Pduprecord -> rhs = Pduprecord
252-
| Pjs_call { prim_name; arg_types; ffi } -> (
253+
| Pjs_call { prim_name; arg_types; ffi; dynamic_import } -> (
253254
match rhs with
254255
| Pjs_call rhs ->
255256
prim_name = rhs.prim_name && arg_types = rhs.arg_types
256-
&& ffi = rhs.ffi
257+
&& ffi = rhs.ffi && dynamic_import = rhs.dynamic_import
257258
| _ -> false)
258259
| Pjs_object_create obj_create -> (
259260
match rhs with

jscomp/core/lam_primitive.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type t =
4343
prim_name : string;
4444
arg_types : External_arg_spec.params;
4545
ffi : External_ffi_types.external_spec;
46+
dynamic_import: bool;
4647
}
4748
| Pjs_object_create of External_arg_spec.obj_params
4849
| Praise

jscomp/test/build.ninja

Lines changed: 3 additions & 1 deletion
Large diffs are not rendered by default.

jscomp/test/import2.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/import2.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let a = Js.import(Import_external.makeA)
2+
let b = Import_external.makeA

jscomp/test/import_external.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/import_external.res

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@module("a")
2+
external makeA: string = "default"
3+
4+
let f8 = Js.import(makeA)

0 commit comments

Comments
 (0)