Skip to content

Instantly share code, notes, and snippets.

@toots
Last active December 1, 2019 17:20
Show Gist options
  • Save toots/0085aadcd4252164a73408d584cb2f43 to your computer and use it in GitHub Desktop.
Save toots/0085aadcd4252164a73408d584cb2f43 to your computer and use it in GitHub Desktop.
include Ctypes
let lift x = x
open Ctypes_static
let rec field : type t a. t typ -> string -> a typ -> (a, t) field =
fun s fname ftype -> match s, fname with
...
| Struct ({ tag = "sockaddr"} as s'), "sa_data" ->
let f = {ftype; fname; foffset = 2} in
(s'.fields <- BoxedField f :: s'.fields; f)
| Struct ({ tag = "sockaddr"} as s'), "sa_family" ->
let f = {ftype; fname; foffset = 1} in
(s'.fields <- BoxedField f :: s'.fields; f)
| View { ty }, _ ->
let { ftype; foffset; fname } = field ty fname ftype in
{ ftype; foffset; fname }
| _ -> failwith ("Unexpected field "^ fname)
let rec seal : type a. a typ -> unit = function
...
| Struct ({ tag = "sockaddr_storage"; spec = Incomplete _ } as s') ->
s'.spec <- Complete { size = 128; align = 8 }
| Struct ({ tag = "sockaddr"; spec = Incomplete _ } as s') ->
s'.spec <- Complete { size = 16; align = 1 }
| Struct { tag; spec = Complete _ } ->
raise (ModifyingSealedType tag)
| Union { utag; uspec = Some _ } ->
raise (ModifyingSealedType utag)
| View { ty } -> seal ty
| _ ->
raise (Unsupported "Sealing a non-structured type")
type 'a const = 'a
let constant (type t) name (t : t typ) : t = match t, name with
| _, s -> failwith ("unmatched constant: "^ s)
let enum (type a) name ?typedef ?unexpected (alist : (a * int64) list) =
match name with
| s ->
failwith ("unmatched enum: "^ s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment