[Nickle] Currency conversion (foreign function interface)
Keith Packard
keithp at keithp.com
Mon Dec 6 00:23:25 PST 2004
I want to build a cairo interface in nickle, but I don't want nickle to be
always tied to X, cairo and a million other libraries.
So, the obvious solution was to construct some kind of foreign function
interface.
I've got something working, but wanted to see if anyone had ideas about
how it should really work before I commit to CVS.
Because foreign libraries often include a wealth of new datatypes, I'd
like to hook them into the nickle memory allocator so that they get
destroyed when nickle is done with them. I created a new primitive
datatype "foreign" which references a single datum. Each foreign object
has a 'free' function and an 'id', which is a string that the foreign
library can use to do run-time typechecking. I'd love static
typechecking, but I'm not sure how to manage that. Suggestions for this
are welcome.
Adding foreign functions was actually pretty easy; use dlopen to load a
shared library, then use dlsym to locate "nickle_init" and call that.
nickle_init is expected to use the existing Builtin* calls to create
and populate a namespace. Of course, the functions must all be nickle
wrappers around the native ones.
I'm currently using RTLD_LAZY, which avoids requiring all symbols be
resolved, but runs the risk of blowing up the whole nickle session if an
undefined symbol is ever used. RTLD_NOW would switch that around; I'm not
sure that's better though.
I tested things by making a tiny Xlib FFI; the three functions I added in
that "work", but it's tedious to do a lot of calls this way. Clearly some
more efficient mechanism is required. In an ideal world, we'd have a tool
that took an existing .so/.h pair and constructed a wrapper .so that called
everything in the .h with appropriate type and range checked arguments.
-keith
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
Url : /pipermail/nickle/attachments/20041206/26584bc1/attachment.pgp
More information about the Nickle
mailing list