Source file ast_util.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
open Import
open Ast_helper
let js_property loc obj (name : string) =
Pexp_send
( [%expr [%e Exp.ident { txt = Ast_literal.unsafe_downgrade; loc }] [%e obj]],
{ loc; txt = name } )
let ocaml_obj_as_js_object loc (mapper : Ast_traverse.map) (self_pat : pattern)
(clfs : class_field list) =
let generate_val_method_pair loc (mapper : Ast_traverse.map)
(val_name : string Asttypes.loc) is_mutable =
let result = Typ.var ~loc val_name.txt in
( result,
Of.tag val_name result
::
(if is_mutable then
[
Of.tag
{
val_name with
txt =
val_name.txt
^ Melange_ffi.External_ffi_types.Literals.setter_suffix;
}
(Ast_typ_uncurry.to_method_type loc mapper Nolabel result
[%type: unit]);
]
else []) )
in
let ( (internal_label_attr_types : object_field list),
(public_label_attr_types : object_field list) ) =
List.fold_right
~f:(fun
({ pcf_loc = loc; _ } as x : class_field)
(label_attr_types, public_label_attr_types)
->
match x.pcf_desc with
| Pcf_method (label, public_flag, Cfk_concrete (Fresh, e)) -> (
match e.pexp_desc with
| Pexp_poly ({ pexp_desc = Pexp_fun (lbl, _, pat, e); _ }, None) ->
let method_type =
Ast_typ_uncurry.generate_arg_type x.pcf_loc mapper label.txt
lbl pat e
in
( Of.tag label method_type :: label_attr_types,
if public_flag = Public then
Of.tag label method_type :: public_label_attr_types
else public_label_attr_types )
| Pexp_poly (_, Some _) ->
Location.raise_errorf ~loc
"polymorphic type annotation not supported yet"
| Pexp_poly (_, None) ->
Location.raise_errorf ~loc
"Unsupported JS Object syntax. Methods must take at least \
one argument"
| _ -> Location.raise_errorf ~loc "Unsupported syntax in js object")
| Pcf_val (label, mutable_flag, Cfk_concrete (Fresh, _)) ->
let _, label_attr =
generate_val_method_pair x.pcf_loc mapper label
(mutable_flag = Mutable)
in
(List.append label_attr label_attr_types, public_label_attr_types)
| Pcf_val (_, _, Cfk_concrete (Override, _)) ->
Location.raise_errorf ~loc "override flag not support currently"
| Pcf_val (_, _, Cfk_virtual _) ->
Location.raise_errorf ~loc "virtual flag not support currently"
| Pcf_method (_, _, Cfk_concrete (Override, _)) ->
Location.raise_errorf ~loc "override flag not supported"
| Pcf_method (_, _, Cfk_virtual _) ->
Location.raise_errorf ~loc "virtural method not supported"
| Pcf_inherit _ | Pcf_initializer _ | Pcf_attribute _ | Pcf_extension _
| Pcf_constraint _ ->
Location.raise_errorf ~loc "Only method support currently")
clfs ~init:([], [])
in
let internal_obj_type =
Ast_core_type.make_obj ~loc internal_label_attr_types
in
let public_obj_type =
Ast_core_type.to_js_type ~loc
(Typ.object_ ~loc public_label_attr_types Closed)
in
let labels, label_types, exprs, _ =
List.fold_right
~f:(fun (x : class_field) (labels, label_types, exprs, aliased) ->
match x.pcf_desc with
| Pcf_method (label, _public_flag, Cfk_concrete (Fresh, e)) -> (
match e.pexp_desc with
| Pexp_poly
(({ pexp_desc = Pexp_fun (ll, None, pat, e); _ } as f), None) ->
let alias_type =
if aliased then None else Some internal_obj_type
in
let label_type =
Ast_typ_uncurry.generate_method_type ?alias_type x.pcf_loc
mapper label.txt ll pat e
in
( label :: labels,
label_type :: label_types,
{
f with
pexp_desc =
(let f = Ast_pat.is_unit_cont pat ~yes:e ~no:f in
Ast_uncurry_gen.to_method_callback loc mapper Nolabel
self_pat f)
;
}
:: exprs,
true )
| Pexp_poly (_, Some _) ->
Location.raise_errorf ~loc
"polymorphic type annotation not supported yet"
| Pexp_poly (_, None) ->
Location.raise_errorf ~loc
"Unsupported syntax, expect syntax like `method x () = x ` "
| _ -> Location.raise_errorf ~loc "Unsupported syntax in js object")
| Pcf_val (label, mutable_flag, Cfk_concrete (Fresh, val_exp)) ->
let label_type, _ =
generate_val_method_pair x.pcf_loc mapper label
(mutable_flag = Mutable)
in
( label :: labels,
label_type :: label_types,
mapper#expression val_exp :: exprs,
aliased )
| Pcf_val (_, _, Cfk_concrete (Override, _)) ->
Location.raise_errorf ~loc "override flag not support currently"
| Pcf_val (_, _, Cfk_virtual _) ->
Location.raise_errorf ~loc "virtual flag not support currently"
| Pcf_method (_, _, Cfk_concrete (Override, _)) ->
Location.raise_errorf ~loc "override flag not supported"
| Pcf_method (_, _, Cfk_virtual _) ->
Location.raise_errorf ~loc "virtural method not supported"
| Pcf_inherit _ | Pcf_initializer _ | Pcf_attribute _ | Pcf_extension _
| Pcf_constraint _ ->
Location.raise_errorf ~loc "Only method support currently")
clfs ~init:([], [], [], false)
in
let pval_type =
List.fold_right2
~f:(fun label label_type acc ->
Typ.arrow ~loc:label.Asttypes.loc (Labelled label.Asttypes.txt)
label_type acc)
labels label_types ~init:public_obj_type
in
Ast_external_mk.local_extern_cont_to_obj loc
~pval_prim:(Ast_external_mk.pval_prim_of_labels labels)
(fun e ->
Exp.apply ~loc e
(List.map2 ~f:(fun l expr -> (Labelled l.txt, expr)) labels exprs))
~pval_type