Skip to content

Commit 949af10

Browse files
committed
use @taggedTemplate annotation in ffi instead
1 parent d7f48c7 commit 949af10

24 files changed

+55
-112
lines changed

jscomp/core/lam.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ type apply_status = App_na | App_infer_full | App_uncurry
2828
type ap_info = {
2929
ap_loc : Location.t;
3030
ap_inlined : Lambda.inline_attribute;
31-
ap_tagged_template : bool;
3231
ap_status : apply_status;
3332
}
3433

jscomp/core/lam.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ type apply_status = App_na | App_infer_full | App_uncurry
2727
type ap_info = {
2828
ap_loc : Location.t;
2929
ap_inlined : Lambda.inline_attribute;
30-
ap_tagged_template : bool;
3130
ap_status : apply_status;
3231
}
3332

jscomp/core/lam_analysis.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
9898
true
9999
| Pjs_apply | Pjs_runtime_apply | Pjs_call _ | Pinit_mod | Pupdate_mod
100100
| Pjs_unsafe_downgrade _ | Pdebugger | Pvoid_run | Pfull_apply
101-
| Pjs_fn_method | Pjs_tagged_template _
101+
| Pjs_fn_method
102102
(* TODO *)
103103
| Praw_js_code _ | Pbytessetu | Pbytessets
104104
(* Operations on boxed integers (Nativeint.t, Int32.t, Int64.t) *)

jscomp/core/lam_compile.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,6 @@ and compile_prim (prim_info : Lam.prim_info)
15991599
{
16001600
ap_loc = loc;
16011601
ap_inlined = Default_inline;
1602-
ap_tagged_template = false;
16031602
ap_status = App_uncurry;
16041603
})
16051604
(*FIXME: should pass info down: `f a [@bs][@inlined]`*)

jscomp/core/lam_compile_external_call.ml

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,17 @@ let translate_scoped_access scopes obj =
252252
let translate_ffi (cxt : Lam_compile_context.t) arg_types
253253
(ffi : External_ffi_types.external_spec) (args : J.expression list) =
254254
match ffi with
255-
| Js_call { external_module_name = module_name; name = fn; splice; scopes } ->
255+
| 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+
(match args with
258+
| [ stringArgs; valueArgs ] -> (
259+
match (stringArgs, valueArgs) with
260+
| ({expression_desc = Array (strings, _); _}, {expression_desc = Array (values, _); _}) ->
261+
E.tagged_template fn strings values
262+
| _ -> assert false
263+
)
264+
| _ -> assert false)
265+
| Js_call { external_module_name = module_name; name = fn; splice; scopes; tagged_template = false } ->
256266
let fn = translate_scoped_module_val module_name fn scopes in
257267
if splice then
258268
let args, eff, dynamic = assemble_args_has_splice arg_types args in
@@ -381,19 +391,3 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
381391
| [ obj; v; value ] ->
382392
Js_arr.set_array (translate_scoped_access scopes obj) v value
383393
| _ -> assert false)
384-
385-
let translate_tagged_template (cxt : Lam_compile_context.t)
386-
(ffi : External_ffi_types.external_spec) (args : J.expression list) =
387-
let fn = match ffi with
388-
| Js_call { external_module_name; name; scopes; _ } ->
389-
translate_scoped_module_val external_module_name name scopes
390-
| _ -> assert false
391-
in
392-
match args with
393-
| [ stringArgs; valueArgs ] -> (
394-
match (stringArgs, valueArgs) with
395-
| ({expression_desc = Array (strings, _); _}, {expression_desc = Array (values, _); _}) ->
396-
E.tagged_template fn strings values
397-
| _ -> assert false
398-
)
399-
| _ -> assert false

jscomp/core/lam_compile_external_call.mli

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ val translate_ffi :
3636
J.expression list ->
3737
J.expression
3838

39-
val translate_tagged_template :
40-
Lam_compile_context.t ->
41-
External_ffi_types.external_spec ->
42-
J.expression list ->
43-
J.expression
44-
4539
(** TODO: document supported attributes
4640
Attributes starting with `js` are reserved
4741
examples: "variadic"

jscomp/core/lam_compile_primitive.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
339339
| Pjs_object_create _ -> assert false
340340
| Pjs_call { arg_types; ffi } ->
341341
Lam_compile_external_call.translate_ffi cxt arg_types ffi args
342-
| Pjs_tagged_template { ffi } -> Lam_compile_external_call.translate_tagged_template cxt ffi args
343342
(* FIXME, this can be removed later *)
344343
| Pisint -> E.is_type_number (Ext_list.singleton_exn args)
345344
| Pis_poly_var_block -> E.is_type_object (Ext_list.singleton_exn args)

jscomp/core/lam_convert.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,11 +512,11 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
512512
match lam with
513513
| Lvar x -> Lam.var (Hash_ident.find_default alias_tbl x x)
514514
| Lconst x -> Lam.const (Lam_constant_convert.convert_constant x)
515-
| Lapply { ap_func = fn; ap_args = args; ap_loc = loc; ap_inlined; ap_tagged_template } ->
515+
| Lapply { ap_func = fn; ap_args = args; ap_loc = loc; ap_inlined } ->
516516
(* we need do this eargly in case [aux fn] add some wrapper *)
517517
Lam.apply (convert_aux fn)
518518
(Ext_list.map args convert_aux)
519-
{ ap_loc = loc; ap_inlined; ap_tagged_template; ap_status = App_na }
519+
{ ap_loc = loc; ap_inlined; ap_status = App_na }
520520
| Lfunction { params; body; attr } ->
521521
let new_map, body =
522522
rename_optional_parameters Map_ident.empty params body
@@ -685,15 +685,13 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
685685
{
686686
ap_loc = outer_loc;
687687
ap_inlined = ap_info.ap_inlined;
688-
ap_tagged_template = ap_info.ap_tagged_template;
689688
ap_status = App_na;
690689
}
691690
| _ ->
692691
Lam.apply f [ x ]
693692
{
694693
ap_loc = outer_loc;
695694
ap_inlined = Default_inline;
696-
ap_tagged_template = false;
697695
ap_status = App_na;
698696
}
699697
and convert_switch (e : Lambda.lambda) (s : Lambda.lambda_switch) =

jscomp/core/lam_eta_conversion.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ let transform_under_supply n ap_info fn args =
111111
let unsafe_adjust_to_arity loc ~(to_ : int) ?(from : int option) (fn : Lam.t) :
112112
Lam.t =
113113
let ap_info : Lam.ap_info =
114-
{ ap_loc = loc; ap_inlined = Default_inline; ap_tagged_template = false; ap_status = App_na }
114+
{ ap_loc = loc; ap_inlined = Default_inline; ap_status = App_na }
115115
in
116116
let is_async_fn = match fn with
117117
| Lfunction { attr = {async}} -> async

jscomp/core/lam_pass_remove_alias.ml

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
198198

199199
(* Ext_log.dwarn __LOC__ "%s/%d" v.name v.stamp; *)
200200
let ap_args = Ext_list.map ap_args simpl in
201-
let normal () = Lam.apply (simpl fn) ap_args ap_info in
201+
let[@local] normal () = Lam.apply (simpl fn) ap_args ap_info in
202202
match Hash_ident.find_opt meta.ident_tbl v with
203203
| Some
204204
(FunctionId
@@ -238,7 +238,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
238238
Lam_closure.is_closed_with_map meta.export_idents params body
239239
in
240240
let is_export_id = Set_ident.mem meta.export_idents v in
241-
let result = match (is_export_id, param_map) with
241+
match (is_export_id, param_map) with
242242
| false, (_, param_map) | true, (true, param_map) -> (
243243
match rec_flag with
244244
| Lam_rec ->
@@ -256,18 +256,6 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
256256
(Lam_beta_reduce.propagate_beta_reduce_with_map meta
257257
param_map params body ap_args))
258258
| _ -> normal ()
259-
in
260-
let result = (match result with
261-
| Lprim {primitive; args; loc} -> (match primitive with
262-
(* Converts Pjs_calls to Pjs_tagged_templates if ap_tagged_template is true *)
263-
| Pjs_call {prim_name; ffi} when ap_info.ap_tagged_template ->
264-
let prim = Lam_primitive.Pjs_tagged_template {prim_name; ffi} in
265-
Lam.prim ~primitive:prim ~args loc
266-
| _ -> result
267-
)
268-
| _ -> result)
269-
in
270-
result
271259
else normal ()
272260
else normal ()
273261
| Some _ | None -> normal ())

jscomp/core/lam_primitive.ml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ type t =
4949
arg_types : External_arg_spec.params;
5050
ffi : External_ffi_types.external_spec;
5151
}
52-
| Pjs_tagged_template of {
53-
prim_name : string;
54-
ffi : External_ffi_types.external_spec;
55-
}
5652
| Pjs_object_create of External_arg_spec.obj_params
5753
(* Exceptions *)
5854
| Praise
@@ -263,11 +259,6 @@ let eq_primitive_approx (lhs : t) (rhs : t) =
263259
match rhs with
264260
| Pjs_object_create obj_create1 -> obj_create = obj_create1
265261
| _ -> false)
266-
| Pjs_tagged_template { prim_name; ffi } -> (
267-
match rhs with
268-
| Pjs_tagged_template rhs ->
269-
prim_name = rhs.prim_name && ffi = rhs.ffi
270-
| _ -> false)
271262
| Pintcomp comparison -> (
272263
match rhs with
273264
| Pintcomp comparison1 -> Lam_compat.eq_comparison comparison comparison1

jscomp/core/lam_primitive.mli

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ type t =
4444
arg_types : External_arg_spec.params;
4545
ffi : External_ffi_types.external_spec;
4646
}
47-
| Pjs_tagged_template of {
48-
prim_name : string;
49-
ffi : External_ffi_types.external_spec;
50-
}
5147
| Pjs_object_create of External_arg_spec.obj_params
5248
| Praise
5349
| Psequand

jscomp/core/lam_print.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ let primitive ppf (prim : Lam_primitive.t) =
9393
| Plazyforce -> fprintf ppf "force"
9494
| Pccall p -> fprintf ppf "%s" p.prim_name
9595
| Pjs_call { prim_name } -> fprintf ppf "%s[js]" prim_name
96-
| Pjs_tagged_template { prim_name } -> fprintf ppf "%s[js.tagged_template]" prim_name
9796
| Pjs_object_create _ -> fprintf ppf "[js.obj]"
9897
| Praise -> fprintf ppf "raise"
9998
| Psequand -> fprintf ppf "&&"
@@ -264,13 +263,12 @@ let lambda ppf v =
264263
| Lvar id -> Ident.print ppf id
265264
| Lglobal_module (id, dynamic_import) -> fprintf ppf (if dynamic_import then "dynamic global %a" else "global %a") Ident.print id
266265
| Lconst cst -> struct_const ppf cst
267-
| Lapply { ap_func; ap_args; ap_info = { ap_inlined; ap_tagged_template } } ->
266+
| Lapply { ap_func; ap_args; ap_info = { ap_inlined } } ->
268267
let lams ppf args =
269268
List.iter (fun l -> fprintf ppf "@ %a" lam l) args
270269
in
271-
fprintf ppf "@[<2>(apply%s%s@ %a%a)@]"
270+
fprintf ppf "@[<2>(apply%s@ %a%a)@]"
272271
(match ap_inlined with Always_inline -> "%inlned" | _ -> "")
273-
(match ap_tagged_template with true -> "%tagged_template" | _ -> "")
274272
lam ap_func lams ap_args
275273
| Lfunction { params; body; _ } ->
276274
let pr_params ppf params =

jscomp/frontend/ast_external_process.ml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ type external_desc = {
184184
get_name: bundle_source option;
185185
mk_obj: bool;
186186
return_wrapper: External_ffi_types.return_wrapper;
187+
tagged_template: bool;
187188
}
188189

189190
let init_st =
@@ -202,6 +203,7 @@ let init_st =
202203
get_name = None;
203204
mk_obj = false;
204205
return_wrapper = Return_unset;
206+
tagged_template = false;
205207
}
206208

207209
let return_wrapper loc (txt : string) : External_ffi_types.return_wrapper =
@@ -291,6 +293,7 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string)
291293
between unset/set
292294
*)
293295
| scopes -> {st with scopes})
296+
| "taggedTemplate" -> {st with splice = true; tagged_template = true}
294297
| "bs.splice" | "bs.variadic" | "variadic" -> {st with splice = true}
295298
| "bs.send" | "send" ->
296299
{st with val_send = Some (name_from_payload_or_prim ~loc payload)}
@@ -366,6 +369,7 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
366369
get_name = None;
367370
get_index = false;
368371
return_wrapper = Return_unset;
372+
tagged_template = _;
369373
set_index = false;
370374
mk_obj = _;
371375
scopes =
@@ -564,6 +568,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
564568
get_name = None;
565569
return_wrapper = _;
566570
mk_obj = _;
571+
tagged_template = _;
567572
} ->
568573
if arg_type_specs_length = 3 then
569574
Js_set_index {js_set_index_scopes = scopes}
@@ -588,6 +593,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
588593
set_index = false;
589594
mk_obj = _;
590595
return_wrapper = _;
596+
tagged_template = _;
591597
} ->
592598
if arg_type_specs_length = 2 then
593599
Js_get_index {js_get_index_scopes = scopes}
@@ -614,6 +620,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
614620
set_index = false;
615621
return_wrapper = _;
616622
mk_obj = _;
623+
tagged_template = _;
617624
} -> (
618625
match (arg_types_ty, new_name, val_name) with
619626
| [], None, _ -> Js_module_as_var external_module_name
@@ -655,6 +662,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
655662
mk_obj = _;
656663
(* mk_obj is always false *)
657664
return_wrapper = _;
665+
tagged_template;
658666
} ->
659667
let name = prim_name_or_pval_prim.name in
660668
if arg_type_specs_length = 0 then
@@ -665,7 +673,9 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
665673
FIXME: splice is not supported here
666674
*)
667675
Js_var {name; external_module_name = None; scopes}
668-
else Js_call {splice; name; external_module_name = None; scopes}
676+
else
677+
Js_call
678+
{splice; name; external_module_name = None; scopes; tagged_template}
669679
| {
670680
call_name = Some {name; source = _};
671681
splice;
@@ -681,6 +691,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
681691
get_name = None;
682692
mk_obj = _;
683693
return_wrapper = _;
694+
tagged_template;
684695
} ->
685696
if arg_type_specs_length = 0 then
686697
(*
@@ -690,7 +701,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
690701
*)
691702
Js_var {name; external_module_name; scopes}
692703
(*FIXME: splice is not supported here *)
693-
else Js_call {splice; name; external_module_name; scopes}
704+
else Js_call {splice; name; external_module_name; scopes; tagged_template}
694705
| {call_name = Some _; _} ->
695706
Bs_syntaxerr.err loc
696707
(Conflict_ffi_attribute "Attribute found that conflicts with %@val")
@@ -709,6 +720,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
709720
return_wrapper = _;
710721
splice = false;
711722
scopes;
723+
tagged_template = _;
712724
} ->
713725
(*
714726
if no_arguments -->
@@ -735,6 +747,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
735747
get_name = None;
736748
mk_obj = _;
737749
return_wrapper = _;
750+
tagged_template;
738751
} ->
739752
let name = prim_name_or_pval_prim.name in
740753
if arg_type_specs_length = 0 then
@@ -744,7 +757,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
744757
]}
745758
*)
746759
Js_var {name; external_module_name; scopes}
747-
else Js_call {splice; name; external_module_name; scopes}
760+
else Js_call {splice; name; external_module_name; scopes; tagged_template}
748761
| {
749762
val_send = Some {name; source = _};
750763
splice;
@@ -760,6 +773,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
760773
external_module_name = None;
761774
mk_obj = _;
762775
return_wrapper = _;
776+
tagged_template = _;
763777
} -> (
764778
(* PR #2162 - since when we assemble arguments the first argument in
765779
[@@send] is ignored
@@ -791,6 +805,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
791805
scopes;
792806
mk_obj = _;
793807
return_wrapper = _;
808+
tagged_template = _;
794809
} ->
795810
Js_new {name; external_module_name; splice; scopes}
796811
| {new_name = Some _; _} ->
@@ -811,6 +826,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
811826
mk_obj = _;
812827
return_wrapper = _;
813828
scopes;
829+
tagged_template = _;
814830
} ->
815831
if arg_type_specs_length = 2 then
816832
Js_set {js_set_scopes = scopes; js_set_name = name}
@@ -834,6 +850,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
834850
mk_obj = _;
835851
return_wrapper = _;
836852
scopes;
853+
tagged_template = _;
837854
} ->
838855
if arg_type_specs_length = 1 then
839856
Js_get {js_get_name = name; js_get_scopes = scopes}

jscomp/frontend/external_ffi_types.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type external_spec =
5353
external_module_name: external_module_name option;
5454
splice: bool;
5555
scopes: string list;
56+
tagged_template: bool;
5657
}
5758
| Js_send of {name: string; splice: bool; js_send_scopes: string list}
5859
(* we know it is a js send, but what will happen if you pass an ocaml objct *)
@@ -188,7 +189,9 @@ let check_ffi ?loc ffi : bool =
188189
upgrade (is_package_relative_path external_module_name.bundle);
189190
check_external_module_name external_module_name
190191
| Js_new {external_module_name; name; splice = _; scopes = _}
191-
| Js_call {external_module_name; name; splice = _; scopes = _} ->
192+
| Js_call
193+
{external_module_name; name; splice = _; scopes = _; tagged_template = _}
194+
->
192195
Ext_option.iter external_module_name (fun external_module_name ->
193196
upgrade (is_package_relative_path external_module_name.bundle));
194197
Ext_option.iter external_module_name (fun name ->

0 commit comments

Comments
 (0)