Version française
Home     About     Download     Resources     Contact us    
Browse thread
Easier FFI
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] Easier FFI
I've got flu at the moment so apologies about this slightly unfocused
laundry-list ...

Lessons from ocaml-libvirt:

[Line numbers refer to this file:
http://hg.et.redhat.com/virt/applications/virt-top--devel?f=c29881e5aa70;file=libvirt/libvirt_c.c ]

(1) You need to be able to link to multiple versions of a dynamic
library.  Programs written in OCaml, using ocaml-libvirt, can link to
any libvirt.so >= 0.2.1, even to *future* versions of libvirt.so.

At the very least you need to be able to find out if a function exists
in the dynamic library, and turn non-existance into an OCaml exception
(instead of a dynamic linker segfault).  [lines 65-143, example on
lines 560-578]

(2) In this library and in ODE, C objects contain implicit
relationships with each other, and use reference counting to free
themselves.  This had to be modelled by making the implicit references
explicit in OCaml (so the GC knows about them).  [lines 168-208]

(3) Some libvirt API calls are long-running and need
enter_blocking_section / leave_blocking_section around them.  Others
are short-running and so don't.  [lines 47-57]

(4) You can't assume that a library won't add more values to the end
of an enum, particularly if you allow yourself to link to a later
version of the library than you compile against.  [lines 1826-1889]

On the subject of 'ctypes':

perl4caml offers two 'levels' of access to Perl code.  At the low
level you can directly load / instantiate Perl packages and objects.
This low level is unsafe -- it can throw runtime type exceptions --
but requires no explicit binding code at all.  If you see a Perl
library which you like, as long as it is installed on your system an
OCaml program can use it.  There's also a higher level where you can
write OCaml code which uses Perl objects in a type-safe way, but that
requires someone to write bindings for the Perl library.

However does a 'ctypes for OCaml' need to be type unsafe.  PG'OCaml
(my PostgreSQL bindings for OCaml) does this with a camlp4
preprocessor which processes the SQL statements embedded in the OCaml
code, checks the types of the arguments / return values with the
database, and generates inline type-safe code.  If there was a way to
take a block of C code, work out what types it requires (using
something like gcc? CIL?), then you could generate compile-time
type-safe bindings for inline C code.  That would be a marvellous
thing to have ...

Rich.

-- 
Richard Jones
Red Hat