JRuby access to C libraries using FFI
02.09.2008 – 10:59With the new release of JRuby 1.1.4 it is possible to use the rubinius FFI API. With FFI you can access C libraries via plain ruby code. To get started with FFI the only documentation I have found is provided by Free Minds blog entry. Additional info may be posted to the discussion using jruby-ff. Hopefully gem authors start to implement extensions with FFI besides their native extensions to make it possible to use the gem with Rubinius and JRuby.
One problem is to provide ruby callback functions for the C library. I am trying to figure out how to extend sqlite3-ruby to work with jruby. Sqlite3-ruby uses swig to create a native binding to the sqlite3 library. Within driver.rb some callbacks are registered for the C code like:
API.sqlite3_busy_handler( db, API::Sqlite3_ruby_busy_handler, cb )
API::Sqlite3_ruby_busy_handler is a callback implemented in the native extension. FFI does not provide any support for callbacks. To support C APIs that use callbacks a workaround must be found.