Progress on GBoxed type mappings

August 1, 2009

I’ve mostly finished the hard parts of mapping GBoxed types (the test code is on gboxed branch of repository). Properly mapping foreign structure types requires a change in CFFI regarding callbacks. The new type mapping for boxed types will simplify implementation of interfaces and overriding methods and will make GObject binding more type-safe and more lispy.

When the callback defined with defcallback is called, CFFI uses the generic function translate-to-foreign to convert foreign values to Lisp values. So we can create Lisp structures from C structures or create instances of wrapper classes around opaque pointers.

Operations with wrappers involve calling foreign functions with the wrapped pointer. We want to be sure that we never call foreign functions with dangling or freed pointer. Often, callbacks are called with pointers to stack-allocated structures or with pointers to structures whose lifetime is short and does not extend beyond the callback. To ensure that operations on wrappers will not result in memory corruptions, wrapper should be invalidated when callback returns.

The other aspect is that callbacks are sometimes called with pointers to structures that should be modified by callbacks. For example, to implement GObject interface, various callbacks should be provided that correspond to methods of this interface. Method gtk_tree_model_get_iter_first of GtkTreeModel interface accepts a pointer to GtkTreeIter structure that will hold result. The most intuitive way of implementing the gtk_tree_model_get_iter_first method is to have a Lisp structure passed into the callback; then the method will modify the structure; and the structure modification are reflected in the foreign structure.

This requires being able to define actions that are performed on callback arguments when the callback finishes execution. I’ve written a patch that adds such support, but CFFI developers haven’t responded to it yet. I hope that the patch will eventually get merged into CFFI, otherwise I’ll have to make workarounds.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: