Skip to content

Commit 123f7d3

Browse files
authored
Merge pull request #4580 from BuckleScript/ffi_clean_up2
continue ffi clean up, remove some intermediate allocation
2 parents 9f61c3f + de3b14a commit 123f7d3

17 files changed

+209
-493
lines changed

jscomp/core/js_of_lam_variant.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type arg_expression =
3131
| Splice2 of E.t * E.t
3232

3333
(* we need destruct [undefined] when input is optional *)
34-
let eval (arg : J.expression) (dispatches : (Ast_compatible.hash_label * string) list ) : E.t =
34+
let eval (arg : J.expression) (dispatches : (string * string) list ) : E.t =
3535
if arg == E.undefined then E.undefined
3636
else
3737
match arg.expression_desc with
@@ -51,7 +51,7 @@ let eval (arg : J.expression) (dispatches : (Ast_compatible.hash_label * string)
5151

5252
(** invariant: optional is not allowed in this case *)
5353
(** arg is a polyvar *)
54-
let eval_as_event (arg : J.expression) (dispatches : (Ast_compatible.hash_label * string) list option) =
54+
let eval_as_event (arg : J.expression) (dispatches : (string * string) list option) =
5555
match arg.expression_desc with
5656
| Caml_block([{expression_desc = Str(_,s)}; cb], _, _, Blk_poly_var ) when Js_analyzer.no_side_effect_expression cb
5757
->
@@ -94,7 +94,7 @@ let eval_as_event (arg : J.expression) (dispatches : (Ast_compatible.hash_label
9494
*)
9595

9696
(* we need destruct [undefined] when input is optional *)
97-
let eval_as_int (arg : J.expression) (dispatches : (Ast_compatible.hash_label * int) list ) : E.t =
97+
let eval_as_int (arg : J.expression) (dispatches : (string * int) list ) : E.t =
9898
if arg == E.undefined then E.undefined else
9999
match arg.expression_desc with
100100
| Str(_,i) ->

jscomp/core/js_of_lam_variant.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ type arg_expression =
3131

3232
val eval :
3333
J.expression ->
34-
(Ast_compatible.hash_label * string) list
34+
(string * string) list
3535
-> J.expression
3636

3737
val eval_as_event :
3838
J.expression ->
39-
(Ast_compatible.hash_label * string) list option
39+
(string * string) list option
4040
-> arg_expression
4141

4242
val eval_as_int :
4343
J.expression ->
44-
(Ast_compatible.hash_label * int) list ->
44+
(string * int) list ->
4545
J.expression
4646

4747
val eval_as_unwrap : J.expression -> J.expression

jscomp/syntax/ast_compatible.ml

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ open Parsetree
2828
let default_loc = Location.none
2929

3030

31-
type poly_var_label = Asttypes.label Asttypes.loc
3231

3332

3433

@@ -37,8 +36,9 @@ type poly_var_label = Asttypes.label Asttypes.loc
3736

3837

3938

40-
let arrow ?(loc=default_loc) ?(attrs = []) a b =
41-
Ast_helper.Typ.arrow ~loc ~attrs Nolabel a b
39+
40+
let arrow ?loc ?attrs a b =
41+
Ast_helper.Typ.arrow ?loc ?attrs Nolabel a b
4242

4343
let apply_simple
4444
?(loc = default_loc)
@@ -105,21 +105,6 @@ let fun_
105105
pexp_desc = Pexp_fun(Nolabel,None, pat, exp)
106106
}
107107

108-
(* let opt_label s =
109-
Asttypes.Optional s *)
110-
111-
(* let label_fun
112-
?(loc = default_loc)
113-
?(attrs = [])
114-
~label
115-
pat
116-
exp =
117-
{
118-
pexp_loc = loc;
119-
pexp_attributes = attrs;
120-
pexp_desc = Pexp_fun(label, None, pat, exp)
121-
} *)
122-
type hash_label = string
123108

124109

125110
let const_exp_string
@@ -133,15 +118,7 @@ let const_exp_string
133118
pexp_desc = Pexp_constant(Pconst_string(s,delimiter))
134119
}
135120

136-
let const_hash_label
137-
?(loc = default_loc)
138-
?(attrs = [])
139-
(s : hash_label) : expression =
140-
{
141-
pexp_loc = loc;
142-
pexp_attributes = attrs;
143-
pexp_desc = Pexp_constant(Pconst_string(s,None))
144-
}
121+
145122

146123
let const_exp_int
147124
?(loc = default_loc)
@@ -165,19 +142,6 @@ let apply_labels
165142
fn,
166143
Ext_list.map args (fun (l,a) -> Asttypes.Labelled l, a) ) }
167144

168-
let object_
169-
?(loc= default_loc)
170-
?(attrs = [])
171-
(fields : (Asttypes.label Asttypes.loc * attributes * core_type) list)
172-
flg : core_type =
173-
{
174-
ptyp_desc =
175-
Ptyp_object(
176-
Ext_list.map fields (fun (a,b,c) ->
177-
Parsetree.Otag (a,b,c)),flg);
178-
ptyp_loc = loc;
179-
ptyp_attributes = attrs
180-
}
181145

182146

183147

jscomp/syntax/ast_compatible.mli

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525

26-
type poly_var_label = Asttypes.label Asttypes.loc
26+
2727

2828

2929

@@ -33,7 +33,7 @@ type poly_var_label = Asttypes.label Asttypes.loc
3333

3434
type loc = Location.t
3535
type attrs = Parsetree.attribute list
36-
type hash_label = string
36+
3737
open Parsetree
3838

3939

@@ -50,20 +50,13 @@ val const_exp_int:
5050
int ->
5151
expression
5252

53-
val const_hash_label :
54-
?loc:Location.t ->
55-
?attrs:attrs ->
56-
string ->
57-
expression
5853

5954

6055
val const_exp_int_list_as_array:
6156
int list ->
6257
expression
6358

64-
(* val const_exp_string_list_as_array:
65-
string list ->
66-
expression *)
59+
6760

6861

6962
val apply_simple:
@@ -150,12 +143,6 @@ val opt_arrow:
150143
core_type ->
151144
core_type
152145

153-
val object_:
154-
?loc:loc ->
155-
?attrs:attrs ->
156-
(string Asttypes.loc * attributes * core_type) list ->
157-
Asttypes.closed_flag ->
158-
core_type
159146

160147

161148
(* val nonrec_type_str:

jscomp/syntax/ast_core_type.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ let from_labels ~loc arity labels
104104
Typ.var ~loc ("a" ^ string_of_int i)))) in
105105
let result_type =
106106
Ast_comb.to_js_type loc
107-
(Ast_compatible.object_ ~loc
108-
(Ext_list.map2 labels tyvars (fun x y -> x ,[], y)) Closed)
107+
(Typ.object_ ~loc
108+
(Ext_list.map2 labels tyvars
109+
(fun x y -> Parsetree.Otag (x ,[], y))) Closed)
109110
in
110111
Ext_list.fold_right2 labels tyvars result_type
111112
(fun label (* {loc ; txt = label }*)
@@ -115,7 +116,7 @@ let from_labels ~loc arity labels
115116

116117
let make_obj ~loc xs =
117118
Ast_comb.to_js_type loc
118-
(Ast_compatible.object_ ~loc xs Closed)
119+
(Typ.object_ ~loc xs Closed)
119120

120121

121122

jscomp/syntax/ast_core_type.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ val from_labels :
4747

4848
val make_obj :
4949
loc:Location.t ->
50-
(string Asttypes.loc * Parsetree.attributes * t) list ->
50+
Parsetree.object_field list ->
5151
t
5252

5353
val is_user_option : t -> bool

jscomp/syntax/ast_derive_js_mapper.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,10 @@ let init () =
435435

436436
let objType flag =
437437
Ast_comb.to_js_type loc @@
438-
Ast_compatible.object_
438+
Typ.object_
439439
(Ext_list.map label_declarations
440440
(fun {pld_name ; pld_type } ->
441-
pld_name, [], pld_type))
441+
Parsetree.Otag(pld_name, [], pld_type)))
442442
flag in
443443
newTypeStr +?
444444
[

jscomp/syntax/ast_external_process.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ let process_obj
400400
} ->
401401
if String.length prim_name <> 0 then
402402
Location.raise_errorf ~loc "[@@bs.obj] expect external names to be empty string";
403-
let arg_kinds, new_arg_types_ty, result_types =
403+
let arg_kinds, new_arg_types_ty, (result_types : Parsetree.object_field list) =
404404
Ext_list.fold_right arg_types_ty ( [], [], [])
405405
(fun param_type ( arg_labels, (arg_types : Ast_compatible.param_type list), result_types) ->
406406
let arg_label = param_type.label in
@@ -426,22 +426,22 @@ let process_obj
426426
{obj_arg_label = External_arg_spec.obj_label s;
427427
obj_arg_type },
428428
arg_types, (* ignored in [arg_types], reserved in [result_types] *)
429-
(({Asttypes.txt = name; loc} , [], new_ty) :: result_types)
429+
(Parsetree.Otag({Asttypes.txt = name; loc} , [], new_ty) :: result_types)
430430
| Nothing ->
431431
let s = (Lam_methname.translate name) in
432432
{obj_arg_label = External_arg_spec.obj_label s ; obj_arg_type },
433433
{param_type with ty = new_ty}::arg_types,
434-
(({Asttypes.txt = name; loc} , [], new_ty) :: result_types)
434+
(Otag ({Asttypes.txt = name; loc} , [], new_ty) :: result_types)
435435
| Int _ ->
436436
let s = Lam_methname.translate name in
437437
{obj_arg_label = External_arg_spec.obj_label s; obj_arg_type},
438438
{param_type with ty = new_ty}::arg_types,
439-
(({Asttypes.txt = name; loc}, [], Ast_literal.type_int ~loc ()) :: result_types)
439+
(Otag ({Asttypes.txt = name; loc}, [], Ast_literal.type_int ~loc ()) :: result_types)
440440
| Poly_var_string _ ->
441441
let s = Lam_methname.translate name in
442442
{obj_arg_label = External_arg_spec.obj_label s; obj_arg_type},
443443
{param_type with ty = new_ty }::arg_types,
444-
(({Asttypes.txt = name; loc}, [], Ast_literal.type_string ~loc ()) :: result_types)
444+
(Otag({Asttypes.txt = name; loc}, [], Ast_literal.type_string ~loc ()) :: result_types)
445445
| Fn_uncurry_arity _ ->
446446
Location.raise_errorf ~loc
447447
"The combination of [@@bs.obj], [@@bs.uncurry] is not supported yet"
@@ -464,17 +464,17 @@ let process_obj
464464
let s = (Lam_methname.translate name) in
465465
{obj_arg_label = External_arg_spec.optional s; obj_arg_type},
466466
param_type :: arg_types,
467-
( ({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc ty) :: result_types)
467+
( Parsetree.Otag ({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc ty) :: result_types)
468468
| Int _ ->
469469
let s = Lam_methname.translate name in
470470
{obj_arg_label = External_arg_spec.optional s ; obj_arg_type },
471471
param_type :: arg_types,
472-
(({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc @@ Ast_literal.type_int ~loc ()) :: result_types)
472+
(Otag ({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc @@ Ast_literal.type_int ~loc ()) :: result_types)
473473
| Poly_var_string _ ->
474474
let s = Lam_methname.translate name in
475475
{obj_arg_label = External_arg_spec.optional s ; obj_arg_type },
476476
param_type::arg_types,
477-
(({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc @@ Ast_literal.type_string ~loc ()) :: result_types)
477+
(Otag ({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc @@ Ast_literal.type_string ~loc ()) :: result_types)
478478
| Arg_cst _
479479
->
480480
Location.raise_errorf ~loc "bs.as is not supported with optional yet"

jscomp/syntax/ast_polyvar.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
val map_row_fields_into_ints:
2727
Location.t ->
2828
Parsetree.row_field list ->
29-
(Ast_compatible.hash_label * int ) list
29+
(string * int ) list
3030

3131
val map_constructor_declarations_into_ints:
3232
Parsetree.constructor_declaration list ->

jscomp/syntax/ast_util.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ let ocaml_obj_as_js_object
6666

6767
let result = Typ.var ~loc val_name.txt in
6868
result ,
69-
((val_name , [], result ) ::
69+
(Parsetree.Otag (val_name , [], result ) ::
7070
(if is_mutable then
71-
[{val_name with txt = val_name.txt ^ Literals.setter_suffix},[],
72-
Ast_typ_uncurry.to_method_type loc mapper Nolabel result (Ast_literal.type_unit ~loc ()) ]
71+
[ Otag ({val_name with txt = val_name.txt ^ Literals.setter_suffix},[],
72+
Ast_typ_uncurry.to_method_type loc mapper Nolabel result (Ast_literal.type_unit ~loc ())) ]
7373
else
7474
[]) )
7575
in
@@ -84,7 +84,8 @@ let ocaml_obj_as_js_object
8484
for public object type its [@bs.meth] it does not depend on itself
8585
while for label argument it is [@bs.this] which depends internal object
8686
*)
87-
let internal_label_attr_types, public_label_attr_types =
87+
let (internal_label_attr_types : Parsetree.object_field list),
88+
(public_label_attr_types : Parsetree.object_field list) =
8889
Ext_list.fold_right clfs ([], [])
8990
(fun ({pcf_loc = loc} as x : Parsetree.class_field)
9091
(label_attr_types, public_label_attr_types) ->
@@ -102,9 +103,9 @@ let ocaml_obj_as_js_object
102103
->
103104
let method_type =
104105
Ast_typ_uncurry.generate_arg_type x.pcf_loc mapper label.txt lbl pat e in
105-
((label, [], method_type) :: label_attr_types),
106+
(Parsetree.Otag(label, [], method_type) :: label_attr_types),
106107
(if public_flag = Public then
107-
(label, [], method_type) :: public_label_attr_types
108+
Parsetree.Otag (label, [], method_type) :: public_label_attr_types
108109
else
109110
public_label_attr_types)
110111

0 commit comments

Comments
 (0)