|
| 1 | +open Base |
| 2 | +open Import |
| 3 | + |
| 4 | +module Id : sig |
| 5 | + type t |
| 6 | + |
| 7 | + val create : unit -> t |
| 8 | + val to_string : t -> string |
| 9 | +end = struct |
| 10 | + type t = int |
| 11 | + |
| 12 | + let create = |
| 13 | + let current = ref 0 in |
| 14 | + fun () -> |
| 15 | + Int.incr current; |
| 16 | + !current |
| 17 | + ;; |
| 18 | + |
| 19 | + let to_string = Int.to_string |
| 20 | +end |
| 21 | + |
| 22 | +let content_field = "_content" |
| 23 | + |
| 24 | +type 'a t = |
| 25 | + { wrap : 'a -> Py.Object.t |
| 26 | + ; unwrap : Py.Object.t -> 'a |
| 27 | + ; name : string |
| 28 | + ; mutable cls_object : Py.Object.t option |
| 29 | + } |
| 30 | + |
| 31 | +let set_cls_object_exn t pyobject = |
| 32 | + if Option.is_some t.cls_object |
| 33 | + then Printf.failwithf "cls_object for %s has already been set" t.name (); |
| 34 | + t.cls_object <- Some pyobject |
| 35 | +;; |
| 36 | + |
| 37 | +module Init = struct |
| 38 | + type 'a cls = 'a t |
| 39 | + |
| 40 | + type 'a t = |
| 41 | + { fn : 'a cls -> args:pyobject list -> 'a |
| 42 | + ; docstring : string option |
| 43 | + } |
| 44 | + [@@deriving fields] |
| 45 | + |
| 46 | + let create ?docstring fn = { docstring; fn } |
| 47 | +end |
| 48 | + |
| 49 | +module Method = struct |
| 50 | + type 'a cls = 'a t |
| 51 | + |
| 52 | + type 'a fn = |
| 53 | + | No_keywords of ('a cls -> self:'a * pyobject -> args:pyobject list -> pyobject) |
| 54 | + | With_keywords of |
| 55 | + ('a cls |
| 56 | + -> self:'a * pyobject |
| 57 | + -> args:pyobject list |
| 58 | + -> keywords:(string, pyobject, String.comparator_witness) Map.t |
| 59 | + -> pyobject) |
| 60 | + |
| 61 | + type 'a t = |
| 62 | + { name : string |
| 63 | + ; fn : 'a fn |
| 64 | + ; docstring : string option |
| 65 | + } |
| 66 | + [@@deriving fields] |
| 67 | + |
| 68 | + let create ?docstring name fn = { name; fn = No_keywords fn; docstring } |
| 69 | + |
| 70 | + let create_with_keywords ?docstring name fn = |
| 71 | + { name; fn = With_keywords fn; docstring } |
| 72 | + ;; |
| 73 | + |
| 74 | + let defunc ?docstring name fn = |
| 75 | + let fn cls ~self ~args ~keywords = |
| 76 | + Defunc.apply (fn cls ~self) (Array.of_list args) keywords |
| 77 | + in |
| 78 | + create_with_keywords ?docstring name fn |
| 79 | + ;; |
| 80 | +end |
| 81 | + |
| 82 | +let wrap_capsule t obj = t.wrap obj |
| 83 | + |
| 84 | +let unwrap_exn t pyobj = |
| 85 | + let pyobj = |
| 86 | + match Py.Object.get_attr_string pyobj content_field with |
| 87 | + | None -> Printf.failwithf "no %s field in object" content_field () |
| 88 | + | Some content -> content |
| 89 | + in |
| 90 | + if not (Py.Capsule.check pyobj) then failwith "not an ocaml capsule"; |
| 91 | + t.unwrap pyobj |
| 92 | +;; |
| 93 | + |
| 94 | +let unwrap t pyobj = |
| 95 | + try Some (unwrap_exn t pyobj) with |
| 96 | + | _ -> None |
| 97 | +;; |
| 98 | + |
| 99 | +let wrap t obj = |
| 100 | + let cls = Option.value_exn t.cls_object in |
| 101 | + let pyobject = Py.Object.call_function_obj_args cls [||] in |
| 102 | + Py.Object.set_attr_string pyobject content_field (wrap_capsule t obj); |
| 103 | + pyobject |
| 104 | +;; |
| 105 | + |
| 106 | +let make ?to_string_repr ?to_string ?eq ?init name ~methods = |
| 107 | + let id = Id.create () in |
| 108 | + let t = |
| 109 | + let wrap, unwrap = Py.Capsule.make (Printf.sprintf !"%s-%{Id}" name id) in |
| 110 | + { wrap; unwrap; cls_object = None; name } |
| 111 | + in |
| 112 | + let methods = |
| 113 | + let to_string = |
| 114 | + Option.map to_string ~f:(fun fn t ~self ~args:_ -> |
| 115 | + fn t (fst self) |> Py.String.of_string) |
| 116 | + in |
| 117 | + let to_string_repr = |
| 118 | + Option.map to_string_repr ~f:(fun fn t ~self ~args:_ -> |
| 119 | + fn t (fst self) |> Py.String.of_string) |
| 120 | + in |
| 121 | + let to_string_repr = Option.first_some to_string_repr to_string in |
| 122 | + let eq = |
| 123 | + Option.map eq ~f:(fun fn t ~self ~args -> |
| 124 | + let rhs = |
| 125 | + match args with |
| 126 | + | [] -> failwith "eq with no argument" |
| 127 | + | _ :: _ :: _ -> |
| 128 | + Printf.failwithf "eq with %d arguments" (List.length args) () |
| 129 | + | [ rhs ] -> rhs |
| 130 | + in |
| 131 | + fn t (fst self) (unwrap_exn t rhs) |> Py.Bool.of_bool) |
| 132 | + in |
| 133 | + List.filter_map |
| 134 | + [ "__str__", to_string; "__repr__", to_string_repr; "__eq__", eq ] |
| 135 | + ~f:(fun (name, fn) -> Option.map fn ~f:(fun fn -> Method.create name fn)) |
| 136 | + @ methods |
| 137 | + in |
| 138 | + let methods = |
| 139 | + List.map methods ~f:(fun { Method.name; fn; docstring } -> |
| 140 | + let fn = |
| 141 | + let self_and_args args = |
| 142 | + let args = Array.to_list args in |
| 143 | + match args with |
| 144 | + | [] -> failwith "empty input" |
| 145 | + | p :: q -> p, q |
| 146 | + in |
| 147 | + match (fn : _ Method.fn) with |
| 148 | + | No_keywords fn -> |
| 149 | + Py.Callable.of_function ?docstring (fun args -> |
| 150 | + let self, args = self_and_args args in |
| 151 | + try fn t ~self:(unwrap_exn t self, self) ~args with |
| 152 | + | Py.Err _ as pyerr -> raise pyerr |
| 153 | + | exn -> |
| 154 | + let msg = Printf.sprintf !"ocaml error %{Exn#mach}" exn in |
| 155 | + raise (Py.Err (ValueError, msg))) |
| 156 | + | With_keywords fn -> |
| 157 | + Py.Callable.of_function_with_keywords ?docstring (fun args keywords -> |
| 158 | + try |
| 159 | + let self, args = self_and_args args in |
| 160 | + let keywords = |
| 161 | + Py_module.keywords_of_python keywords |> Or_error.ok_exn |
| 162 | + in |
| 163 | + fn t ~self:(unwrap_exn t self, self) ~args ~keywords |
| 164 | + with |
| 165 | + | Py.Err _ as pyerr -> raise pyerr |
| 166 | + | exn -> |
| 167 | + let msg = Printf.sprintf !"ocaml error %{Exn#mach}" exn in |
| 168 | + raise (Py.Err (ValueError, msg))) |
| 169 | + in |
| 170 | + name, fn) |
| 171 | + in |
| 172 | + let init = |
| 173 | + let fn = |
| 174 | + let docstring = Option.bind init ~f:Init.docstring in |
| 175 | + Py.Callable.of_function_as_tuple ?docstring (fun tuple -> |
| 176 | + try |
| 177 | + let self, args = |
| 178 | + match Py.Tuple.to_list tuple with |
| 179 | + | [] -> failwith "empty input" |
| 180 | + | p :: q -> p, q |
| 181 | + in |
| 182 | + let content = |
| 183 | + match init with |
| 184 | + | Some init -> init.fn t ~args |> wrap_capsule t |
| 185 | + | None -> Py.none |
| 186 | + in |
| 187 | + Py.Object.set_attr_string self content_field content; |
| 188 | + Py.none |
| 189 | + with |
| 190 | + | Py.Err _ as pyerr -> raise pyerr |
| 191 | + | exn -> |
| 192 | + let msg = Printf.sprintf !"ocaml error %{Exn#mach}" exn in |
| 193 | + raise (Py.Err (ValueError, msg))) |
| 194 | + in |
| 195 | + "__init__", fn |
| 196 | + in |
| 197 | + let cls_object = |
| 198 | + Py.Class.init name ~fields:[ content_field, Py.none ] ~methods:(init :: methods) |
| 199 | + in |
| 200 | + set_cls_object_exn t cls_object; |
| 201 | + t |
| 202 | +;; |
| 203 | + |
| 204 | +let register_in_module t modl = |
| 205 | + Py_module.set_value modl t.name (Option.value_exn t.cls_object) |
| 206 | +;; |
0 commit comments