File: ppx.mlt

package info (click to toggle)
js-of-ocaml 6.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,932 kB
  • sloc: ml: 135,957; javascript: 58,364; ansic: 437; makefile: 422; sh: 12; perl: 4
file content (690 lines) | stat: -rw-r--r-- 14,609 bytes parent folder | download
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
module Js_of_ocaml = struct
  module Js = struct

    class type js_string = object end

    type +'a t

    type (-'a, +'b) meth_callback

    type +'a opt

    type +'a optdef

    type +'a meth

    type +'a gen_prop

    type 'a readonly_prop = < get : 'a > gen_prop

    type 'a writeonly_prop = < set : 'a -> unit > gen_prop

    type 'a prop = < get : 'a ; set : 'a -> unit > gen_prop

    type 'a optdef_prop = < get : 'a optdef ; set : 'a -> unit > gen_prop

    type +'a constr

    (****)

    type 'a callback = (unit, 'a) meth_callback

    module Unsafe = struct
      type any

      type any_js_array = any

      let inject : 'a -> any = fun _ -> assert false

      let get : 'a -> 'b -> 'c = fun _ _ -> assert false

      let set : 'a -> 'b -> 'c -> unit = fun _ _ _ -> assert false

      let meth_call : 'a -> string -> any array -> 'b = fun _ _ _ -> assert false

      let obj : (string * any) array -> 'a = fun _ -> assert false
    end

    let wrap_meth_callback : ('a -> 'b) -> ('a, 'b) meth_callback = fun _ -> assert false

    let string : string -> js_string t = fun _ -> assert false

    let undefined : unit -> 'a optdef = fun () -> assert false

    let def : 'a -> 'a optdef = fun _ -> assert false
  end
end

open Js_of_ocaml

[%%expect
{|
module Js_of_ocaml :
  sig
    module Js :
      sig
        class type js_string = object  end
        type +'a t
        type (-'a, +'b) meth_callback
        type +'a opt
        type +'a optdef
        type +'a meth
        type +'a gen_prop
        type 'a readonly_prop = < get : 'a > gen_prop
        type 'a writeonly_prop = < set : 'a -> unit > gen_prop
        type 'a prop = < get : 'a; set : 'a -> unit > gen_prop
        type 'a optdef_prop = < get : 'a optdef; set : 'a -> unit > gen_prop
        type +'a constr
        type 'a callback = (unit, 'a) meth_callback
        module Unsafe :
          sig
            type any
            type any_js_array = any
            val inject : 'a -> any
            val get : 'a -> 'b -> 'c
            val set : 'a -> 'b -> 'c -> unit
            val meth_call : 'a -> string -> any array -> 'b
            val obj : (string * any) array -> 'a
          end
        val wrap_meth_callback : ('a -> 'b) -> ('a, 'b) meth_callback
        val string : string -> js_string t
        val undefined : unit -> 'a optdef
        val def : 'a -> 'a optdef
      end
  end
|}]

(* Test methods. *)

;;
fun (obj : int) -> obj##m

[%%expect
{|
Line _, characters 19-22:
Error: The value obj has type int but an expression was expected of type
         < .. > Js_of_ocaml.Js.t
|}]

;;
fun (obj : < > Js.t) -> obj##m

[%%expect
{|
Line _, characters 24-27:
Error: This expression has type <  >
       It has no method m
|}]

;;
fun (obj : < m : float Js.prop > Js.t) -> obj##m

[%%expect
{|
Line _, characters 42-48:
Error: The method call x#m has type
         float Js_of_ocaml.Js.prop =
           < get : float; set : float -> unit > Js_of_ocaml.Js.gen_prop
       but an expression was expected of type 'res Js_of_ocaml.Js.meth
|}]

;;
fun (obj : < m : float Js.meth > Js.t) -> obj##m + 1

[%%expect
{|
Line _, characters 42-48:
Error: This expression has type float but an expression was expected of type
         int
|}]

;;
fun (obj : < m : int -> int Js.meth > Js.t) -> obj##m + 1

[%%expect
{|
Line _, characters 47-53:
Error: The method call x#m has type int -> int Js_of_ocaml.Js.meth
       but an expression was expected of type 'res Js_of_ocaml.Js.meth
|}]

;;
fun (obj : < m : int Js.meth > Js.t) -> obj##m 1

[%%expect
{|
Line _, characters 40-46:
Error: The method call x#m has type int Js_of_ocaml.Js.meth
       but an expression was expected of type int -> 'res Js_of_ocaml.Js.meth
|}]

;;
fun (obj : < m : int -> float Js.meth > Js.t) -> obj##m 1 + 1

[%%expect
{|
Line _, characters 49-57:
Error: This expression has type float but an expression was expected of type
         int
|}]

;;
fun (obj : < m : 'a. 'a -> unit Js.meth > Js.t) ->
  obj##m "string";
  obj##m ();
  obj##w

[%%expect
{|
Line _, characters 2-5:
Error: This expression has type < m : 'a. 'a -> unit Js_of_ocaml.Js.meth >
       It has no method w
|}]

(* Test prop reading. *)

;;
fun (obj : int) -> obj##.p

[%%expect
{|
Line _, characters 19-22:
Error: The value obj has type int but an expression was expected of type
         < .. > Js_of_ocaml.Js.t
|}]

;;
fun (obj : < > Js.t) -> obj##.p

[%%expect
{|
Line _, characters 24-27:
Error: This expression has type <  >
       It has no method p
|}]

;;
fun (obj : < p : float Js.writeonly_prop > Js.t) -> obj##.p + 1

[%%expect
{|
Line _, characters 58-59:
Error: The method call x#p has type
         float Js_of_ocaml.Js.writeonly_prop =
           < set : float -> unit > Js_of_ocaml.Js.gen_prop
       but an expression was expected of type
         < get : 'res; .. > Js_of_ocaml.Js.gen_prop
       The first object type has no method get
|}]

;;
fun (obj : < p : float Js.prop > Js.t) -> obj##.p + 1

[%%expect
{|
Line _, characters 42-49:
Error: This expression has type float but an expression was expected of type
         int
|}]

(* Test prop writing. *)

;;
fun (obj : int) -> obj##.p := 2

[%%expect
{|
Line _, characters 19-22:
Error: The value obj has type int but an expression was expected of type
         < .. > Js_of_ocaml.Js.t
|}]

;;
fun (obj : < > Js.t) -> obj##.p := 2

[%%expect
{|
Line _, characters 24-27:
Error: This expression has type <  >
       It has no method p
|}]

;;
fun (obj : < p : float Js.readonly_prop > Js.t) -> obj##.p := 1

[%%expect
{|
Line _, characters 51-58:
Error: The method call x#p has type
         float Js_of_ocaml.Js.readonly_prop =
           < get : float > Js_of_ocaml.Js.gen_prop
       but an expression was expected of type
         < set : int -> unit; .. > Js_of_ocaml.Js.gen_prop
       The first object type has no method set
|}]

;;
fun (obj : < p : float Js.prop > Js.t) -> obj##.p := 1

[%%expect
{|
Line _, characters 42-49:
Error: The method call x#p has type
         float Js_of_ocaml.Js.prop =
           < get : float; set : float -> unit > Js_of_ocaml.Js.gen_prop
       but an expression was expected of type
         < set : int -> unit; .. > Js_of_ocaml.Js.gen_prop
       The method set has type float -> unit,
       but the expected method type was int -> unit
|}]

;;
fun (obj : < p : int Js.prop > Js.t) -> (obj##.p := 1) + 1

[%%expect
{|
Line _, characters 40-54:
Error: This expression has type unit but an expression was expected of type
         int
|}]

(* Test objects. *)

;;
fun (obj : < > Js.t) ->
  obj
  = object%js
      val m = ()
    end

[%%expect
{|
Line _, characters 4-38:
Error: This expression has type
         < m : unit Js_of_ocaml.Js.readonly_prop > Js_of_ocaml.Js.t
       but an expression was expected of type <  > Js_of_ocaml.Js.t
       The second object type has no method m
|}]

;;
fun (obj : < m : float Js.prop > Js.t) ->
  obj
  = object%js
      val mutable m = 0
    end

[%%expect
{|
Line _, characters 4-45:
Error: This expression has type
         < m : int Js_of_ocaml.Js.prop > Js_of_ocaml.Js.t
       but an expression was expected of type
         < m : float Js_of_ocaml.Js.prop > Js_of_ocaml.Js.t
       Type
         int Js_of_ocaml.Js.prop =
           < get : int; set : int -> unit > Js_of_ocaml.Js.gen_prop
       is not compatible with type
         float Js_of_ocaml.Js.prop =
           < get : float; set : float -> unit > Js_of_ocaml.Js.gen_prop
       The method get has type int, but the expected method type was float
|}]

;;
fun () ->
  (object%js
     val r = 2

     val mutable w = 3.

     method m = ""
   end
    : < m : int Js.meth ; .. > Js.t)

[%%expect
{|
Line _, characters 3-79:
Error: This expression has type
         < m : string Js_of_ocaml.Js.meth;
           r : int Js_of_ocaml.Js.readonly_prop;
           w : float Js_of_ocaml.Js.prop >
         Js_of_ocaml.Js.t
       but an expression was expected of type
         < m : int Js_of_ocaml.Js.meth; .. > Js_of_ocaml.Js.t
       The method m has type string Js_of_ocaml.Js.meth,
       but the expected method type was int Js_of_ocaml.Js.meth
|}]

;;
fun () ->
  object%js
    val r = 2

    val _r = 2
  end

[%%expect
{|
Line _, characters 8-10:
Error: Duplicated val or method "_r" (normalized to "r").
Line _, characters 8-9:
  Duplicated val or method "r".
|}]

;;
fun () ->
  (object%js
     val _r_a = 2

     val _r_b = 2
   end
    : < _r_a : int Js.readonly_prop > Js.t)

[%%expect
{|
Line _, characters 9-13:
Error: Duplicated val or method "_r_b" (normalized to "r").
Line _, characters 9-13:
  Duplicated val or method "_r_a" (normalized to "r").
|}]

;;
fun () ->
  object%js
    val mutable w = 2

    val mutable _w = 2
  end

[%%expect
{|
Line _, characters 16-18:
Error: Duplicated val or method "_w" (normalized to "w").
Line _, characters 16-17:
  Duplicated val or method "w".
|}]

;;
fun () ->
  (object%js
     val mutable _w_a = 2

     val mutable _w_b = 2
   end
    : < _r_a : int Js.prop > Js.t)

[%%expect
{|
Line _, characters 17-21:
Error: Duplicated val or method "_w_b" (normalized to "w").
Line _, characters 17-21:
  Duplicated val or method "_w_a" (normalized to "w").
|}]

;;
fun () ->
  object%js
    method m = ""

    method _m = ""
  end

[%%expect
{|
Line _, characters 11-13:
Error: Duplicated val or method "_m" (normalized to "m").
Line _, characters 11-12:
  Duplicated val or method "m".
|}]

;;
fun () ->
  (object%js
     method _m_a = ""

     method _m_bc = ""
   end
    : < _m_a : string Js.meth ; .. > Js.t)

[%%expect
{|
Line _, characters 12-17:
Error: Duplicated val or method "_m_bc" (normalized to "m").
Line _, characters 12-16:
  Duplicated val or method "_m_a" (normalized to "m").
|}]

;;
fun () ->
  (object%js
     val r = Js.def 2 [@@jsoo.optdef]

     val mutable w = 3.

     method m = ""
   end
    : < r : int Js.prop ; .. > Js.t)

[%%expect
{|
Line _, characters 3-102:
Error: This expression has type
         < m : string Js_of_ocaml.Js.meth;
           r : int Js_of_ocaml.Js.optdef_prop;
           w : float Js_of_ocaml.Js.prop >
         Js_of_ocaml.Js.t
       but an expression was expected of type
         < r : int Js_of_ocaml.Js.prop; .. > Js_of_ocaml.Js.t
       Type
         int Js_of_ocaml.Js.optdef_prop =
           < get : int Js_of_ocaml.Js.optdef; set : int -> unit >
           Js_of_ocaml.Js.gen_prop
       is not compatible with type
         int Js_of_ocaml.Js.prop =
           < get : int; set : int -> unit > Js_of_ocaml.Js.gen_prop
       The method get has type int Js_of_ocaml.Js.optdef,
       but the expected method type was int
|}]

;;
fun () ->
  (object%js
     val r = 2 [@@jsoo.writeonly]

     val mutable w = 3.

     method m = ""
   end
    : < r : int Js.prop ; .. > Js.t)

[%%expect
{|
Line _, characters 3-98:
Error: This expression has type
         < m : string Js_of_ocaml.Js.meth;
           r : int Js_of_ocaml.Js.writeonly_prop;
           w : float Js_of_ocaml.Js.prop >
         Js_of_ocaml.Js.t
       but an expression was expected of type
         < r : int Js_of_ocaml.Js.prop; .. > Js_of_ocaml.Js.t
       Type
         int Js_of_ocaml.Js.writeonly_prop =
           < set : int -> unit > Js_of_ocaml.Js.gen_prop
       is not compatible with type
         int Js_of_ocaml.Js.prop =
           < get : int; set : int -> unit > Js_of_ocaml.Js.gen_prop
       The first object type has no method get
|}]

;;
fun () ->
  (object%js
     val mutable r = 2 [@@jsoo.readonly]

     val mutable w = 3.

     method m = ""
   end
    : < r : int Js.prop ; .. > Js.t)

[%%expect {|
Line _, characters 5-40:
Error: A mutable field cannot be readonly.
|}]

;;
fun () ->
  (object%js
     val r = 2 [@@jsoo.readonly]

     val mutable w = 3.

     method m = ""
   end
    : < r : int Js.prop ; .. > Js.t)

[%%expect
{|
Line _, characters 3-97:
Error: This expression has type
         < m : string Js_of_ocaml.Js.meth;
           r : int Js_of_ocaml.Js.readonly_prop;
           w : float Js_of_ocaml.Js.prop >
         Js_of_ocaml.Js.t
       but an expression was expected of type
         < r : int Js_of_ocaml.Js.prop; .. > Js_of_ocaml.Js.t
       Type
         int Js_of_ocaml.Js.readonly_prop =
           < get : int > Js_of_ocaml.Js.gen_prop
       is not compatible with type
         int Js_of_ocaml.Js.prop =
           < get : int; set : int -> unit > Js_of_ocaml.Js.gen_prop
       The first object type has no method set
|}]

;;
fun () ->
  (object%js
     val mutable r = 2 [@@jsoo.readonly] [@@writeonly]

     val mutable w = 3.

     method m = ""
   end
    : < r : int Js.prop ; .. > Js.t)

[%%expect {|
Line _, characters 5-54:
Error: Too many attributes.
|}]

;;
fun () ->
  (object%js
     val mutable r = 2 [@@jsoo.wrong]

     val mutable w = 3.

     method m = ""
   end
    : < r : int Js.prop ; .. > Js.t)

[%%expect {|
Line _, characters 5-37:
Error: Unkown jsoo attribute ([@@jsoo.wrong]).
|}]

;;
fun () ->
  object%js
    val r = 2 [@@jsoo.readonly]
  end

[%%expect
{|
- : unit -> < r : int Js_of_ocaml.Js.readonly_prop > Js_of_ocaml.Js.t = <fun>
|}]

;;
fun () ->
  object%js
    val r = 2 [@@jsoo.writeonly]
  end

[%%expect
{|
- : unit -> < r : int Js_of_ocaml.Js.writeonly_prop > Js_of_ocaml.Js.t =
<fun>
|}]

;;
fun () ->
  object%js
    val r = 2 [@@jsoo.readwrite]
  end

[%%expect {|
- : unit -> < r : int Js_of_ocaml.Js.prop > Js_of_ocaml.Js.t = <fun>
|}]

;;
fun () ->
  object%js
    val r = Js.def 2 [@@jsoo.optdef]
  end

[%%expect
{|
- : unit -> < r : int Js_of_ocaml.Js.optdef_prop > Js_of_ocaml.Js.t = <fun>
|}]

;;
fun () ->
  object%js
    val r = Js.def 2 [@@jsoo.optdef]
  end

[%%expect
{|
- : unit -> < r : int Js_of_ocaml.Js.optdef_prop > Js_of_ocaml.Js.t = <fun>
|}]

;;
fun () ->
  object%js (self)
    (* Equivalent of this *)
    val x1 = 3

    (* read-only prop *)
    val x2 = 3 [@@readonly]

    (* read-only prop *)
    val mutable y1 = 4

    (* read/write prop *)
    val y2 = 4 [@@readwrite]

    (* read/write prop *)
    val z = 5 [@@writeonly]

    (* write-only prop *)
    val t1 = Js.def 6 [@@optdef]

    (* optdef prop *)
    val t2 = Js.undefined () [@@jsoo.optdef]

    (* optdef prop *)
    method foo i = self##.y1 := self##.x1 + i
  end

[%%expect
{|
- : unit ->
    < foo : int -> unit Js_of_ocaml.Js.meth;
      t1 : int Js_of_ocaml.Js.optdef_prop;
      t2 : 't57 Js_of_ocaml.Js.optdef_prop;
      x1 : int Js_of_ocaml.Js.readonly_prop;
      x2 : int Js_of_ocaml.Js.readonly_prop; y1 : int Js_of_ocaml.Js.prop;
      y2 : int Js_of_ocaml.Js.prop; z : int Js_of_ocaml.Js.writeonly_prop >
    Js_of_ocaml.Js.t
= <fun>
|}]