Skip to content

Commit 003887d

Browse files
committed
handle external JS ffi in the early pipe line. prepare for auto-uncurrying support
1 parent 989a1f2 commit 003887d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3931
-3415
lines changed

Diff for: jscomp/all.depend

+13-11
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ core/ocaml_options.cmi :
238238
core/ocaml_parse.cmi :
239239
core/lam_module_ident.cmi : core/js_op.cmx common/js_config.cmi core/j.cmx \
240240
ext/hashtbl_gen.cmx ext/hash_set_gen.cmx
241-
core/lam.cmi : core/lam_module_ident.cmi ext/ident_set.cmi
241+
core/lam.cmi : core/lam_module_ident.cmi ext/ident_set.cmi \
242+
syntax/ast_ffi_types.cmi
242243
core/lam_print.cmi : core/lam.cmi
243244
core/lam_beta_reduce_util.cmi : core/lam.cmi
244245
core/lam_inline_util.cmi : core/lam.cmi
@@ -305,7 +306,8 @@ core/lam_compile_global.cmi : core/lam_compile_env.cmi core/lam.cmi \
305306
core/lam_dispatch_primitive.cmi : core/j.cmx
306307
core/lam_beta_reduce.cmi : core/lam_stats.cmi core/lam_closure.cmi \
307308
core/lam.cmi ext/ident_map.cmi
308-
core/lam_compile_external_call.cmi : core/lam_compile_defs.cmi core/j.cmx
309+
core/lam_compile_external_call.cmi : core/lam_compile_defs.cmi core/j.cmx \
310+
syntax/ast_ffi_types.cmi
309311
core/lam_compile_primitive.cmi : core/lam_compile_defs.cmi core/lam.cmi \
310312
core/j.cmx
311313
core/lam_compile.cmi : core/lam_compile_defs.cmi core/lam.cmi \
@@ -338,7 +340,7 @@ core/lam.cmx : ext/ordered_hash_map_local_ident.cmx \
338340
common/js_config.cmx ext/int_vec_vec.cmx ext/int_vec_util.cmx \
339341
ext/int_vec.cmx ext/ident_set.cmx ext/ident_hashtbl.cmx \
340342
ext/ident_hash_set.cmx ext/hash_set_ident_mask.cmx ext/ext_string.cmx \
341-
ext/ext_scc.cmx core/lam.cmi
343+
ext/ext_scc.cmx syntax/ast_ffi_types.cmx core/lam.cmi
342344
core/lam_print.cmx : core/lam.cmx core/lam_print.cmi
343345
core/lam_beta_reduce_util.cmx : core/lam.cmx ext/ident_hashtbl.cmx \
344346
core/lam_beta_reduce_util.cmi
@@ -489,15 +491,15 @@ core/lam_beta_reduce.cmx : core/lam_util.cmx core/lam_stats.cmx \
489491
core/lam_compile_global.cmx core/lam_closure.cmx \
490492
core/lam_beta_reduce_util.cmx core/lam_analysis.cmx core/lam.cmx \
491493
ext/ident_map.cmx ext/ident_hashtbl.cmx core/lam_beta_reduce.cmi
492-
core/lam_compile_external_call.cmx : core/lam_dispatch_primitive.cmx \
493-
core/lam_compile_env.cmx core/lam_compile_defs.cmx core/js_op.cmx \
494-
core/js_of_lam_variant.cmx core/js_of_lam_option.cmx core/js_exp_make.cmx \
495-
core/js_arr.cmx core/js_analyzer.cmx core/j.cmx ext/ext_list.cmx \
496-
ext/ext_ident.cmx syntax/ast_ffi_types.cmx \
497-
core/lam_compile_external_call.cmi
494+
core/lam_compile_external_call.cmx : core/lam_compile_env.cmx \
495+
core/lam_compile_defs.cmx core/js_op.cmx core/js_of_lam_variant.cmx \
496+
core/js_of_lam_option.cmx core/js_exp_make.cmx core/js_arr.cmx \
497+
core/js_analyzer.cmx core/j.cmx ext/ext_list.cmx ext/ext_ident.cmx \
498+
syntax/ast_ffi_types.cmx core/lam_compile_external_call.cmi
498499
core/lam_compile_primitive.cmx : core/lam_util.cmx \
499-
core/lam_compile_external_call.cmx core/lam_compile_defs.cmx core/lam.cmx \
500-
core/js_op_util.cmx core/js_of_lam_string.cmx core/js_of_lam_record.cmx \
500+
core/lam_dispatch_primitive.cmx core/lam_compile_external_call.cmx \
501+
core/lam_compile_defs.cmx core/lam.cmx core/js_op_util.cmx \
502+
core/js_of_lam_string.cmx core/js_of_lam_record.cmx \
501503
core/js_of_lam_float_record.cmx core/js_of_lam_exception.cmx \
502504
core/js_of_lam_block.cmx core/js_of_lam_array.cmx core/js_long.cmx \
503505
core/js_exp_make.cmx common/js_config.cmx core/j.cmx common/ext_log.cmx \

Diff for: jscomp/bin/bsdep.ml

+95-18
Original file line numberDiff line numberDiff line change
@@ -23701,6 +23701,8 @@ let is_single_int (x : t ) =
2370123701
_}] -> Some name
2370223702
| _ -> None
2370323703

23704+
23705+
2370423706
let as_string_exp (x : t ) =
2370523707
match x with (** TODO also need detect empty phrase case *)
2370623708
| PStr [ {
@@ -25334,8 +25336,8 @@ type derive_attr = {
2533425336
explict_nonrec : bool;
2533525337
bs_deriving : [`Has_deriving of Ast_payload.action list | `Nothing ]
2533625338
}
25337-
val process_bs_string_int :
25338-
t -> [`Nothing | `String | `Int | `Ignore] * t
25339+
val process_bs_string_int_uncurry :
25340+
t -> [`Nothing | `String | `Int | `Ignore | `Uncurry of int option ] * t
2533925341

2534025342
val process_bs_string_as :
2534125343
t -> string option * t
@@ -25359,6 +25361,9 @@ val bs_method : attr
2535925361

2536025362

2536125363
val warn_unused_attributes : t -> unit
25364+
25365+
25366+
2536225367
end = struct
2536325368
#1 "ast_attributes.ml"
2536425369
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -25509,17 +25514,24 @@ let process_derive_type attrs =
2550925514

2551025515

2551125516

25512-
let process_bs_string_int attrs =
25517+
let process_bs_string_int_uncurry attrs =
2551325518
List.fold_left
2551425519
(fun (st,attrs)
25515-
(({txt ; loc}, payload ) as attr : attr) ->
25520+
(({txt ; loc}, (payload : _ ) ) as attr : attr) ->
2551625521
match txt, st with
2551725522
| "bs.string", (`Nothing | `String)
2551825523
-> `String, attrs
2551925524
| "bs.int", (`Nothing | `Int)
2552025525
-> `Int, attrs
2552125526
| "bs.ignore", (`Nothing | `Ignore)
2552225527
-> `Ignore, attrs
25528+
25529+
| "bs.uncurry", `Nothing
25530+
->
25531+
`Uncurry (Ast_payload.is_single_int payload), attrs
25532+
(* Don't allow duplicated [bs.uncurry] since
25533+
it may introduce inconsistency in arity
25534+
*)
2552325535
| "bs.int", _
2552425536
| "bs.string", _
2552525537
| "bs.ignore", _
@@ -27300,8 +27312,8 @@ val is_unit : t -> bool
2730027312
val is_array : t -> bool
2730127313
type arg_label =
2730227314
| Label of string
27303-
(*| Label_int_lit of string * int
27304-
| Label_string_lit of string * string *)
27315+
(*| Label_int_lit of string * int
27316+
| Label_string_lit of string * string *)
2730527317
| Optional of string
2730627318
| Empty
2730727319
type arg_type =
@@ -27310,6 +27322,7 @@ type arg_type =
2731027322
| Int of (int * int ) list
2731127323
| Arg_int_lit of int
2731227324
| Arg_string_lit of string
27325+
| Fn_uncurry_arity of int (* annotated with [@bs.uncurry ] or [@bs.uncurry 2]*)
2731327326
| Array
2731427327
| Extern_unit
2731527328
| Nothing
@@ -27339,6 +27352,19 @@ val make_obj :
2733927352
t
2734027353

2734127354
val is_optional_label : string -> bool
27355+
27356+
(**
27357+
returns 0 when it can not tell arity from the syntax
27358+
*)
27359+
val get_arity : t -> int
27360+
27361+
27362+
(** fails when Ptyp_poly *)
27363+
val list_of_arrow :
27364+
t ->
27365+
t * (Asttypes.label * t * Parsetree.attributes * Location.t) list
27366+
27367+
2734227368
end = struct
2734327369
#1 "ast_core_type.ml"
2734427370
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -27378,6 +27404,7 @@ type arg_type =
2737827404
| Int of (int * int ) list (* ([`a | `b ] [@bs.int])*)
2737927405
| Arg_int_lit of int
2738027406
| Arg_string_lit of string
27407+
| Fn_uncurry_arity of int (* annotated with [@bs.uncurry ] or [@bs.uncurry 2]*)
2738127408
(* maybe we can improve it as a combination of {!Asttypes.constant} and tuple *)
2738227409
| Array
2738327410
| Extern_unit
@@ -27475,6 +27502,35 @@ let make_obj ~loc xs =
2747527502
Ast_comb.to_js_type loc @@
2747627503
Ast_helper.Typ.object_ ~loc xs Closed
2747727504

27505+
27506+
27507+
(**
27508+
27509+
{[ 'a . 'a -> 'b ]}
27510+
OCaml does not support such syntax yet
27511+
{[ 'a -> ('a. 'a -> 'b) ]}
27512+
27513+
*)
27514+
let get_arity (ty : t) =
27515+
let rec aux (ty : t) acc =
27516+
match ty.ptyp_desc with
27517+
| Ptyp_arrow(_, _ , new_ty) ->
27518+
aux new_ty (succ acc)
27519+
| Ptyp_poly (_,ty) ->
27520+
aux ty acc
27521+
| _ -> acc in
27522+
aux ty 0
27523+
27524+
let list_of_arrow (ty : t) =
27525+
let rec aux (ty : t) acc =
27526+
match ty.ptyp_desc with
27527+
| Ptyp_arrow(label,t1,t2) ->
27528+
aux t2 ((label,t1,ty.ptyp_attributes,ty.ptyp_loc) ::acc)
27529+
| Ptyp_poly(_, ty) -> (* should not happen? *)
27530+
Location.raise_errorf ~loc:ty.ptyp_loc "Unhandled poly type"
27531+
| return_type -> ty, List.rev acc
27532+
in aux ty []
27533+
2747827534
end
2747927535
module Ast_ffi_types : sig
2748027536
#1 "ast_ffi_types.mli"
@@ -27656,11 +27712,14 @@ type arg_type = Ast_core_type.arg_type =
2765627712
| Int of (int * int ) list (* ([`a | `b ] [@bs.int])*)
2765727713
| Arg_int_lit of int
2765827714
| Arg_string_lit of string
27715+
| Fn_uncurry_arity of int (* annotated with [@bs.uncurry ] or [@bs.uncurry 2]*)
2765927716
(* maybe we can improve it as a combination of {!Asttypes.constant} and tuple *)
2766027717
| Array
2766127718
| Extern_unit
2766227719
| Nothing
27663-
| Ignore
27720+
27721+
27722+
| Ignore (* annotated with [@bs.ignore] *)
2766427723

2766527724
type arg_label =
2766627725
| Label of string
@@ -28624,7 +28683,7 @@ let get_arg_type ~nolabel optional
2862428683
Arg_string_lit i, Ast_literal.type_string ~loc:ptyp.ptyp_loc ()
2862528684
end
2862628685
else
28627-
match Ast_attributes.process_bs_string_int ptyp.ptyp_attributes, ptyp.ptyp_desc with
28686+
match Ast_attributes.process_bs_string_int_uncurry ptyp.ptyp_attributes, ptyp.ptyp_desc with
2862828687
| (`String, ptyp_attributes), Ptyp_variant ( row_fields, Closed, None)
2862928688
->
2863028689
let case, result, row_fields =
@@ -28689,6 +28748,27 @@ let get_arg_type ~nolabel optional
2868928748
}
2869028749

2869128750
| (`Int, _), _ -> Location.raise_errorf ~loc:ptyp.ptyp_loc "Not a valid string type"
28751+
| (`Uncurry opt_arity, ptyp_attributes), ptyp_desc ->
28752+
let real_arity = Ast_core_type.get_arity ptyp in
28753+
(begin match opt_arity, real_arity with
28754+
| Some arity, 0 ->
28755+
Fn_uncurry_arity arity
28756+
| None, 0 ->
28757+
Location.raise_errorf
28758+
~loc:ptyp.ptyp_loc
28759+
"Can not infer the arity by syntax, either [@bs.uncurry n] or \n\
28760+
write it in arrow syntax
28761+
"
28762+
| None, arity ->
28763+
Fn_uncurry_arity arity
28764+
| Some arity, n ->
28765+
if n <> arity then
28766+
Location.raise_errorf
28767+
~loc:ptyp.ptyp_loc
28768+
"Inconsistent arity %d vs %d" arity n
28769+
else Fn_uncurry_arity arity
28770+
28771+
end, {ptyp with ptyp_attributes})
2869228772
| (`Nothing, ptyp_attributes), ptyp_desc ->
2869328773
begin match ptyp_desc with
2869428774
| Ptyp_constr ({txt = Lident "bool"}, [])
@@ -28836,15 +28916,6 @@ let process_external_attributes
2883628916
(init_st, []) prim_attributes
2883728917

2883828918

28839-
let list_of_arrow (ty : Parsetree.core_type) =
28840-
let rec aux (ty : Parsetree.core_type) acc =
28841-
match ty.ptyp_desc with
28842-
| Ptyp_arrow(label,t1,t2) ->
28843-
aux t2 ((label,t1,ty.ptyp_attributes,ty.ptyp_loc) ::acc)
28844-
| Ptyp_poly(_, ty) -> (* should not happen? *)
28845-
Location.raise_errorf ~loc:ty.ptyp_loc "Unhandled poly type"
28846-
| return_type -> ty, List.rev acc
28847-
in aux ty []
2884828919

2884928920

2885028921
(** Note that the passed [type_annotation] is already processed by visitor pattern before
@@ -28860,7 +28931,7 @@ let handle_attributes
2886028931
else `Nm_external prim_name (* need check name *)
2886128932
in
2886228933
let result_type, arg_types_ty =
28863-
list_of_arrow type_annotation in
28934+
Ast_core_type.list_of_arrow type_annotation in
2886428935

2886528936
let (st, left_attrs) =
2886628937
process_external_attributes
@@ -28930,6 +29001,9 @@ let handle_attributes
2893029001
{arg_label = Label s; arg_type},
2893129002
(label,new_ty,attr,loc)::arg_types,
2893229003
((name, [], Ast_literal.type_string ~loc ()) :: result_types)
29004+
| Fn_uncurry_arity _ ->
29005+
Location.raise_errorf ~loc
29006+
"The combination of [@@bs.obj], [@@bs.uncurry] is not supported yet"
2893329007
| Extern_unit -> assert false
2893429008
| NonNullString _
2893529009
->
@@ -28962,6 +29036,9 @@ let handle_attributes
2896229036
| Arg_int_lit _
2896329037
| Arg_string_lit _ ->
2896429038
Location.raise_errorf ~loc "bs.as is not supported with optional yet"
29039+
| Fn_uncurry_arity _ ->
29040+
Location.raise_errorf ~loc
29041+
"The combination of [@@bs.obj], [@@bs.uncurry] is not supported yet"
2896529042
| Extern_unit -> assert false
2896629043
| NonNullString _
2896729044
->

0 commit comments

Comments
 (0)