Skip to content

Commit cbaade4

Browse files
author
Hongbo Zhang
committed
getter/setter also work on object type now
1 parent 2f9119c commit cbaade4

File tree

3 files changed

+53
-46
lines changed

3 files changed

+53
-46
lines changed

jscomp/syntax/ppx_entry.ml

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -202,33 +202,46 @@ let handle_typ
202202

203203
let check_auto_uncurry core_type = self.typ self core_type in
204204
let methods =
205-
List.map (fun (label, ptyp_attrs, core_type ) ->
206-
match Ast_attributes.process_attributes_rev ptyp_attrs with
207-
| `Nothing, _ ->
208-
label, ptyp_attrs , check_auto_uncurry core_type
209-
| `Uncurry, ptyp_attrs ->
210-
label , ptyp_attrs,
211-
check_auto_uncurry
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-
check_auto_uncurry
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-
check_auto_uncurry
226-
{ core_type with
227-
ptyp_attributes =
228-
Ast_attributes.bs_this :: core_type.ptyp_attributes}
229-
) methods
230-
231-
in
205+
methods
206+
|> List.map (fun (label, ptyp_attrs, core_type ) ->
207+
(match Ast_attributes.process_attributes_rev ptyp_attrs with
208+
| `Nothing, _ ->
209+
label, ptyp_attrs , check_auto_uncurry core_type
210+
| `Uncurry, ptyp_attrs ->
211+
label , ptyp_attrs,
212+
check_auto_uncurry
213+
{ core_type with
214+
ptyp_attributes =
215+
Ast_attributes.bs :: core_type.ptyp_attributes}
216+
| `Method, ptyp_attrs
217+
->
218+
label , ptyp_attrs,
219+
check_auto_uncurry
220+
{ core_type with
221+
ptyp_attributes =
222+
Ast_attributes.bs_method :: core_type.ptyp_attributes}
223+
| `Meth_callback, ptyp_attrs
224+
->
225+
label , ptyp_attrs,
226+
check_auto_uncurry
227+
{ core_type with
228+
ptyp_attributes =
229+
Ast_attributes.bs_this :: core_type.ptyp_attributes})
230+
)
231+
in
232+
let methods =
233+
List.fold_right (fun (label, ptyp_attrs, core_type) acc ->
234+
let get ty name attrs =
235+
name , attrs, ty in
236+
let set ty name attrs =
237+
name, attrs,
238+
Ast_util.to_method_type loc self "" ty
239+
(Ast_literal.type_unit ~loc ()) in
240+
let no ty =
241+
label, ptyp_attrs, ty in
242+
process_getter_setter ~no ~get ~set
243+
loc label ptyp_attrs core_type acc
244+
) methods [] in
232245
let inner_type =
233246
{ ty
234247
with ptyp_desc = Ptyp_object(methods, closed_flag);

jscomp/test/mutable_obj_test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict';
22

33

4+
function f(x) {
5+
return x.height = 3;
6+
}
47

8+
exports.f = f;
59
/* No side effect */

jscomp/test/mutable_obj_test.ml

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,12 @@ type u =
66
< height : int [@bs.set] > Js.t
77

88

9-
class type v = object
10-
method height : int
11-
end [@bs]
12-
13-
module type X = sig
14-
class xx : int -> object
15-
method height : int
16-
end [@bs] (* should give warning *)
17-
(* class type xx : int -> object *)
18-
(* method height : int *)
19-
(* end [@bs] syntax error *)
20-
21-
end
22-
23-
24-
(* let u x = *)
25-
(* [%bs.obj{ *)
26-
(* x with y = 3 *)
27-
(* }] *)
9+
10+
11+
let f (x : u) =
12+
x##height#= 3 ;
13+
x##height * 2
14+
15+
let f ( x : < height : int [@bs.set{no_get}] > Js.t) =
16+
x##height#=3
17+

0 commit comments

Comments
 (0)