luajit-ffi

James Miller james at aatch.net
Wed May 2 19:23:26 PDT 2012


Just pitching in as somebody that has worked on a few C bindings, 
some small some big (current one is XCB, which has the lovely 
xproto.h file coming to over 15000 lines). Doing automatic 
conversion of C headers, properly, is difficult, if not 
impossible. There are all sorts of gotchas and strange things 
that make trying to write bindings difficult to automate.

A lot of APIs rely on the CPP, as part of the API itself. This is 
an issue because you generally need to replicate those parts of 
the API in some D way, without completely mangling the original 
API, generally you want to be able to copy-and-paste C code and 
have it work as D code.

Not all defines can, or should, be converted to enums, even if 
you distinguish macros and constants. Not all typedefs can be 
converted to aliases. Different libraries use different ways of 
typedef'ing their structs and the like. Each library needs a 
specific touch to make the D bindings as true to the original as 
possible, and a tool cannot do that well.

Manually writing bindings is boring, but also quite medatitive. 
It is simple enough that you can just shut off, or you can make 
it a challenge to find the best macro for converting a certain 
construct in this library. But it is generally not that difficult.

The difference between using bindings and FFI is that bindings 
are compile time and you get all the advantages from that, such 
as type checking and the like. FFI is all runtime, so you don't 
get that, unless you do some crazy CTFE magic.

--
James Miller


More information about the Digitalmars-d mailing list