[ocaml-ctypes] Best practices for wrapping ocaml-ctypes?

Andi McClure andi.m.mcclure at gmail.com
Sat Jun 27 14:25:13 BST 2015

Hello, I have a project which is a programming language (
http://emilylang.org/ ) and the current interpreter is implemented in
ocaml. I want to add a C FFI, and I am looking at ocaml-ctypes, but I am
having trouble figuring out how best to use the ocaml-ctypes library given
my project's specific needs as a language.

Basically: ocaml-ctypes seems to take libFFI and recast its operations in
terms of ocaml idioms and primitives. This looks great if I am writing an
ocaml program. However, my goal is to present an interface to libffi (or
something like it) in terms of *Emily* idioms and primitives (Emily being
my language) so that I can write an Emily program. This means directly
wrapping ocaml-ctypes probably won't work out well (the ocaml idioms
ocaml-ctypes uses can't all be directly represented in my language, and
they might come across as weird to the language users who are probably not
ocaml users).

My initial thought was that what I probably wanted to do was try to present
an interface between the interpreter and interpreted code that closely
tracks the programming interface of libffi. Then I could write an
in-language library on top of this with a friendlier/more idiomatic
interface. (The reason I believe I want the interpreter-interpreted
interface to resemble libffi is that I might someday switch from ocaml to
another language, and libffi will probably be available in other contexts
but ocaml-ctypes will not). Looking over the ctypes code it looked like a
"raw" libffi-flavored interface might be possible using
ctypes-foreign-base/ctypes_ffi.mli and the function_of_pointer interface,
but it looks like this mli is present in the code but not exposed in the
opam package.

What would you recommend in this case?

Is there a way to access ctypes-foreign-base after all? Is that a
reasonable thing to attempt?

Is there a way to use ocaml-ctypes which at least closely resembles a "trap
door" to let a client provide access to libffi directly?

Is there a library other than ocaml-ctypes which wraps libffi more directly?

Am I meowing up the wrong tree entirely? :)

Any advice welcome, thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ocaml.org/pipermail/ctypes/attachments/20150627/a60d1de7/attachment.html>

More information about the Ctypes mailing list