forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast_derive_js_mapper.ml
504 lines (475 loc) · 19.9 KB
/
ast_derive_js_mapper.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
(* Copyright (C) 2017 Authors of BuckleScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
open Ast_helper
module U = Ast_derive_util
type tdcls = Parsetree.type_declaration list
let js_field (o : Parsetree.expression) m =
Ast_compatible.app2
(Exp.ident {txt = Lident "##"; loc = o.pexp_loc})
o
(Exp.ident m)
let handle_config (config : Parsetree.expression option) =
match config with
| Some config ->
(match config.pexp_desc with
| Pexp_record (
[
{txt = Lident "newType"},
{pexp_desc =
(Pexp_construct
(
{txt =
Lident ("true"
| "false"
as x)}, None)
| Pexp_ident {txt = Lident ("newType" as x)}
)
}
],None)
-> not (x = "false")
| Pexp_ident {txt = Lident ("newType")}
-> true
| _ -> U.invalid_config config)
| None -> false
let noloc = Location.none
(* [eraseType] will be instrumented, be careful about the name conflict*)
let eraseTypeLit = "jsMapperEraseType"
let eraseTypeExp = Exp.ident {loc = noloc; txt = Lident eraseTypeLit}
let eraseType x =
Ast_compatible.app1 eraseTypeExp x
let eraseTypeStr =
let any = Typ.any () in
Str.primitive
(Val.mk ~prim:["%identity"] {loc = noloc; txt = eraseTypeLit}
(Ast_compatible.arrow any any)
)
let app2 = Ast_compatible.app2
let app3 = Ast_compatible.app3
let (<=~) a b =
app2 (Exp.ident {loc = noloc; txt = Lident "<="}) a b
let (-~) a b =
app2 (Exp.ident {loc = noloc; txt = Ldot(Lident "Pervasives","-")})
a b
let (+~) a b =
app2 (Exp.ident {loc = noloc; txt = Ldot(Lident "Pervasives","+")})
a b
let (&&~) a b =
app2 (Exp.ident {loc = noloc; txt = Ldot(Lident "Pervasives","&&")})
a b
let (->~) a b = Ast_compatible.arrow a b
let jsMapperRt =
Longident.Ldot (Lident "Js", "MapperRt")
let search upper polyvar array =
app3
(Exp.ident ({loc = noloc;
txt = Longident.Ldot (jsMapperRt,"binarySearch") })
)
upper
(eraseType polyvar)
array
let revSearch len constantArray exp =
app3
(Exp.ident
{loc= noloc;
txt = Longident.Ldot (jsMapperRt, "revSearch")})
len
constantArray
exp
let revSearchAssert len constantArray exp =
app3
(Exp.ident
{loc= noloc;
txt = Longident.Ldot (jsMapperRt, "revSearchAssert")})
len
constantArray
exp
let toInt exp array =
app2
(Exp.ident
{ loc=noloc;
txt = Longident.Ldot (jsMapperRt, "toInt")})
(eraseType exp)
array
let fromInt len array exp =
app3
(Exp.ident
{loc = noloc;
txt = Longident.Ldot (jsMapperRt,"fromInt")})
len
array
exp
let fromIntAssert len array exp =
app3
(Exp.ident
{loc = noloc;
txt = Longident.Ldot (jsMapperRt,"fromIntAssert")})
len
array
exp
let assertExp e =
Exp.extension
({Asttypes.loc = noloc; txt = "assert"},
(PStr
[Str.eval e ]
)
)
let derivingName = "jsConverter"
(* let notApplicable loc =
Location.prerr_warning
loc
(Warnings.Bs_derive_warning ( derivingName ^ " not applicable to this type")) *)
let init () =
Ast_derive.register
derivingName
(fun ( x : Parsetree.expression option) ->
let createType = handle_config x in
{
structure_gen = (fun (tdcls : tdcls) _ ->
let handle_tdcl (tdcl: Parsetree.type_declaration) =
let core_type = U.core_type_of_type_declaration tdcl
in
let name = tdcl.ptype_name.txt in
let toJs = name ^ "ToJs" in
let fromJs = name ^ "FromJs" in
let constantArray = "jsMapperConstantArray" in
let loc = tdcl.ptype_loc in
let patToJs = {Asttypes.loc; txt = toJs} in
let patFromJs = {Asttypes.loc; txt = fromJs} in
let param = "param" in
let ident_param = {Asttypes.txt = Longident.Lident param; loc} in
let pat_param = {Asttypes.loc; txt = param} in
let exp_param = Exp.ident ident_param in
let newType,newTdcl =
U.new_type_of_type_declaration tdcl ("abs_" ^ name) in
let newTypeStr = Ast_compatible.rec_type_str [newTdcl] in
let toJsBody body =
Ast_comb.single_non_rec_value patToJs
(Ast_compatible.fun_ (Pat.constraint_ (Pat.var pat_param) core_type)
body )
in
let (+>) a ty =
Exp.constraint_ (eraseType a) ty in
let (+:) a ty =
eraseType (Exp.constraint_ a ty) in
let coerceResultToNewType e =
if createType then
e +> newType
else e
in
match tdcl.ptype_kind with
| Ptype_record label_declarations ->
let exp =
coerceResultToNewType
(Exp.extension
(
{Asttypes.loc; txt = "bs.obj"},
(PStr
[Str.eval
(Exp.record
(List.map
(fun ({pld_name = {loc; txt } } : Parsetree.label_declaration) ->
let label =
{Asttypes.loc; txt = Longident.Lident txt } in
label,Exp.field exp_param label
) label_declarations) None)]))) in
let toJs =
toJsBody exp
in
let obj_exp =
Exp.record
(List.map
(fun ({pld_name = {loc; txt } } : Parsetree.label_declaration) ->
let label =
{Asttypes.loc; txt = Longident.Lident txt } in
label,
js_field exp_param label
) label_declarations) None in
let fromJs =
Ast_comb.single_non_rec_value patFromJs
(Ast_compatible.fun_ (Pat.var pat_param)
(if createType then
(Exp.let_ Nonrecursive
[Vb.mk
(Pat.var pat_param)
(exp_param +: newType)]
(Exp.constraint_ obj_exp core_type) )
else
(Exp.constraint_ obj_exp core_type) ))
in
let rest =
[
toJs;
fromJs
] in
if createType then eraseTypeStr:: newTypeStr :: rest else rest
| Ptype_abstract ->
(match Ast_polyvar.is_enum_polyvar tdcl with
| Some row_fields ->
let attr =
Ast_polyvar.map_row_fields_into_strings loc row_fields
in
let expConstantArray =
Exp.ident {loc; txt = Longident.Lident constantArray} in
begin match attr with
| NullString result ->
let result_len = List.length result in
let exp_len = Ast_compatible.const_exp_int result_len in
let v = [
eraseTypeStr;
Ast_comb.single_non_rec_value
{loc; txt = constantArray}
(Exp.array
(List.map (fun (i,str) ->
Exp.tuple
[
Ast_compatible.const_exp_int i;
Ast_compatible.const_exp_string str
]
) (List.sort (fun (a,_) (b,_) -> compare (a:int) b) result)));
(
toJsBody
(coerceResultToNewType
(search
exp_len
exp_param
expConstantArray
))
);
Ast_comb.single_non_rec_value
patFromJs
(Ast_compatible.fun_
(Pat.var pat_param)
(if createType then
revSearchAssert
exp_len
expConstantArray
(exp_param +: newType)
+>
core_type
else
revSearch
exp_len
expConstantArray
exp_param
+>
Ast_core_type.lift_option_type core_type
)
)
] in
if createType then
newTypeStr :: v
else v
| _ -> assert false
end
| None ->
U.notApplicable
tdcl.Parsetree.ptype_loc
derivingName;
[]
)
| Ptype_variant ctors ->
if Ast_polyvar.is_enum_constructors ctors then
let xs = Ast_polyvar.map_constructor_declarations_into_ints ctors in
match xs with
| `New xs ->
let constantArrayExp = Exp.ident {loc; txt = Lident constantArray} in
let exp_len = Ast_compatible.const_exp_int (List.length ctors) in
let v = [
eraseTypeStr;
Ast_comb.single_non_rec_value
{loc; txt = constantArray}
(Ast_compatible.const_exp_int_list_as_array xs)
(* (Exp.array (List.map (fun i -> Ast_compatible.const_exp_int i) xs )) *)
;
toJsBody
(
coerceResultToNewType @@
toInt
exp_param
constantArrayExp
)
;
Ast_comb.single_non_rec_value
patFromJs
(Ast_compatible.fun_
(Pat.var pat_param)
(
if createType then
fromIntAssert
exp_len
constantArrayExp
(exp_param +: newType)
+>
core_type
else
fromInt
exp_len
constantArrayExp
exp_param
+>
Ast_core_type.lift_option_type core_type
)
)
] in
if createType then newTypeStr :: v else v
| `Offset offset ->
let v =
[ eraseTypeStr;
toJsBody (
coerceResultToNewType
(eraseType exp_param +~ Ast_compatible.const_exp_int offset)
)
;
let len = List.length ctors in
let range_low = Ast_compatible.const_exp_int (offset + 0) in
let range_upper = Ast_compatible.const_exp_int (offset + len - 1) in
Ast_comb.single_non_rec_value
{loc ; txt = fromJs}
(Ast_compatible.fun_
(Pat.var pat_param)
(if createType then
(Exp.let_ Nonrecursive
[Vb.mk
(Pat.var pat_param)
(exp_param +: newType)
]
(
Exp.sequence
(assertExp
((exp_param <=~ range_upper) &&~ (range_low <=~ exp_param))
)
(exp_param -~ Ast_compatible.const_exp_int offset))
)
+>
core_type
else
(Exp.ifthenelse
( (exp_param <=~ range_upper) &&~ (range_low <=~ exp_param))
(Exp.construct {loc; txt = Lident "Some"}
( Some (exp_param -~ Ast_compatible.const_exp_int offset)))
(Some (Exp.construct {loc; txt = Lident "None"} None)))
+>
Ast_core_type.lift_option_type core_type
)
)
] in
if createType then newTypeStr :: v else v
else
begin
U.notApplicable
tdcl.Parsetree.ptype_loc
derivingName;
[]
end
| Ptype_open ->
U.notApplicable tdcl.Parsetree.ptype_loc
derivingName;
[] in
Ext_list.flat_map handle_tdcl tdcls
);
signature_gen =
(fun (tdcls : tdcls) _ ->
let handle_tdcl tdcl =
let core_type = U.core_type_of_type_declaration tdcl
in
let name = tdcl.ptype_name.txt in
let toJs = name ^ "ToJs" in
let fromJs = name ^ "FromJs" in
let loc = tdcl.ptype_loc in
let patToJs = {Asttypes.loc; txt = toJs} in
let patFromJs = {Asttypes.loc; txt = fromJs} in
let toJsType result =
Ast_comb.single_non_rec_val patToJs (Ast_compatible.arrow core_type result) in
let newType,newTdcl =
U.new_type_of_type_declaration tdcl ("abs_" ^ name) in
let newTypeStr = Ast_compatible.rec_type_sig [newTdcl] in
let (+?) v rest = if createType then v :: rest else rest in
match tdcl.ptype_kind with
| Ptype_record label_declarations ->
let objType flag =
Ast_comb.to_js_type loc @@
Ast_compatible.object_
(List.map
(fun ({pld_name = {loc; txt }; pld_type } : Parsetree.label_declaration) ->
txt, [], pld_type
) label_declarations)
flag in
newTypeStr +?
[
toJsType (if createType then newType else objType Closed);
Ast_comb.single_non_rec_val patFromJs
( (if createType then newType else objType Open)->~ core_type)
]
| Ptype_abstract ->
(match Ast_polyvar.is_enum_polyvar tdcl with
| Some _ ->
let ty1 =
if createType then newType else
(Ast_literal.type_string ()) in
let ty2 =
if createType then core_type
else Ast_core_type.lift_option_type core_type in
newTypeStr +?
[
toJsType ty1;
Ast_comb.single_non_rec_val
patFromJs
(ty1 ->~ ty2)
]
| None ->
U.notApplicable tdcl.Parsetree.ptype_loc
derivingName;
[])
| Ptype_variant ctors
->
if Ast_polyvar.is_enum_constructors ctors then
let ty1 =
if createType then newType
else Ast_literal.type_int() in
let ty2 =
if createType then core_type
else Ast_core_type.lift_option_type core_type in
newTypeStr +?
[
toJsType ty1;
Ast_comb.single_non_rec_val
patFromJs
(ty1 ->~ ty2)
]
else
begin
U.notApplicable tdcl.Parsetree.ptype_loc
derivingName;
[]
end
| Ptype_open ->
U.notApplicable tdcl.Parsetree.ptype_loc
derivingName;
[] in
Ext_list.flat_map handle_tdcl tdcls
);
expression_gen = None
}
)
;