2023-12-02 18:51:15 +00:00
|
|
|
type slotidx = int
|
|
|
|
type mthdidx = int
|
|
|
|
|
2023-11-29 22:56:42 +00:00
|
|
|
type vtable = {
|
|
|
|
n_slots : int;
|
|
|
|
elems : (string, elem) Hashtbl.t;
|
2023-12-02 18:51:15 +00:00
|
|
|
mthds : mthd array;
|
2023-11-29 22:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
and elem =
|
|
|
|
| Field of slotidx
|
2023-12-02 18:51:15 +00:00
|
|
|
| Method of mthdidx
|
2023-11-29 22:56:42 +00:00
|
|
|
|
2023-12-02 18:51:15 +00:00
|
|
|
and mthd = ..
|
2023-11-29 22:56:42 +00:00
|
|
|
|
2023-11-29 21:48:24 +00:00
|
|
|
type t =
|
|
|
|
| Nil
|
|
|
|
| True
|
|
|
|
| False
|
|
|
|
| Int of int64
|
2023-11-29 22:56:42 +00:00
|
|
|
| Obj of vtable * t array
|
2023-11-29 21:48:24 +00:00
|
|
|
|
2023-11-29 22:56:42 +00:00
|
|
|
let make_obj vtable = Obj (vtable, Array.make vtable.n_slots Nil)
|
|
|
|
|
2023-11-30 03:46:27 +00:00
|
|
|
let bool = function
|
|
|
|
| true -> True
|
|
|
|
| false -> False
|
|
|
|
|
2023-12-02 20:31:29 +00:00
|
|
|
let of_elem e =
|
|
|
|
let idx =
|
|
|
|
match e with
|
|
|
|
| Field i -> i
|
|
|
|
| Method i -> -succ i
|
|
|
|
in
|
|
|
|
Int (Int64.of_int idx)
|
|
|
|
|
|
|
|
let to_elem = function
|
|
|
|
| Int idx ->
|
2023-12-02 22:02:40 +00:00
|
|
|
let i = Int64.to_int idx in
|
|
|
|
if i >= 0 then
|
|
|
|
Field i
|
|
|
|
else
|
|
|
|
Method (-succ i)
|
|
|
|
| _ ->
|
|
|
|
invalid_arg "to_elem: non integer value"
|
2023-12-02 20:31:29 +00:00
|
|
|
|
2023-11-29 22:56:42 +00:00
|
|
|
let rec pp ppf = function
|
|
|
|
| Obj (vtable, slots) -> pp_obj ppf vtable slots
|
|
|
|
| Int n -> Fmt.string ppf (Int64.to_string n)
|
|
|
|
| v -> Fmt.pf ppf "%S" (to_string v)
|
|
|
|
|
|
|
|
and to_string = function
|
2023-11-29 21:48:24 +00:00
|
|
|
| Nil -> "nil"
|
|
|
|
| True -> "true"
|
|
|
|
| False -> "false"
|
2023-11-29 22:56:42 +00:00
|
|
|
| v -> Fmt.str "%a" pp v
|
2023-11-29 21:48:24 +00:00
|
|
|
|
2023-11-29 22:56:42 +00:00
|
|
|
and pp_obj ppf vtable slots =
|
|
|
|
Fmt.pf ppf "{";
|
|
|
|
let sep = ref "" in
|
|
|
|
Hashtbl.iter
|
|
|
|
(fun name -> function
|
2023-12-02 22:02:40 +00:00
|
|
|
| Method _ -> ()
|
|
|
|
| Field idx ->
|
|
|
|
Fmt.pf ppf "%s%S:%a" !sep name pp slots.(idx);
|
|
|
|
sep := ",")
|
2023-11-29 22:56:42 +00:00
|
|
|
vtable.elems;
|
|
|
|
Fmt.pf ppf "}"
|
2023-12-02 18:51:15 +00:00
|
|
|
|
2023-12-02 21:06:57 +00:00
|
|
|
type mthd += Native_function of (t list -> t)
|
2023-12-02 18:51:15 +00:00
|
|
|
|
2023-12-02 21:06:57 +00:00
|
|
|
let call mthd _self args =
|
2023-12-02 20:31:29 +00:00
|
|
|
match mthd with
|
2023-12-02 21:06:57 +00:00
|
|
|
| Native_function f -> f args
|
2023-12-02 18:51:15 +00:00
|
|
|
| _ -> raise Not_found
|
2023-12-02 21:06:57 +00:00
|
|
|
|
|
|
|
let native_lib fns =
|
|
|
|
let elems = Hashtbl.create (List.length fns * 4) in
|
|
|
|
List.iteri (fun i (name, _) -> Hashtbl.add elems name (Method i)) fns;
|
2023-12-02 22:02:40 +00:00
|
|
|
let mthds = List.map (fun (_, f) -> Native_function f) fns |> Array.of_list in
|
2023-12-02 21:06:57 +00:00
|
|
|
let vtable = { n_slots = 0; elems; mthds } in
|
|
|
|
Obj (vtable, [||])
|