Added 'make test' and some test scripts which exercise parts of the
[perl4caml.git] / perl.ml
1 (* Interface to Perl from OCaml.
2  * Copyright (C) 2003 Merjis Ltd.
3  * $Id: perl.ml,v 1.13 2005-01-28 23:09:31 rich Exp $
4  *)
5
6 type sv
7 type av
8 type hv
9
10 exception Perl_failure of string
11
12 (* Initialization. This must happen first, otherwise other parts of the
13  * program will segfault because of a missing interpreter.
14  *)
15 external c_init : unit -> unit = "perl4caml_init"
16 let () =
17   Callback.register_exception "perl4caml_perl_failure" (Perl_failure "");
18   c_init ();                            (* Initialise C code. *)
19   ()
20
21 external int_of_sv : sv -> int = "perl4caml_int_of_sv"
22 external sv_of_int : int -> sv = "perl4caml_sv_of_int"
23 external float_of_sv : sv -> float = "perl4caml_float_of_sv"
24 external sv_of_float : float -> sv = "perl4caml_sv_of_float"
25 external string_of_sv : sv -> string = "perl4caml_string_of_sv"
26 external sv_of_string : string -> sv = "perl4caml_sv_of_string"
27 external sv_is_true : sv -> bool = "perl4caml_sv_is_true"
28 external sv_undef : unit -> sv = "perl4caml_sv_undef"
29 external sv_is_undef : sv -> bool = "perl4caml_sv_is_undef"
30 external sv_yes : unit -> sv = "perl4caml_sv_yes"
31 external sv_no : unit -> sv = "perl4caml_sv_no"
32
33 let sv_true () = sv_of_int 1
34 let sv_false () = sv_of_int 0
35
36 let bool_of_sv = sv_is_true
37 let sv_of_bool b = if b then sv_true () else sv_false ()
38
39 type sv_t    = SVt_NULL
40              | SVt_IV
41              | SVt_NV
42              | SVt_PV
43              | SVt_RV
44              | SVt_PVAV
45              | SVt_PVHV
46              | SVt_PVCV
47              | SVt_PVGV
48              | SVt_PVMG
49
50 external sv_type : sv -> sv_t = "perl4caml_sv_type"
51
52 let string_of_sv_t = function
53     SVt_NULL  -> "SVt_NULL"
54   | SVt_IV    -> "SVt_IV"
55   | SVt_NV    -> "SVt_NV"
56   | SVt_PV    -> "SVt_PV"
57   | SVt_RV    -> "SVt_RV"
58   | SVt_PVAV  -> "SVt_PVAV"
59   | SVt_PVHV  -> "SVt_PVHV"
60   | SVt_PVCV  -> "SVt_PVCV"
61   | SVt_PVGV  -> "SVt_PVGV"
62   | SVt_PVMG  -> "SVt_PVMG"
63
64 external address_of_sv : sv -> Nativeint.t = "perl4caml_address_of_sv"
65 external address_of_av : av -> Nativeint.t = "perl4caml_address_of_av"
66 external address_of_hv : hv -> Nativeint.t = "perl4caml_address_of_hv"
67
68 external scalarref : sv -> sv = "perl4caml_scalarref"
69 external arrayref : av -> sv = "perl4caml_arrayref"
70 external hashref : hv -> sv = "perl4caml_hashref"
71
72 external deref : sv -> sv = "perl4caml_deref"
73 external deref_array : sv -> av = "perl4caml_deref_array"
74 external deref_hash : sv -> hv = "perl4caml_deref_hash"
75
76 external av_empty : unit -> av = "perl4caml_av_empty"
77 external av_of_sv_list : sv list -> av = "perl4caml_av_of_sv_list"
78 external av_push : av -> sv -> unit = "perl4caml_av_push"
79 external av_pop : av -> sv = "perl4caml_av_pop"
80 external av_shift : av -> sv = "perl4caml_av_shift"
81 external av_unshift : av -> sv -> unit = "perl4caml_av_unshift"
82 external av_length : av -> int = "perl4caml_av_length"
83 external av_set : av -> int -> sv -> unit = "perl4caml_av_set"
84 external av_get : av -> int -> sv = "perl4caml_av_get"
85 external av_clear : av -> unit = "perl4caml_av_clear"
86 external av_undef : av -> unit = "perl4caml_av_undef"
87 external av_extend : av -> int -> unit = "perl4caml_av_extend"
88
89 let av_map f av =
90   let list = ref [] in
91   for i = 0 to av_length av - 1 do
92     list := f (av_get av i) :: !list
93   done;
94   List.rev !list
95
96 let list_of_av av =
97   let list = ref [] in
98   for i = 0 to av_length av - 1 do
99     list := av_get av i :: !list
100   done;
101   List.rev !list
102
103 let av_of_string_list strs =
104   av_of_sv_list (List.map sv_of_string strs)
105
106 external hv_empty : unit -> hv = "perl4caml_hv_empty"
107 external hv_set : hv -> string -> sv -> unit = "perl4caml_hv_set"
108 external hv_get : hv -> string -> sv = "perl4caml_hv_get"
109 external hv_exists : hv -> string -> bool = "perl4caml_hv_exists"
110 external hv_delete : hv -> string -> unit = "perl4caml_hv_delete"
111 external hv_clear : hv -> unit = "perl4caml_hv_clear"
112 external hv_undef : hv -> unit = "perl4caml_hv_undef"
113
114 external get_sv : ?create:bool -> string -> sv = "perl4caml_get_sv"
115 external get_av : ?create:bool -> string -> av = "perl4caml_get_av"
116 external get_hv : ?create:bool -> string -> hv = "perl4caml_get_hv"
117
118 external call : ?sv:sv -> ?fn:string -> sv list -> sv
119   = "perl4caml_call"
120 external call_array : ?sv:sv -> ?fn:string -> sv list -> sv list
121   = "perl4caml_call_array"
122 external call_void : ?sv:sv -> ?fn:string -> sv list -> unit
123   = "perl4caml_call_void"
124
125 external eval : string -> sv
126   = "perl4caml_eval"
127
128 external call_method : sv -> string -> sv list -> sv
129   = "perl4caml_call_method"
130 external call_method_array : sv -> string -> sv list -> sv list
131   = "perl4caml_call_method_array"
132 external call_method_void : sv -> string -> sv list -> unit
133   = "perl4caml_call_method_void"
134 external call_class_method : string -> string -> sv list -> sv
135   = "perl4caml_call_class_method"
136 external call_class_method_array : string -> string -> sv list -> sv list
137   = "perl4caml_call_class_method_array"
138 external call_class_method_void : string -> string -> sv list -> unit
139   = "perl4caml_call_class_method_void"