Skip to content

Commit 1ce48f4

Browse files
authored
Merge pull request #675 from bloomberg/no_publish_and_support_pipe
beautiful chaining
2 parents 7a5e849 + a2a1a8f commit 1ce48f4

21 files changed

+488
-101
lines changed

jscomp/bin/bs_ppx.ml

Lines changed: 111 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(** Bundled by bspack 08/24-11:29 *)
1+
(** Bundled by bspack 08/25-10:52 *)
22
module String_map : sig
33
#1 "string_map.mli"
44
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -115,7 +115,8 @@ type action =
115115
val is_single_string : t -> string option
116116
val is_single_int : t -> int option
117117

118-
val as_string_exp : t -> Parsetree.expression option
118+
val as_string_exp : t -> Parsetree.expression option
119+
val as_core_type : Location.t -> t -> Parsetree.core_type
119120
val as_empty_structure : t -> bool
120121
val as_ident : t -> lid option
121122
val raw_string_payload : Location.t -> string -> t
@@ -203,6 +204,11 @@ let as_string_exp (x : t ) =
203204
_}] -> Some e
204205
| _ -> None
205206

207+
let as_core_type loc x =
208+
match x with
209+
| Parsetree.PTyp x -> x
210+
| _ -> Location.raise_errorf ~loc "except a core type"
211+
206212
let as_ident (x : t ) =
207213
match x with
208214
| PStr [
@@ -1393,7 +1399,7 @@ val init : int -> (int -> 'a) -> 'a list
13931399
val take : int -> 'a list -> 'a list * 'a list
13941400
val try_take : int -> 'a list -> 'a list * int * 'a list
13951401

1396-
val exclude_tail : 'a list -> 'a list
1402+
val exclude_tail : 'a list -> 'a * 'a list
13971403

13981404
val filter_map2 : ('a -> 'b -> 'c option) -> 'a list -> 'b list -> 'c list
13991405

@@ -1457,6 +1463,8 @@ val ref_push : 'a -> 'a t -> unit
14571463

14581464
val ref_pop : 'a t -> 'a
14591465

1466+
val rev_except_last : 'a list -> 'a list * 'a
1467+
14601468
end = struct
14611469
#1 "ext_list.ml"
14621470
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -1645,11 +1653,11 @@ let try_take n l =
16451653
l, arr_length, []
16461654
else Array.to_list (Array.sub arr 0 n ), n, (Array.to_list (Array.sub arr n (arr_length - n)))
16471655

1648-
let exclude_tail (x : 'a list) : 'a list =
1656+
let exclude_tail (x : 'a list) =
16491657
let rec aux acc x =
16501658
match x with
16511659
| [] -> invalid_arg "Ext_list.exclude_tail"
1652-
| [ _ ] -> List.rev acc
1660+
| [ x ] -> x, List.rev acc
16531661
| y0::ys -> aux (y0::acc) ys in
16541662
aux [] x
16551663

@@ -1794,6 +1802,14 @@ let ref_pop refs =
17941802
refs := rest ;
17951803
x
17961804

1805+
let rev_except_last xs =
1806+
let rec aux acc xs =
1807+
match xs with
1808+
| [ ] -> invalid_arg "Ext_list.rev_except_last"
1809+
| [ x ] -> acc ,x
1810+
| x :: xs -> aux (x::acc) xs in
1811+
aux [] xs
1812+
17971813
end
17981814
module Ast_comb : sig
17991815
#1 "ast_comb.mli"
@@ -4218,10 +4234,11 @@ type js_call = {
42184234
splice : bool ;
42194235
name : string;
42204236
}
4221-
4237+
type pipe = bool
42224238
type js_send = {
42234239
splice : bool ;
4224-
name : string
4240+
name : string ;
4241+
pipe : pipe
42254242
} (* we know it is a js send, but what will happen if you pass an ocaml objct *)
42264243

42274244
type js_val = string external_module
@@ -4260,7 +4277,9 @@ type t =
42604277

42614278

42624279

4263-
4280+
(**
4281+
return value is of [pval_type, pval_prim]
4282+
*)
42644283
val handle_attributes_as_string :
42654284
Bs_loc.t ->
42664285
string ->
@@ -4269,6 +4288,7 @@ val handle_attributes_as_string :
42694288
string ->
42704289
Ast_core_type.t * string list
42714290

4291+
42724292
val bs_external : string
42734293
val to_string : t -> string
42744294
val from_string : string -> t
@@ -4313,15 +4333,16 @@ type 'a external_module = {
43134333
external_module_name : external_module_name option;
43144334
}
43154335

4316-
4336+
type pipe = bool
43174337
type js_call = {
43184338
splice : bool ;
43194339
name : string;
43204340
}
43214341

43224342
type js_send = {
43234343
splice : bool ;
4324-
name : string
4344+
name : string ;
4345+
pipe : bool
43254346
} (* we know it is a js send, but what will happen if you pass an ocaml objct *)
43264347

43274348
type js_val = string external_module
@@ -4345,6 +4366,8 @@ type ffi =
43454366
| Js_module_as_class of external_module_name
43464367
| Js_call of js_call external_module
43474368
| Js_send of js_send
4369+
(* Note how we encode it will have a semantic difference
4370+
*)
43484371
| Js_new of js_val
43494372
| Js_set of string
43504373
| Js_get of string
@@ -4460,7 +4483,8 @@ type st =
44604483
{ val_name : name_source;
44614484
external_module_name : external_module_name option;
44624485
module_as_val : external_module_name option;
4463-
val_send : name_source;
4486+
val_send : name_source ;
4487+
val_send_pipe : [`Nm_na | `Type of Ast_core_type.t ];
44644488
splice : bool ; (* mutable *)
44654489
set_index : bool; (* mutable *)
44664490
get_index : bool;
@@ -4478,6 +4502,7 @@ let init_st =
44784502
external_module_name = None ;
44794503
module_as_val = None;
44804504
val_send = `Nm_na;
4505+
val_send_pipe = `Nm_na;
44814506
splice = false;
44824507
set_index = false;
44834508
get_index = false;
@@ -4516,7 +4541,8 @@ let handle_attributes
45164541
(loc : Bs_loc.t)
45174542
(pval_prim : string )
45184543
(type_annotation : Parsetree.core_type)
4519-
(prim_attributes : Ast_attributes.t) (prim_name : string) =
4544+
(prim_attributes : Ast_attributes.t) (prim_name : string)
4545+
: Ast_core_type.t * string * t =
45204546
let prim_name_or_pval_prim =
45214547
if String.length prim_name = 0 then `Nm_val pval_prim
45224548
else `Nm_external prim_name (* need check name *)
@@ -4573,6 +4599,9 @@ let handle_attributes
45734599
| "bs.splice" -> {st with splice = true}
45744600
| "bs.send" ->
45754601
{ st with val_send = name_from_payload_or_prim payload}
4602+
| "bs.send.pipe"
4603+
->
4604+
{ st with val_send_pipe = `Type (Ast_payload.as_core_type loc payload)}
45764605
| "bs.set" ->
45774606
{st with set_name = name_from_payload_or_prim payload}
45784607
| "bs.get" -> {st with get_name = name_from_payload_or_prim payload}
@@ -4591,12 +4620,15 @@ let handle_attributes
45914620
if Ast_core_type.is_array ty then Array
45924621
else if Ast_core_type.is_unit ty then Unit
45934622
else (Ast_core_type.string_type ty :> arg_type) in
4623+
let translate_arg_type =
4624+
(fun (label, ty) ->
4625+
{ arg_label = Ast_core_type.label_name label ;
4626+
arg_type = aux ty
4627+
}) in
45944628
let arg_types =
4595-
List.map (fun (label, ty) ->
4596-
{ arg_label = Ast_core_type.label_name label ;
4597-
arg_type = aux ty
4598-
}) arg_types_ty in
4599-
let result_type = aux result_type_ty in
4629+
List.map translate_arg_type arg_types_ty in
4630+
let result_type = aux result_type_ty in
4631+
let object_type = ref None in
46004632
let ffi =
46014633
match st with
46024634
| {mk_obj = true;
@@ -4605,12 +4637,13 @@ let handle_attributes
46054637
external_module_name = None ;
46064638
module_as_val = None;
46074639
val_send = `Nm_na;
4640+
val_send_pipe = `Nm_na;
46084641
splice = false;
46094642
new_name = `Nm_na;
46104643
call_name = `Nm_na;
46114644
set_name = `Nm_na ;
46124645
get_name = `Nm_na ;
4613-
get_index = false ;
4646+
get_index = false ;
46144647
} ->
46154648
let labels = List.map (function
46164649
| {arg_type = Unit ; arg_label = (Empty as l)}
@@ -4633,6 +4666,7 @@ let handle_attributes
46334666
external_module_name = None ;
46344667
module_as_val = None;
46354668
val_send = `Nm_na;
4669+
val_send_pipe = `Nm_na;
46364670
splice = false;
46374671
get_index = false;
46384672
new_name = `Nm_na;
@@ -4661,6 +4695,8 @@ let handle_attributes
46614695
external_module_name = None ;
46624696
module_as_val = None;
46634697
val_send = `Nm_na;
4698+
val_send_pipe = `Nm_na;
4699+
46644700
splice = false;
46654701
new_name = `Nm_na;
46664702
call_name = `Nm_na;
@@ -4692,6 +4728,8 @@ let handle_attributes
46924728
*)
46934729
external_module_name = None ;
46944730
val_send = `Nm_na;
4731+
val_send_pipe = `Nm_na;
4732+
46954733
splice = false;
46964734
call_name = `Nm_na;
46974735
set_name = `Nm_na ;
@@ -4720,6 +4758,8 @@ let handle_attributes
47204758
val_name = `Nm_na ;
47214759
module_as_val = None;
47224760
val_send = `Nm_na ;
4761+
val_send_pipe = `Nm_na;
4762+
47234763
set_index = false;
47244764
get_index = false;
47254765
new_name = `Nm_na;
@@ -4736,6 +4776,7 @@ let handle_attributes
47364776
call_name = `Nm_na ;
47374777
module_as_val = None;
47384778
val_send = `Nm_na ;
4779+
val_send_pipe = `Nm_na;
47394780
set_index = false;
47404781
get_index = false;
47414782
new_name = `Nm_na;
@@ -4754,6 +4795,7 @@ let handle_attributes
47544795
call_name = `Nm_na ;
47554796
module_as_val = None;
47564797
val_send = `Nm_na ;
4798+
val_send_pipe = `Nm_na;
47574799
set_index = false;
47584800
get_index = false;
47594801
new_name = `Nm_na;
@@ -4770,7 +4812,7 @@ let handle_attributes
47704812

47714813
| {val_send = (`Nm_val name | `Nm_external name | `Nm_payload name);
47724814
splice;
4773-
4815+
val_send_pipe = `Nm_na;
47744816
val_name = `Nm_na ;
47754817
call_name = `Nm_na ;
47764818
module_as_val = None;
@@ -4783,13 +4825,38 @@ let handle_attributes
47834825
} ->
47844826
begin match arg_types with
47854827
| _self :: _args ->
4786-
Js_send {splice ; name}
4828+
Js_send {splice ; name; pipe = false}
47874829
| _ ->
47884830
Location.raise_errorf ~loc "Ill defined attribute [@@bs.send] (at least one argument)"
47894831
end
47904832
| {val_send = #bundle_source}
47914833
-> Location.raise_errorf ~loc "conflict attributes found"
47924834

4835+
| {val_send_pipe = `Type typ;
4836+
splice = (false as splice);
4837+
val_send = `Nm_na;
4838+
val_name = `Nm_na ;
4839+
call_name = `Nm_na ;
4840+
module_as_val = None;
4841+
set_index = false;
4842+
get_index = false;
4843+
new_name = `Nm_na;
4844+
set_name = `Nm_na ;
4845+
get_name = `Nm_na ;
4846+
external_module_name = None ;
4847+
} ->
4848+
begin match arg_types with
4849+
| _self :: _args ->
4850+
object_type := Some typ ;
4851+
Js_send {splice ;
4852+
name = string_of_bundle_source prim_name_or_pval_prim;
4853+
pipe = true}
4854+
| _ ->
4855+
Location.raise_errorf ~loc "Ill defined attribute [@@bs.send] (at least one argument)"
4856+
end
4857+
| {val_send_pipe = `Type _ }
4858+
-> Location.raise_errorf ~loc "conflict attributes found"
4859+
47934860
| {new_name = (`Nm_val name | `Nm_external name | `Nm_payload name);
47944861
external_module_name;
47954862

@@ -4799,6 +4866,7 @@ let handle_attributes
47994866
set_index = false;
48004867
get_index = false;
48014868
val_send = `Nm_na ;
4869+
val_send_pipe = `Nm_na;
48024870
set_name = `Nm_na ;
48034871
get_name = `Nm_na
48044872
}
@@ -4814,6 +4882,7 @@ let handle_attributes
48144882
set_index = false;
48154883
get_index = false;
48164884
val_send = `Nm_na ;
4885+
val_send_pipe = `Nm_na;
48174886
new_name = `Nm_na ;
48184887
get_name = `Nm_na ;
48194888
external_module_name = None
@@ -4835,6 +4904,7 @@ let handle_attributes
48354904
set_index = false;
48364905
get_index = false;
48374906
val_send = `Nm_na ;
4907+
val_send_pipe = `Nm_na;
48384908
new_name = `Nm_na ;
48394909
set_name = `Nm_na ;
48404910
external_module_name = None
@@ -4871,12 +4941,28 @@ let handle_attributes
48714941
end
48724942
| (_, _), Ast_core_type.Empty -> acc
48734943
) arg_types_ty arg_labels []) in
4874-
Ast_core_type.replace_result type_annotation result
4875-
| _, _ -> type_annotation) ,
4944+
Ast_core_type.replace_result type_annotation result
4945+
| Js_send {pipe = true }, _ ->
4946+
begin match !object_type with
4947+
| Some obj ->
4948+
Ast_core_type.replace_result type_annotation
4949+
(Ast_helper.Typ.arrow ~loc "" obj result_type_ty)
4950+
| None -> assert false
4951+
end
4952+
| _, _ -> type_annotation
4953+
) ,
4954+
4955+
(* TODO: document *)
48764956
(match ffi , prim_name with
4877-
| Obj_create _ , _ -> prim_name
4878-
| _ , "" -> pval_prim
4879-
| _, _ -> prim_name), Bs(arg_types, result_type, ffi)
4957+
| Obj_create _ , _ -> prim_name
4958+
| _ , "" -> pval_prim
4959+
| _, _ -> prim_name),
4960+
(match !object_type with
4961+
|None ->
4962+
Bs(arg_types, result_type, ffi)
4963+
| Some obj ->
4964+
Bs(arg_types @ [translate_arg_type ("", obj) ], result_type, ffi)
4965+
)
48804966

48814967

48824968
let handle_attributes_as_string

0 commit comments

Comments
 (0)