Friday, October 31, 2008

Headius on FFI

As usual Headius is the shit

FFI stands for Foreign Function Interface. FFI has been implemented in various libraries; one of them, libffi, actually serves as the core of JNA, allowing Java code to load and call arbitrary C libraries. libffi allows code to load a library by name, retrieve a pointer to a function within that library, and invoke it, all without static bindings, header files, or any compile phase.

Take that SWIG! Although IIRC, .NET (including Mono) had some this sort of coolness as well.

To break it down

A simple FFI script calling the C "getpid" function:

require 'ffi'

module GetPid
extend FFI::Library

attach_function :getpid, [], :uint