forked from links-lang/links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.ml
1806 lines (1483 loc) · 53.1 KB
/
lib.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
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*pp deriving *)
open List
open Value
open Types
open Utility
open Proc
(* Alias environment *)
module AliasEnv = Env.String
(* This is done in two stages because the datatype for regexes refers
to the String alias *)
let alias_env : Types.tycon_environment = DefaultAliases.alias_env
let alias_env : Types.tycon_environment =
AliasEnv.bind alias_env
("Regex", `Alias ([], (DesugarDatatypes.read ~aliases:alias_env Linksregex.Regex.datatype)))
let datatype = DesugarDatatypes.read ~aliases:alias_env
(*
assumption:
the only kind of lists that are allowed to be inserted into databases
are strings
*)
let value_as_string db =
function
| `String s -> "\'" ^ db # escape_string s ^ "\'"
| v -> string_of_value v
let row_columns = function
| `List ((`Record fields)::_) -> map fst fields
| r -> failwith ("Internal error: forming query from non-row (row_columns): "^string_of_value r)
and row_values db = function
| `List records ->
(List.map (function
| `Record fields -> map (value_as_string db -<- snd) fields
| _ -> failwith "Internal error: forming query from non-row") records)
| r -> failwith ("Internal error: forming query from non-row (row_values): "^string_of_value r)
(* and delete_condition db = function *)
(* | `List(rows) -> "("^ (String.concat " OR " (map (single_match db) rows)) ^")" *)
(* | r -> failwith ("Internal error: forming query from non-row (delete_condition): "^string_of_value r) *)
(* and updates db : Value.t -> string = function *)
(* | `Record fields -> *)
(* let field (k, v) = (k ^" = "^ value_as_string db v) in *)
(* (String.concat ", " (map field fields)) *)
(* | r -> failwith ("Internal error: forming query from non-row: "^string_of_value r) *)
type primitive =
[ t
| `PFun of RequestData.request_data -> Value.t list -> Value.t ]
type pure = PURE | IMPURE
type located_primitive = [ `Client | `Server of primitive | primitive ]
let mk_binop_fn impl unbox_fn constr = function
| [x; y] -> constr (impl (unbox_fn x) (unbox_fn y))
| _ -> failwith "arity error in integer operation"
let int_op impl pure : located_primitive * Types.datatype * pure =
(`PFun (fun _ -> mk_binop_fn impl unbox_int (fun x -> `Int x))),
datatype "(Int, Int) -> Int",
pure
let float_op impl pure : located_primitive * Types.datatype * pure =
(`PFun (fun _ -> mk_binop_fn impl unbox_float (fun x -> `Float x))),
datatype "(Float, Float) -> Float",
pure
let string_op impl pure : located_primitive * Types.datatype * pure =
(`PFun (fun _ -> mk_binop_fn impl unbox_string (fun x -> `String x))),
datatype "(String, String) -> String",
pure
let conversion_op' ~unbox ~conv ~(box :'a->Value.t): Value.t list -> Value.t = function
| [x] -> box (conv (unbox x))
| _ -> assert false
let conversion_op ~from ~unbox ~conv ~(box :'a->Value.t) ~into pure : located_primitive * Types.datatype * pure =
((`PFun (fun _ x -> conversion_op' ~unbox:unbox ~conv:conv ~box:box x) : located_primitive),
(let q, r = Types.fresh_row_quantifier (`Any, `Any) in
(`ForAll (Types.box_quantifiers [q], `Function (make_tuple_type [from], r, into)) : Types.datatype)),
pure)
let string_to_xml : Value.t -> Value.t = function
| `String s -> `List [`XML (Text s)]
| _ -> failwith "internal error: non-string value passed to xml conversion routine"
(* The following functions expect 1 argument. Assert false otherwise. *)
let char_test_op fn pure =
(`PFun (fun _ args ->
match args with
| [c] -> (`Bool (fn (unbox_char c)))
| _ -> assert false),
datatype "(Char) ~> Bool",
pure)
let char_conversion fn pure =
(`PFun (fun _ args ->
match args with
| [c] -> (box_char (fn (unbox_char c)))
| _ -> assert false),
datatype "(Char) -> Char",
pure)
let float_fn fn pure =
(`PFun (fun _ args ->
match args with
| [c] -> (box_float (fn (unbox_float c)))
| _ -> assert false),
datatype "(Float) -> Float",
pure)
(* Functions which also take the request data as an argument --
* for example those which set cookies, change the headers, etc. *)
let p1D fn =
`PFun (fun req_data args ->
match args with
| ([a]) -> fn a req_data
| _ -> assert false)
let p2D fn =
`PFun (fun req_data args ->
match args with
| [a; b] -> fn a b req_data
| _ -> assert false)
let p3D fn =
`PFun (fun req_data args ->
match args with
| [a;b;c] -> fn a b c req_data
| _ -> assert false)
let p1 fn = p1D (fun x _ -> fn x)
let p2 fn = p2D (fun x y _ -> fn x y)
let p3 fn = p3D (fun x y z _ -> fn x y z)
let rec equal l r =
match l, r with
| `Bool l , `Bool r -> l = r
| `Int l , `Int r -> l = r
| `Float l , `Float r -> l = r
| `Char l , `Char r -> l = r
| `String l, `String r -> l = r
| `Record lfields, `Record rfields ->
let rec one_equal_all = (fun alls (ref_label, ref_result) ->
match alls with
| [] -> false
| (label, result) :: _ when label = ref_label -> equal result ref_result
| _ :: alls -> one_equal_all alls (ref_label, ref_result)) in
List.for_all (one_equal_all rfields) lfields && List.for_all (one_equal_all lfields) rfields
| `Variant (llabel, lvalue), `Variant (rlabel, rvalue) -> llabel = rlabel && equal lvalue rvalue
| `List (l), `List (r) -> equal_lists l r
| l, r -> failwith ("Comparing "^ string_of_value l ^" with "^ string_of_value r ^" either doesn't make sense or isn't implemented")
and equal_lists l r =
match l,r with
| [], [] -> true
| (l::ls), (r::rs) -> equal l r && equal_lists ls rs
| _,_ -> false
let rec less l r =
match l, r with
| `Bool l, `Bool r -> l < r
| `Int l, `Int r -> l < r
| `Float l, `Float r -> l < r
| `Char l, `Char r -> l < r
| `String l, `String r -> l < r
(* Compare fields in lexicographic order of labels *)
| `Record lf, `Record rf ->
let order = sort (fun x y -> compare (fst x) (fst y)) in
let lv, rv = map snd (order lf), map snd (order rf) in
let rec compare_list = function
| [] -> false
| (l,r)::_ when less l r -> true
| (l,r)::_ when less r l -> false
| _::rest -> compare_list rest in
compare_list (combine lv rv)
| `List (l), `List (r) -> less_lists (l,r)
| l, r -> failwith ("Cannot yet compare "^ string_of_value l ^" with "^ string_of_value r)
and less_lists = function
| _, [] -> false
| [], (_::_) -> true
| (l::_), (r::_) when less l r -> true
| (l::_), (r::_) when less r l -> false
| (_::l), (_::r) -> less_lists (l, r)
let less_or_equal l r = less l r || equal l r
let add_attribute : Value.t * Value.t -> Value.t -> Value.t =
fun (name,value) -> function
| `XML (Node (tag, children)) ->
let name = unbox_string name
and value = unbox_string value in
let rec filter = function
| [] -> []
| Attr (s, _) :: nodes when s=name -> filter nodes
| node :: nodes -> node :: filter nodes
in
`XML (Node (tag, Attr (name, value) :: filter children))
| r -> failwith ("cannot add attribute to " ^ string_of_value r)
let add_attributes : (Value.t * Value.t) list -> Value.t -> Value.t =
List.fold_right add_attribute
let prelude_tyenv = ref None (* :-( *)
let prelude_nenv = ref None (* :-( *)
let env : (string * (located_primitive * Types.datatype * pure)) list ref = ref [
"+", int_op (+) PURE;
"-", int_op (-) PURE;
"*", int_op ( * ) PURE;
"/", int_op (/) IMPURE;
"^", int_op pow PURE;
"mod", int_op (mod) IMPURE;
"+.", float_op (+.) PURE;
"-.", float_op (-.) PURE;
"*.", float_op ( *.) PURE;
"/.", float_op (/.) PURE;
"^.", float_op ( ** ) PURE;
"^^", string_op ( ^ ) PURE;
(* Comparisons *)
"==",
(p2 (fun v1 v2 -> box_bool (equal v1 v2)),
datatype "(a,a) -> Bool",
PURE);
"<>",
(p2 (fun v1 v2 -> box_bool (not (equal v1 v2))),
datatype "(a,a) -> Bool",
PURE);
"<",
(p2 (fun v1 v2 -> box_bool (less v1 v2)),
datatype "(a,a) -> Bool",
PURE);
">",
(p2 (fun v1 v2 -> box_bool (less v2 v1)),
datatype "(a,a) -> Bool",
PURE);
"<=",
(p2 (fun v1 v2 -> box_bool (less_or_equal v1 v2)),
datatype "(a,a) -> Bool",
PURE);
">=",
(p2 (fun v1 v2 -> box_bool (less_or_equal v2 v1)),
datatype "(a,a) -> Bool",
PURE);
(* Conversions (any missing?) *)
"intToString", conversion_op ~from:(`Primitive `Int) ~unbox:unbox_int ~conv:string_of_int ~box:box_string ~into:Types.string_type PURE;
"stringToInt", conversion_op ~from:Types.string_type ~unbox:unbox_string ~conv:int_of_string ~box:box_int ~into:(`Primitive `Int) IMPURE;
"intToFloat", conversion_op ~from:(`Primitive `Int) ~unbox:unbox_int ~conv:float_of_int ~box:box_float ~into:(`Primitive `Float) PURE;
"floatToInt", conversion_op ~from:(`Primitive `Float) ~unbox:unbox_float ~conv:int_of_float ~box:box_int ~into:(`Primitive `Int) PURE;
"floatToString", conversion_op ~from:(`Primitive `Float) ~unbox:unbox_float ~conv:string_of_float' ~box:box_string ~into:Types.string_type PURE;
"stringToFloat", conversion_op ~from:Types.string_type ~unbox:unbox_string ~conv:float_of_string ~box:box_float ~into:(`Primitive `Float) IMPURE;
"stringToXml",
((p1 string_to_xml),
datatype "(String) -> Xml",
PURE);
"intToXml",
(`PFun (fun _ ->
string_to_xml -<- (conversion_op' ~unbox:unbox_int ~conv:(string_of_int) ~box:box_string)),
datatype "(Int) -> Xml",
PURE);
"floatToXml",
(`PFun (fun _ ->
string_to_xml -<- (conversion_op' ~unbox:unbox_float ~conv:(string_of_float') ~box:box_string)),
datatype "(Float) -> Xml",
PURE);
"sysexit",
(p1 (fun ret -> Pervasives.exit (unbox_int ret)),
datatype "(Int) ~> a",
IMPURE);
"show",
(p1 (fun v -> box_string (Value.string_of_value v)),
datatype "(a) ~> String",
PURE);
"exit",
(`Continuation Value.toplevel_cont,
(* Return type must be free so that it unifies with things that
might be used alternatively. E.g.:
if (test) exit(1) else 42 *)
datatype "(a) ~> b",
IMPURE);
(* Adds a list of attributes (represented as pairs of strings) to
each of the root nodes of an XML forest. *)
"addAttributes",
(p2 (fun xml attrs -> match xml, attrs with
| `List xmlitems, `List attrs ->
let attrs = List.map (fun p -> unbox_pair p) attrs in
`List (List.map (add_attributes attrs) xmlitems)
| _ -> failwith "Internal error: addAttributes takes an XML forest and a list of attributes"),
datatype "(Xml, [(String, String)]) -> Xml",
PURE);
"Send",
(p2 (fun _pid _msg ->
assert(false)), (* Now handled in evalir.ml *)
datatype "forall a::Type(Any, Any).(Process ({hear:a|_}), a) ~> ()",
IMPURE);
"self",
(`PFun (fun _ _ -> `Pid (`ServerPid (Proc.get_current_pid()))),
datatype "() ~e~> Process ({ |e })",
IMPURE);
"here",
(`PFun (fun _ _ -> `SpawnLocation (`ServerSpawnLoc)),
datatype "() ~> Location",
IMPURE
);
"there",
(`PFun (fun req_data _ ->
let client_id = RequestData.get_client_id req_data in
`SpawnLocation (`ClientSpawnLoc client_id)),
datatype "() ~> Location",
IMPURE
);
"haveMail",
(`PFun(fun _ ->
failwith "The haveMail function is not implemented on the server yet"),
datatype "() {:_|_}~> Bool",
IMPURE);
"recv",
(* This function is not used, as its application is a special case
in the interpreter. But we need it here (for now) to assign it a
type. Ultimately we should probably not special-case it, but
rather provide a way to implement this primitive from here.
(Ultimately, it should perhaps be a true primitive (an AST node),
because it uses a different evaluation mechanism from functions.
-- jdy) *)
(`PFun (fun (_) -> assert false),
datatype "() {:a|_}~> a",
IMPURE);
"spawn",
(`PFun (fun _ -> assert false),
datatype "(() ~e~@ _) ~> Process ({ |e })",
IMPURE);
"spawnAt",
(`PFun (fun _ -> assert false),
datatype "(Location, (() ~e~@ _)) ~> Process ({ |e })",
IMPURE);
"spawnClient",
(`PFun (fun _ -> assert false),
datatype "(() ~e~@ _) ~> Process ({ |e })",
IMPURE);
"spawnAngel",
(`PFun (fun _ -> assert false),
datatype "(() ~e~@ _) ~> Process ({ |e })",
IMPURE);
"spawnAngelAt",
(`PFun (fun _ -> assert false),
datatype "(Location, (() ~e~@ _)) ~> Process ({ |e })",
IMPURE);
"spawnWait",
(`PFun (fun _ -> assert false),
datatype "(() ~> a) ~> a",
IMPURE);
"spawnWait'",
(`PFun (fun _ -> assert false),
datatype "() ~> a",
IMPURE);
(* If we add more effects then spawn and spawnWait shouldn't
necessarily mask them, so we might want to change their types to
something like this:
spawn : (() {wild{p},hear{q}:a|e}-> _) {hear{_}:_|e}~> Process({wild{p},hear{q}:a|e})
spawnWait : (() {wild{_},hear{_}:_|e}-> a) {hear{_}:_|e}~> a
We might even split spawnWait into spawn and wait:
spawn : (() {wild{p},hear{q}:b|e}-> a) {hear{_}:_|e}~> Process(a, {wild{p},hear{q}:b|e})
wait : Process (a, {wild{_},hear{_}:_|e}) {hear{_}:_}~> a
*)
(* Sessions *)
"send",
(`PFun (fun _ -> assert false),
datatype "forall a::Type(Any, Any), s::Type(Any, Session).(a, !a.s) ~> s",
IMPURE);
"receive",
(`PFun (fun _ -> assert false),
datatype "forall a::Type(Any, Any), s::Type(Any, Session). (?a.s) ~> (a, s)",
IMPURE);
"link",
(`PFun (fun _ -> assert false),
datatype "forall s::Type(Any, Session), e::Row(Unl, Any).(s, ~s) ~e~> ()",
IMPURE);
(* access points *)
"new",
(`PFun (fun _ -> assert false),
datatype "forall s::Type(Any, Session).() ~> AP(s)",
IMPURE);
"newAP",
(`PFun (fun _ -> assert false),
datatype "forall s::Type(Any, Session). (Location) ~> AP(s)",
IMPURE);
"newClientAP",
(`PFun (fun _ -> assert false),
datatype "forall s::Type(Any, Session).() ~> AP(s)",
IMPURE);
"newServerAP",
(`PFun (fun _ -> assert false),
datatype "forall s::Type(Any, Session).() ~> AP(s)",
IMPURE);
"accept",
(`PFun (fun _ -> assert false),
datatype "forall s::Type(Any, Session).(AP(s)) ~> s",
IMPURE);
"request",
(`PFun (fun _ -> assert false),
datatype "forall s::Type(Any, Session).(AP(s)) ~> ~s",
IMPURE);
(* Lists and collections *)
"Nil",
(`List [],
datatype "[a]",
PURE);
"Cons",
(p2 (fun x xs ->
box_list (x :: (unbox_list xs))),
datatype "(a, [a]) -> [a]",
PURE);
"Concat",
(p2 (fun xs ys ->
box_list (unbox_list xs @ unbox_list ys)),
datatype "([a], [a]) -> [a]",
PURE);
"hd",
(p1 (fun lst ->
match (unbox_list lst) with
| [] -> failwith "hd() of empty list"
| x :: _ -> x
),
datatype "([a]) ~> a",
IMPURE);
"tl",
(p1 (fun lst ->
match (unbox_list lst) with
| [] -> failwith "tl() of empty list"
| _x :: xs -> box_list xs
),
datatype "([a]) ~> [a]",
IMPURE);
"length",
(p1 (unbox_list ->- List.length ->- box_int),
datatype "([a]) -> Int",
PURE);
"take",
(p2 (fun n l ->
box_list (Utility.take (unbox_int n) (unbox_list l))),
datatype "(Int, [a]) ~> [a]",
PURE);
"drop",
(p2 (fun n l ->
box_list (Utility.drop (unbox_int n) (unbox_list l))),
datatype "(Int, [a]) ~> [a]",
PURE);
"max",
(p1 (let max2 x y = if less x y then y else x in
function
| `List [] -> `Variant ("None", `Record [])
| `List (x::xs) -> `Variant ("Some", List.fold_left max2 x xs)
| _ -> failwith "Internal error: non-list passed to max"),
datatype "([a]) ~> [|Some:a | None:()|]",
PURE);
"min",
(p1 (let min2 x y = if less x y then x else y in
function
| `List [] -> `Variant ("None", `Record [])
| `List (x::xs) -> `Variant ("Some", List.fold_left min2 x xs)
| _ -> failwith "Internal error: non-list passed to min"),
datatype "([a]) ~> [|Some:a | None:()|]",
PURE);
(* XML *)
"childNodes",
(p1 (function
| `List [`XML (Node (_, children))] ->
let children = filter (function (Node _) -> true | _ -> false) children in
`List (map (fun x -> `XML x) children)
| _ -> failwith "non-XML given to childNodes"),
datatype "(Xml) -> Xml",
IMPURE);
"objectType",
(`Client, datatype "(a) ~> String",
IMPURE);
"attribute",
(p2 (let none = `Variant ("None", `Record []) in
fun elem attr ->
match elem with
| `List ((`XML (Node (_, children)))::_) ->
let attr = unbox_string attr in
let attr_match = (function
| Attr (k, _) when k = attr -> true
| _ -> false) in
(try match List.find attr_match children with
| Attr (_, v) -> `Variant ("Some", box_string v)
| _ -> failwith "Internal error in `attribute'"
with NotFound _ -> none)
| _ -> none),
datatype "(Xml,String) -> [|Some:String | None:()|]",
PURE);
"alertDialog",
(`Client, datatype "(String) ~> ()",
IMPURE);
"debug",
(p1 (fun message -> Debug.print (unbox_string message);
`Record []),
datatype "(String) ~> ()",
IMPURE);
"debugObj",
(`Client, datatype "(a) ~> ()",
IMPURE);
"dump",
(`Client, datatype "(a) ~> ()",
IMPURE);
"textContent",
(`Client, datatype "(DomNode) ~> String",
IMPURE);
"print",
(p1 (fun msg -> print_endline (unbox_string msg); flush stdout; `Record []),
datatype "(String) ~> ()",
IMPURE);
"javascript",
(`Bool false, datatype "Bool",
PURE);
"not",
(p1 (unbox_bool ->- not ->- box_bool),
datatype "(Bool) -> Bool",
PURE);
"negate",
(p1 (unbox_int ->- (~-) ->- box_int), datatype "(Int) -> Int",
PURE);
"negatef",
(p1 (fun f -> box_float (-. (unbox_float f))), datatype "(Float) -> Float",
PURE);
"error",
(p1 (unbox_string ->- failwith), datatype "(String) ~> a",
IMPURE);
(* HACK *)
(* [DEACTIVATED] *)
(* "callForeign", *)
(* (`Client, datatype "((a) -> b) -> (a) -> b"); *)
(* DOM API *)
"isElementNode",
(`Client, datatype "(DomNode) ~> Bool",
PURE);
(* [DEACTIVATED] *)
(* "domOp", *)
(* (p1 (fun message -> failwith("`domOp' is only available on the client."); *)
(* `Record []), *)
(* datatype "(a) -> ()"); *)
"insertBefore",
(`Client, datatype "(Xml, DomNode) ~> ()",
IMPURE);
"appendChildren",
(`Client, datatype "(Xml, DomNode) ~> ()",
IMPURE);
"replaceNode",
(`Client, datatype "(Xml, DomNode) ~> ()",
IMPURE);
"replaceDocument",
(`Client, datatype "(Xml) ~> ()",
IMPURE);
"domInsertBeforeRef",
(`Client, datatype "(DomNode, DomNode) ~> ()",
IMPURE);
"domAppendChildRef",
(`Client, datatype "(DomNode, DomNode) ~> ()",
IMPURE);
"removeNode",
(`Client, datatype "(DomNode) ~> ()",
IMPURE);
"cloneNode",
(`Client, datatype "(DomNode, Bool) ~> (DomNode)",
IMPURE);
"replaceChildren",
(`Client, datatype "(Xml, DomNode) ~> ()",
IMPURE);
"swapNodes",
(`Client, datatype "(DomNode, DomNode) ~> ()",
IMPURE);
"getDocumentNode",
(`Client, datatype "() ~> DomNode",
IMPURE);
"getNodeById",
(`Client, datatype "(String) ~> DomNode",
IMPURE);
"getValue",
(`Client, datatype "(DomNode) ~> Xml",
IMPURE);
"isNull",
(`Client, datatype "(DomNode) ~> Bool",
PURE);
(* Section: Accessors for XML *)
"xmlToVariant",
(`Server (p1 (fun v ->
match v with
| `List xs ->
`List (List.map (function
| (`XML x) -> Value.value_of_xmlitem x
| _ -> failwith "non-XML passed to xmlToVariant") xs)
| _ -> failwith "non-XML passed to xmlToVariant")),
datatype "(Xml) ~> mu n.[ [|Text:String | Attr:(String, String) | Node:(String, n) |] ]",
IMPURE);
"getTagName",
(p1 (fun v ->
match v with
| `List [`XML(Node(name, _))] ->
box_string name
| _ -> failwith "non-element passed to getTagName"),
datatype "(Xml) ~> String",
IMPURE);
"getTextContent",
(`Client, datatype "(Xml) ~> String",
IMPURE);
"getAttributes",
(p1 (fun v ->
match v with
| `List [`XML(Node(_, children))] ->
`List (map
(function
| (Attr (name, value)) ->
`Record [("1", box_string name); ("2", box_string value)]
| _ -> assert false)
(filter (function (Attr _) -> true | _ -> false) children))
| _ -> failwith "non-element given to getAttributes"),
datatype "(Xml) ~> [(String,String)]",
IMPURE);
"hasAttribute",
(`Client, datatype "(Xml, String) ~> Bool",
PURE);
"getAttribute",
(`Client, datatype "(Xml, String) ~> String",
IMPURE);
(* Section: Navigation for XML *)
"getChildNodes",
(p1 (fun v ->
match v with
| `List [`XML(Node(_, children))] ->
`List (map (fun x -> `XML(x)) (filter (function (Attr _) -> false | _ -> true) children))
| _ -> failwith "non-element given to getChildNodes"),
datatype "(Xml) ~> Xml",
IMPURE);
"not",
(p1 (unbox_bool ->- not ->- box_bool),
datatype "(Bool) -> Bool",
PURE);
(* Section: Accessors for DomNodes *)
"domGetNodeValueFromRef",
(`Client, datatype "(DomNode) ~> String",
IMPURE);
"domGetTagNameFromRef",
(`Client, datatype "(DomNode) ~> String",
IMPURE);
"domGetPropertyFromRef",
(`Client, datatype "(DomNode, String) ~> String",
IMPURE);
"domSetPropertyFromRef",
(`Client, datatype "(DomNode, String, String) ~> String",
IMPURE);
"domHasAttribute",
(`Client, datatype "(DomNode, String) ~> Bool",
IMPURE);
"domRemoveAttributeFromRef",
(`Client, datatype "(DomNode, String) ~> ()",
IMPURE);
"domGetAttributeFromRef",
(`Client, datatype "(DomNode, String) ~> String",
IMPURE);
"domSetAttributeFromRef",
(`Client, datatype "(DomNode, String, String) ~> String",
IMPURE);
"domGetStyleAttrFromRef",
(`Client, datatype "(DomNode, String) ~> String",
IMPURE);
"domSetStyleAttrFromRef",
(`Client, datatype "(DomNode, String, String) ~> String",
IMPURE);
(* Section: Navigation for DomNodes *)
"parentNode",
(`Client, datatype "(DomNode) ~> DomNode",
IMPURE);
"firstChild",
(`Client, datatype "(DomNode) ~> DomNode",
IMPURE);
"nextSibling",
(`Client, datatype "(DomNode) ~> DomNode",
IMPURE);
(* Section: DOM Event API *)
"getTarget",
(`Client, datatype "(Event) ~> DomNode",
PURE);
"getTargetValue",
(`Client, datatype "(Event) ~> String",
PURE);
"getTargetElement",
(`Client, datatype "(Event) ~> DomNode",
PURE);
(* event handlers *)
(* what effect annotation should the inner arrow have? *)
"registerEventHandlers",
(`PFun (fun _ -> assert false),
datatype "([(String, (Event) ~> ())]) ~> String",
IMPURE);
(* getPageX : (Event) -> Int *)
"getPageX",
(`Client, datatype "(Event) ~> Int",
PURE);
(* getPageY : (Event) -> Int *)
"getPageY",
(`Client, datatype "(Event) ~> Int",
PURE);
(* getFromElement : (Event) -> DomNode *)
"getFromElement",
(`Client, datatype "(Event) ~> DomNode",
PURE);
(* getToElement : (Event) -> DomNode *)
"getToElement",
(`Client, datatype "(Event) ~> DomNode",
PURE);
(* getTime : (Event) -> Int *)
"getTime",
(`Client, datatype "(Event) ~> Int",
PURE);
(* getCharCode : (Event) -> Int *)
"getCharCode",
(`Client, datatype "(Event) ~> Int",
PURE);
"getInputValue",
(`Client, datatype "(String) ~> String",
PURE);
"event",
(`Client, datatype "Event",
PURE);
(* domSetAnchor : String -> () *)
"domSetAnchor",
(`Client, datatype "(String) ~> ()",
IMPURE);
(* Yahoo UI library functions we don't implement: *)
(* # stopEvent : ??? *)
(* # stopPropagation : ??? *)
(* # preventDefault : ??? *)
(* Cookies *)
"setCookie",
(p2D (fun cookieName cookieVal req_data ->
let cookieName = unbox_string cookieName in
let cookieVal = unbox_string cookieVal in
let resp_headers = RequestData.get_http_response_headers req_data in
RequestData.set_http_response_headers req_data
(("Set-Cookie", cookieName ^ "=" ^ cookieVal) :: resp_headers);
`Record []
(* Note: perhaps this should affect cookies returned by
getcookie during the current request. *)),
datatype "(String, String) ~> ()",
IMPURE);
(* WARNING:
getCookie returns "" to indicate either that the cookie is not
present or that the header is ill-formed (in debug mode a warning
will also be sent to stderr if the header is ill-formed).
Ideally, perhaps, a malformed header from the client should
be ignored at this level (let the HTTP agents handle it).
An absent cookie should probably be indicated by a None value in
the Maybe(String) type.
*)
"getCookie",
(p1D (fun name req_data ->
let name = unbox_string name in
let cookies = RequestData.get_cookies req_data in
let value =
if List.mem_assoc name cookies then
List.assoc name cookies
else
""
in
box_string value),
datatype "(String) ~> String",
IMPURE);
(* getCommandOutput disabled for now; possible security risk. *)
(*
"getCommandOutput",
(p1 ((unbox_string ->- Utility.process_output ->- box_string) :> result -> primitive),
datatype "(String) -> String");
*)
"redirect",
(p1D (fun url req_data ->
let url = unbox_string url in
(* This is all quite hackish, just testing an idea. --ez *)
let resp_headers = RequestData.get_http_response_headers req_data in
RequestData.set_http_response_headers req_data (("Location", url) :: resp_headers);
RequestData.set_http_response_code req_data 302;
`Record []
), datatype "(String) ~> ()",
IMPURE);
(* Should this function really return?
I think not --ez*)
(* REDUNDANT *)
(* (\** reifyK: I choose an obscure name, for an obscure function, until *)
(* a better one can be thought up. It just turns a continuation into its *)
(* string representation *\) *)
(* "reifyK", *)
(* (p1 (function *)
(* `Continuation k -> *)
(* let s = marshal_continuation k in *)
(* box_string s *)
(* | _ -> failwith "argument to reifyK was not a continuation" *)
(* ), *)
(* datatype "((a) -> b) ~> String", *)
(* IMPURE); *)
(* (\* arg type should actually be limited *)
(* to continuations, but we don't have *)
(* any way of specifying that in the *)
(* type system. *\) *)
"sleep",
(p1 (fun _ ->
(* FIXME: This isn't right : it freezes all threads *)
(*Unix.sleep (int_of_num (unbox_int duration));
`Record []*)
failwith "The sleep function is not implemented on the server yet"
),
datatype "(Int) ~> ()",
IMPURE);
"clientTime",
(`Client,
datatype "() ~> Int",
IMPURE);
"serverTime",
(`Server
(`PFun (fun _ _ ->
box_int(int_of_float(Unix.time())))),
datatype "() ~> Int",
IMPURE);
"serverTimeMilliseconds",
(`Server
(`PFun (fun _ _ ->
box_int(time_milliseconds()))),
datatype "() ~> Int",
IMPURE);
"dateToInt",
(p1 (fun r ->
match r with
| `Record r ->
let lookup s =
unbox_int (List.assoc s r) in
let tm = {
Unix.tm_sec = lookup "seconds";
Unix.tm_min = lookup "minutes";
Unix.tm_hour = lookup "hours";
Unix.tm_mday = lookup "day";
Unix.tm_mon = lookup "month";
Unix.tm_year = (lookup "year" - 1900);
Unix.tm_wday = 0; (* ignored *)
Unix.tm_yday = 0; (* ignored *)
Unix.tm_isdst = false} in
let t, _ = Unix.mktime tm in
box_int (int_of_float t)
| _ -> assert false),
datatype "((year:Int, month:Int, day:Int, hours:Int, minutes:Int, seconds:Int)) ~> Int",
IMPURE);
"intToDate",
(p1 (fun t ->
let tm = Unix.localtime(float_of_int (unbox_int t)) in
`Record [
"year", box_int (tm.Unix.tm_year + 1900);
"month", box_int tm.Unix.tm_mon;
"day", box_int tm.Unix.tm_mday;
"hours", box_int tm.Unix.tm_hour;