FFI and Ruby 1.9.2 |
engineering.attinteractive.com
engineering.attinteractive.com
Though it might be just me, being spoiled by ActiveRecord, but wouldn't it be awesome to just instantiate an object that automatically reads the exported functions in a library and makes them available to Ruby? Like what ActiveRecord does with database tables.
I guess if no one else thinks of it I could do it sometime.
Thankfully, someone's done a great part of the work. MacRuby and PyObjC need to be able to call C functions, so Apple threw together BridgeSupport: http://bridgesupport.macosforge.org/trac/wiki . It uses the clang front-end to parse headers, and outputs an XML file with the function info. It would be really cool if you could do this on non-Mac platforms, presumably it wouldn't be terribly much work...
The tricky part as I see it would be to identify the parameter types, which could be done with some sort of heuristic that analyses the functions called on each parameter, or more easily by parsing a header file provided with the library.
Then you would have a tool that takes in a header and a library and spits out a class that wraps that library automatically. However it wouldn't be nearly as clean, transparent, or neat as what ActiveRecord does. Still would be a useful tool to have.
Having an FFI like this in a language is a really nice feature.