Skip to content

Commit 08c3628

Browse files
author
Hongbo Zhang
committed
beautiful chaining
1 parent 56823c9 commit 08c3628

12 files changed

+347
-65
lines changed

jscomp/bin/bs_ppx.ml

Lines changed: 94 additions & 21 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/24-16:25 *)
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 [
@@ -4218,10 +4224,11 @@ type js_call = {
42184224
splice : bool ;
42194225
name : string;
42204226
}
4221-
4227+
type pipe = bool
42224228
type js_send = {
42234229
splice : bool ;
4224-
name : string
4230+
name : string ;
4231+
pipe : pipe
42254232
} (* we know it is a js send, but what will happen if you pass an ocaml objct *)
42264233

42274234
type js_val = string external_module
@@ -4313,15 +4320,16 @@ type 'a external_module = {
43134320
external_module_name : external_module_name option;
43144321
}
43154322

4316-
4323+
type pipe = bool
43174324
type js_call = {
43184325
splice : bool ;
43194326
name : string;
43204327
}
43214328

43224329
type js_send = {
43234330
splice : bool ;
4324-
name : string
4331+
name : string ;
4332+
pipe : bool
43254333
} (* we know it is a js send, but what will happen if you pass an ocaml objct *)
43264334

43274335
type js_val = string external_module
@@ -4345,6 +4353,8 @@ type ffi =
43454353
| Js_module_as_class of external_module_name
43464354
| Js_call of js_call external_module
43474355
| Js_send of js_send
4356+
(* Note how we encode it will have a semantic difference
4357+
*)
43484358
| Js_new of js_val
43494359
| Js_set of string
43504360
| Js_get of string
@@ -4460,7 +4470,8 @@ type st =
44604470
{ val_name : name_source;
44614471
external_module_name : external_module_name option;
44624472
module_as_val : external_module_name option;
4463-
val_send : name_source;
4473+
val_send : name_source ;
4474+
val_send_pipe : [`Nm_na | `Type of Ast_core_type.t ];
44644475
splice : bool ; (* mutable *)
44654476
set_index : bool; (* mutable *)
44664477
get_index : bool;
@@ -4478,6 +4489,7 @@ let init_st =
44784489
external_module_name = None ;
44794490
module_as_val = None;
44804491
val_send = `Nm_na;
4492+
val_send_pipe = `Nm_na;
44814493
splice = false;
44824494
set_index = false;
44834495
get_index = false;
@@ -4516,7 +4528,8 @@ let handle_attributes
45164528
(loc : Bs_loc.t)
45174529
(pval_prim : string )
45184530
(type_annotation : Parsetree.core_type)
4519-
(prim_attributes : Ast_attributes.t) (prim_name : string) =
4531+
(prim_attributes : Ast_attributes.t) (prim_name : string)
4532+
: Ast_core_type.t * string * t =
45204533
let prim_name_or_pval_prim =
45214534
if String.length prim_name = 0 then `Nm_val pval_prim
45224535
else `Nm_external prim_name (* need check name *)
@@ -4573,6 +4586,9 @@ let handle_attributes
45734586
| "bs.splice" -> {st with splice = true}
45744587
| "bs.send" ->
45754588
{ st with val_send = name_from_payload_or_prim payload}
4589+
| "bs.send.pipe"
4590+
->
4591+
{ st with val_send_pipe = `Type (Ast_payload.as_core_type loc payload)}
45764592
| "bs.set" ->
45774593
{st with set_name = name_from_payload_or_prim payload}
45784594
| "bs.get" -> {st with get_name = name_from_payload_or_prim payload}
@@ -4591,12 +4607,15 @@ let handle_attributes
45914607
if Ast_core_type.is_array ty then Array
45924608
else if Ast_core_type.is_unit ty then Unit
45934609
else (Ast_core_type.string_type ty :> arg_type) in
4610+
let translate_arg_type =
4611+
(fun (label, ty) ->
4612+
{ arg_label = Ast_core_type.label_name label ;
4613+
arg_type = aux ty
4614+
}) in
45944615
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
4616+
List.map translate_arg_type arg_types_ty in
4617+
let result_type = aux result_type_ty in
4618+
let object_type = ref None in
46004619
let ffi =
46014620
match st with
46024621
| {mk_obj = true;
@@ -4605,12 +4624,13 @@ let handle_attributes
46054624
external_module_name = None ;
46064625
module_as_val = None;
46074626
val_send = `Nm_na;
4627+
val_send_pipe = `Nm_na;
46084628
splice = false;
46094629
new_name = `Nm_na;
46104630
call_name = `Nm_na;
46114631
set_name = `Nm_na ;
46124632
get_name = `Nm_na ;
4613-
get_index = false ;
4633+
get_index = false ;
46144634
} ->
46154635
let labels = List.map (function
46164636
| {arg_type = Unit ; arg_label = (Empty as l)}
@@ -4633,6 +4653,7 @@ let handle_attributes
46334653
external_module_name = None ;
46344654
module_as_val = None;
46354655
val_send = `Nm_na;
4656+
val_send_pipe = `Nm_na;
46364657
splice = false;
46374658
get_index = false;
46384659
new_name = `Nm_na;
@@ -4661,6 +4682,8 @@ let handle_attributes
46614682
external_module_name = None ;
46624683
module_as_val = None;
46634684
val_send = `Nm_na;
4685+
val_send_pipe = `Nm_na;
4686+
46644687
splice = false;
46654688
new_name = `Nm_na;
46664689
call_name = `Nm_na;
@@ -4692,6 +4715,8 @@ let handle_attributes
46924715
*)
46934716
external_module_name = None ;
46944717
val_send = `Nm_na;
4718+
val_send_pipe = `Nm_na;
4719+
46954720
splice = false;
46964721
call_name = `Nm_na;
46974722
set_name = `Nm_na ;
@@ -4720,6 +4745,8 @@ let handle_attributes
47204745
val_name = `Nm_na ;
47214746
module_as_val = None;
47224747
val_send = `Nm_na ;
4748+
val_send_pipe = `Nm_na;
4749+
47234750
set_index = false;
47244751
get_index = false;
47254752
new_name = `Nm_na;
@@ -4736,6 +4763,7 @@ let handle_attributes
47364763
call_name = `Nm_na ;
47374764
module_as_val = None;
47384765
val_send = `Nm_na ;
4766+
val_send_pipe = `Nm_na;
47394767
set_index = false;
47404768
get_index = false;
47414769
new_name = `Nm_na;
@@ -4754,6 +4782,7 @@ let handle_attributes
47544782
call_name = `Nm_na ;
47554783
module_as_val = None;
47564784
val_send = `Nm_na ;
4785+
val_send_pipe = `Nm_na;
47574786
set_index = false;
47584787
get_index = false;
47594788
new_name = `Nm_na;
@@ -4770,7 +4799,7 @@ let handle_attributes
47704799

47714800
| {val_send = (`Nm_val name | `Nm_external name | `Nm_payload name);
47724801
splice;
4773-
4802+
val_send_pipe = `Nm_na;
47744803
val_name = `Nm_na ;
47754804
call_name = `Nm_na ;
47764805
module_as_val = None;
@@ -4783,13 +4812,38 @@ let handle_attributes
47834812
} ->
47844813
begin match arg_types with
47854814
| _self :: _args ->
4786-
Js_send {splice ; name}
4815+
Js_send {splice ; name; pipe = false}
47874816
| _ ->
47884817
Location.raise_errorf ~loc "Ill defined attribute [@@bs.send] (at least one argument)"
47894818
end
47904819
| {val_send = #bundle_source}
47914820
-> Location.raise_errorf ~loc "conflict attributes found"
47924821

4822+
| {val_send_pipe = `Type typ;
4823+
splice = (false as splice);
4824+
val_send = `Nm_na;
4825+
val_name = `Nm_na ;
4826+
call_name = `Nm_na ;
4827+
module_as_val = None;
4828+
set_index = false;
4829+
get_index = false;
4830+
new_name = `Nm_na;
4831+
set_name = `Nm_na ;
4832+
get_name = `Nm_na ;
4833+
external_module_name = None ;
4834+
} ->
4835+
begin match arg_types with
4836+
| _self :: _args ->
4837+
object_type := Some typ ;
4838+
Js_send {splice ;
4839+
name = string_of_bundle_source prim_name_or_pval_prim;
4840+
pipe = true}
4841+
| _ ->
4842+
Location.raise_errorf ~loc "Ill defined attribute [@@bs.send] (at least one argument)"
4843+
end
4844+
| {val_send_pipe = `Type _ }
4845+
-> Location.raise_errorf ~loc "conflict attributes found"
4846+
47934847
| {new_name = (`Nm_val name | `Nm_external name | `Nm_payload name);
47944848
external_module_name;
47954849

@@ -4799,6 +4853,7 @@ let handle_attributes
47994853
set_index = false;
48004854
get_index = false;
48014855
val_send = `Nm_na ;
4856+
val_send_pipe = `Nm_na;
48024857
set_name = `Nm_na ;
48034858
get_name = `Nm_na
48044859
}
@@ -4814,6 +4869,7 @@ let handle_attributes
48144869
set_index = false;
48154870
get_index = false;
48164871
val_send = `Nm_na ;
4872+
val_send_pipe = `Nm_na;
48174873
new_name = `Nm_na ;
48184874
get_name = `Nm_na ;
48194875
external_module_name = None
@@ -4835,6 +4891,7 @@ let handle_attributes
48354891
set_index = false;
48364892
get_index = false;
48374893
val_send = `Nm_na ;
4894+
val_send_pipe = `Nm_na;
48384895
new_name = `Nm_na ;
48394896
set_name = `Nm_na ;
48404897
external_module_name = None
@@ -4871,12 +4928,28 @@ let handle_attributes
48714928
end
48724929
| (_, _), Ast_core_type.Empty -> acc
48734930
) arg_types_ty arg_labels []) in
4874-
Ast_core_type.replace_result type_annotation result
4875-
| _, _ -> type_annotation) ,
4931+
Ast_core_type.replace_result type_annotation result
4932+
| Js_send {pipe = true }, _ ->
4933+
begin match !object_type with
4934+
| Some obj ->
4935+
Ast_core_type.replace_result type_annotation
4936+
(Ast_helper.Typ.arrow ~loc "" obj result_type_ty)
4937+
| None -> assert false
4938+
end
4939+
| _, _ -> type_annotation
4940+
) ,
4941+
4942+
(* TODO: document *)
48764943
(match ffi , prim_name with
4877-
| Obj_create _ , _ -> prim_name
4878-
| _ , "" -> pval_prim
4879-
| _, _ -> prim_name), Bs(arg_types, result_type, ffi)
4944+
| Obj_create _ , _ -> prim_name
4945+
| _ , "" -> pval_prim
4946+
| _, _ -> prim_name),
4947+
(match !object_type with
4948+
|None ->
4949+
Bs(arg_types, result_type, ffi)
4950+
| Some obj ->
4951+
Bs(arg_types @ [translate_arg_type ("", obj) ], result_type, ffi)
4952+
)
48804953

48814954

48824955
let handle_attributes_as_string

0 commit comments

Comments
 (0)