Toxic Elephant

Don't bury it in your back yard!

GirFFI - An Introduction

Posted by matijs 10/05/2011 at 07h09

Over two years ago, I had the idea, that it should be possible to combine two great technologies, ruby-ffi, and GObject Introspection, to dynamically create bindings for GLib-based libraries.

This idea, like many, was born from frustration: The development of Ruby-GNOME2 is labour-intensive, and therefore, it lags behind the development of Gnome libraries. In particular, I wanted to use the Gio library, which had no bindings at the time, to fetch generated icons for images.

Serious development started in october 2009, with a basic proof-of-concept to show that it was at least possible to use FFI to bind Gtk+.

About a year later, GirFFI 0.0.1 was finally released as a gem. Several more releases followed, and now, with release 0.0.9, it seems be nearly feature-complete.

So, what can GirFFI do for you?

Given any GLib-based library with GObject Introspection data, it generates bindings for that library. These bindings are generated dynamically at runtime, meaning that methods and classes are not generated until first use.

Because GirFFI uses FFI to call into the C libraries, it is not tied to a particular Ruby, but is known to work with MRI 1.8.7 and 1.9.2, and with JRuby 1.6.1. Support for Rubinius is planned.

GirFFI supports both Gtk+ 2, and the new Gtk+ 3. You can choose between them by specifying the version when you set up the bindings.

The bindings GirFFI generates are less Ruby-like than those of Ruby-GNOME2. For example, it uses the standard method names provided by the library, and doesn’t try to change, e.g., get_name and set_name to name and name=. This also means it is not a drop-in replacement for Ruby-GNOME2.

Of course, some work remains to be done.

GirFFI is currently very conservative when it comes to freeing allocated memory. As a result, it leaks memory. There are tools to at least test this, but I have no experience using these. Help would definitely be appreciated.

Also, documentation is rather lacking. In particular, there’s not much information on how to translate from the C function calls described in the Gtk+ documentation to the Ruby method calls needed for GirFFI. Ideally, GirFFI could generate Ruby-oriented documentation straight from the GObject-introspection data.

Finally, there are certain functions that are deemed ‘unintrospectable’ by GObject Introspection. These include functions taking varargs or generic pointers. These will need to be hand-bound. GirFFI includes several such hand-bound methods, but the set is far from complete.

Do give GirFFI a try. The gem’s name is gir_ffi, and you can fork the code on GitHub. Comments, bug reports, and pull requests are all welcome.

Tags no comments no trackbacks

Comments

Comments are disabled