Skip to content

Commit 18eb14d

Browse files
author
Hongbo Zhang
committed
clean up and now provide getter/setter support on < > object type too
1 parent 77d348b commit 18eb14d

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

jscomp/syntax/ppx_entry.ml

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -199,41 +199,45 @@ let handle_typ
199199
ptyp_desc = Ptyp_object ( methods, closed_flag) ;
200200
ptyp_loc = loc
201201
} ->
202-
202+
let (+>) attr (typ : Parsetree.core_type) =
203+
{typ with ptyp_attributes = attr :: typ.ptyp_attributes} in
203204
let methods =
204205
List.fold_right (fun (label, ptyp_attrs, core_type) acc ->
205-
let (label,ptyp_attrs, core_type) =
206-
(match Ast_attributes.process_attributes_rev ptyp_attrs with
207-
| `Nothing, _ ->
208-
label, ptyp_attrs , self.typ self core_type
209-
| `Uncurry, ptyp_attrs ->
210-
label , ptyp_attrs,
211-
self.typ self
212-
{ core_type with
213-
ptyp_attributes =
214-
Ast_attributes.bs :: core_type.ptyp_attributes}
215-
| `Method, ptyp_attrs
216-
->
217-
label , ptyp_attrs,
218-
self.typ self
219-
{ core_type with
220-
ptyp_attributes =
221-
Ast_attributes.bs_method :: core_type.ptyp_attributes}
222-
| `Meth_callback, ptyp_attrs
223-
->
224-
label , ptyp_attrs,
225-
self.typ self
226-
{ core_type with
227-
ptyp_attributes =
228-
Ast_attributes.bs_this :: core_type.ptyp_attributes}) in
229206
let get ty name attrs =
230-
name , attrs, ty in
207+
let attrs, core_type =
208+
match Ast_attributes.process_attributes_rev attrs with
209+
| `Nothing, attrs -> attrs, core_type
210+
| `Uncurry, attrs ->
211+
attrs, Ast_attributes.bs +> ty
212+
| `Method, _
213+
-> Location.raise_errorf "bs.get/set conflicts with bs.meth"
214+
| `Meth_callback, attrs ->
215+
attrs, Ast_attributes.bs_this +> ty
216+
in
217+
name , attrs, self.typ self core_type in
231218
let set ty name attrs =
232-
name, attrs,
233-
Ast_util.to_method_type loc self "" ty
234-
(Ast_literal.type_unit ~loc ()) in
219+
let attrs, core_type =
220+
match Ast_attributes.process_attributes_rev attrs with
221+
| `Nothing, attrs -> attrs, core_type
222+
| `Uncurry, attrs ->
223+
attrs, Ast_attributes.bs +> ty
224+
| `Method, _
225+
-> Location.raise_errorf "bs.get/set conflicts with bs.meth"
226+
| `Meth_callback, attrs ->
227+
attrs, Ast_attributes.bs_this +> ty
228+
in
229+
name, attrs, Ast_util.to_method_type loc self "" core_type (Ast_literal.type_unit ~loc ()) in
235230
let no ty =
236-
label, ptyp_attrs, ty in
231+
let attrs, core_type =
232+
match Ast_attributes.process_attributes_rev ptyp_attrs with
233+
| `Nothing, attrs -> attrs, ty
234+
| `Uncurry, attrs ->
235+
attrs, Ast_attributes.bs +> ty
236+
| `Method, attrs ->
237+
attrs, Ast_attributes.bs_method +> ty
238+
| `Meth_callback, attrs ->
239+
attrs, Ast_attributes.bs_this +> ty in
240+
label, ptyp_attrs, self.typ self core_type in
237241
process_getter_setter ~no ~get ~set
238242
loc label ptyp_attrs core_type acc
239243
) methods [] in

jscomp/test/mutable_obj_test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33

44
function f(x) {
5-
return x.height = 3;
5+
return x.dec = function (x) {
6+
return {
7+
x: x,
8+
y: x
9+
};
10+
};
611
}
712

813
exports.f = f;

jscomp/test/mutable_obj_test.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ let f (x : u) =
1515
let f ( x : < height : int [@bs.set{no_get}] > Js.t) =
1616
x##height#=3
1717

18+
19+
20+
type v =
21+
[%bs.obj: < dec : int -> < x : int ; y : float > [@bs] [@bs.set] > ]
22+
23+
let f (x : v ) =
24+
x##dec#= (fun [@bs] x -> [%bs.obj {x ; y = float_of_int x }])

0 commit comments

Comments
 (0)