Ffi 0.2.0

Greetings Rubyists.

The JRuby team is proud to announce the release of 0.2.0 of FFI for
Ruby. This release is compatible with the FFI implementation released
in JRuby 1.1.6

Get it via ‘gem install ffi’ or download the source and/or gem files
from the project page at http://kenai.com/projects/ruby-ffi

Special thanks to:

Yehuda K.
Luc Heinrich
Andrea F.
Mike D.
Hongli L.

Highlights of changes since 0.1.1:

  • About 25% faster function invocation. This is probably not that
    important, since as soon as you start putting significant ruby code
    around any native call, the native call overhead becomes a bit of a
    wash. Still, the first thing people do is benchmarks, so speeding it
    up a bit was probably worth it.

  • type definitions for size_t, ssize_t, etc autogenerated when ffi gem
    is installed.

  • variadic function support (from Luc H. [email protected])

  • nil can be passed as a string argument (from Luc H.
    [email protected])

  • FFI libraries can now be mixed in to other modules. (from Luc
    Heinrich [email protected])

  • uses system libffi if it is new enough. This really only works
    under very recent linux distributions and libffi from macports.

  • Better library name mangling. Now when an absolute path is set as
    ffi_lib, no name mangling occurs. Also on Linux, if you request ‘c’
    or ‘libc.so’ as the library, it is converted into ‘libc.so.6’

  • Multiple libraries can be specified. This is once again, useful on
    linux, where you have to specify the exact version of a library, but
    you want it to work on other platforms too. e.g. ffi_lib ‘ncurses’,
    ‘libncurses.so.5’

  • Many, many more specs. Most specs now run against the included
    libtest native library. It is still a long way from 100% test
    coverage, but its better than 0.1 was.

  • Better tainted string handling. All strings originating in native
    code - i.e. returned from a function, or obtained from a pointer or
    buffer via get_string() are tainted. If a tainted string is passed to
    a native function, an error is raised.

  • automatic Struct layout. This means you can specify the layout as
    an array of name, type pairs and the offset and size will be
    automatically calculated.

  • Fixed specs and rbxspecs rake targets (Patch from Hongli L.
    [email protected])

  • FFI.errno will now return the errno set by the last native function
    call

  • Memory allocated for Buffer and MemoryPointer is guaranteed to be at
    least 8 byte aligned.

  • Integer types are all range checked. i.e. if you try and pass a
    value > 127 as a :char parameter, it will raise an error.

  • Add compat.h header to deal with ancient (i.e. < 1.8.6) versions of
    ruby

  • Add missing :float and :double Struct member support (Patch from
    Andrea F. [email protected])

  • Reworked get_string and put_string to only handle NUL terminated ascii
    strings, and create get_bytes/put_bytes for binary data.

  • Per-module/library type definitions/aliases.

  • Custom managment of Pointer & Struct lifecycle (from Mike D.)

  • Library handle caching (from Luc H. [email protected])

On Wed, Dec 3, 2008 at 1:09 AM, Wayne M. [email protected]
wrote:

Greetings Rubyists.

The JRuby team is proud to announce the release of 0.2.0 of FFI for
Ruby. This release is compatible with the FFI implementation released
in JRuby 1.1.6

[snip list of great new features]

FFI is just getting better and better. Thanks for this Wayne and
everyone else who has contributed to it.

Regards,
Sean

Awesome, and thanks for the continued work in parallel on keeping
JRuby’s FFI feature-for-feature compatible :slight_smile:

Anyone released any notable FFI-based replacements for C extensions yet?
Sean? ffi-ncurses up for a release soon?

Sean O’Halpin wrote:

As the lib needs features from ffi 0.2.0, it won’t work with JRuby
1.1.5 (but hey, 1.1.6 is just around the corner, no? :slight_smile:

RC tomorrow, final 1.1.6 sometime next week!

  • Charlie

Wayne M. said…

Greetings Rubyists.

The JRuby team is proud to announce the release of 0.2.0 of FFI for
Ruby. This release is compatible with the FFI implementation released
in JRuby 1.1.6

Get it via ‘gem install ffi’ or download the source and/or gem files
from the project page at http://kenai.com/projects/ruby-ffi

Just a suggestion, but I think it would be worth adding a note about
what FFI is.

On Wed, Dec 3, 2008 at 3:42 AM, Charles Oliver N.
[email protected] wrote:

Anyone released any notable FFI-based replacements for C extensions yet?
Sean? ffi-ncurses up for a release soon?

Hi Charles,

I reckon I’ll get a first cut at a gem out this week (even though it’s
by no means complete).

As the lib needs features from ffi 0.2.0, it won’t work with JRuby
1.1.5 (but hey, 1.1.6 is just around the corner, no? :slight_smile:

Regards,
Sean

marc wrote:

Just a suggestion, but I think it would be worth adding a note about
what FFI is.

Here’s the text of my 0.1.1 announcement, which ought to clear things up
:slight_smile: Perhaps the main paragraph should be included in future release
announcements too?


The JRuby team is proud to announce the release of FFI for Ruby 1.8.6/7
and 1.9!

FFI (gem install ffi) is a library for programmatically loading dynamic
libraries, binding functions within them, and calling those functions
from Ruby code. Here’s a quick sample of binding and calling the getpid
C library function:

require ‘ffi’

module GetPid
extend FFI::Library

attach_function :getpid, [], :uint
end

puts GetPid.getpid

Here’s another, calling qsort and passing a Ruby block as a C callback:

require ‘ffi’

module LibC
extend FFI::Library
callback :qsort_cmp, [ :pointer, :pointer ], :int
attach_function :qsort, [ :pointer, :int, :int, :qsort_cmp ], :int
end

p = MemoryPointer.new(:int, 2)
p.put_array_of_int32(0, [ 2, 1 ])
puts “Before qsort #{p.get_array_of_int32(0, 2).join(', ')}”
LibC.qsort(p, 2, 4) do |p1, p2|
i1 = p1.get_int32(0)
i2 = p2.get_int32(0)
i1 < i2 ? -1 : i1 > i2 ? 1 : 0
end
puts “After qsort #{p.get_array_of_int32(0, 2).join(', ')}”

I posted a blog entry with a longer description of the library,
additional examples, and links to some other documentation and posts.
Docs are a little slim at this point, so feel free to experiment and
update the JRuby wiki page:

I’m sure docs from here will filter back into the library and out into
the general cosmos.

Finally, there’s no need to write a C extension to call C libraries, and
the same FFI code will work in Ruby 1.8.6/7, Ruby 1.9, JRuby 1.1.4+, and
Rubinius (though Rubinius has no callback support yet).

Don’t be an extension stooge! Use FFI!

Saji N. Hameed wrote:

Greetings Rubyists.

The JRuby team is proud to announce the release of 0.2.0 of FFI for
Ruby. This release is compatible with the FFI implementation released
in JRuby 1.1.6

Just started learning about Ruby/DL - what are the most significant
advantages of FFI over Ruby/DL ?

  • FFI works on JRuby and Rubinius as well as MRI and 1.9
  • The API is nicer (imho)

Wayne may have some other points…maybe it’s faster?

  • Charlie

Charles Oliver N. wrote:

Saji N. Hameed wrote:

Greetings Rubyists.

The JRuby team is proud to announce the release of 0.2.0 of FFI for
Ruby. This release is compatible with the FFI implementation released
in JRuby 1.1.6

Just started learning about Ruby/DL - what are the most significant
advantages of FFI over Ruby/DL ?

  • FFI works on JRuby and Rubinius as well as MRI and 1.9
  • The API is nicer (imho)

Wayne may have some other points…maybe it’s faster?

  • Charlie
    I want to use FFI instead of Win32API or win32-api.

But I cannot install FFI on Windows.

What’s the plan for Windows support?

Regards,

Park H.

Greetings Rubyists.

The JRuby team is proud to announce the release of 0.2.0 of FFI for
Ruby. This release is compatible with the FFI implementation released
in JRuby 1.1.6

Just started learning about Ruby/DL - what are the most significant
advantages of FFI over Ruby/DL ?

saji

Heesob P. wrote:

I want to use FFI instead of Win32API or win32-api.

But I cannot install FFI on Windows.

What’s the plan for Windows support?

I think we need help getting a build made, but once we have it we have
it. Help us? Join the ML at http://kenai.com/projects/ruby-ffi

  • Charlie

[email protected] wrote:

I’m currently seting up a rubyforge project for the SDL, SGE,
SDL_image, SDL_mixer, SDL_ttf and SDL_mixer
bindings I wrote called Frusdl, short for FFI Ruby SDL. I’ll probably
release the 0.0.1 version when the project is
approved by the Rubyforge admins.

Ahh, now that sounds cool :slight_smile: I hope you’ll have some small demo apps to
go with it.

For advancing Frusdl, I could probably use a few pointers on how to
implement callbacks, if you forgive the pun. :slight_smile:
Currently, I don’t understand how to use them.

There should be an example of calling qsort in the gem, does that help?

  • Charlie

2008/12/4 Charles Oliver N. [email protected]:

Saji N. Hameed wrote:

Just started learning about Ruby/DL - what are the most significant
advantages of FFI over Ruby/DL ?

  • FFI works on JRuby and Rubinius as well as MRI and 1.9
  • The API is nicer (imho)

Those are the major user-visible reasons. There are other reasons,
like the ones Luc mentioned.

On 4 déc. 08, at 05:18, Charles Oliver N. wrote:

  • FFI works on JRuby and Rubinius as well as MRI and 1.9
  • The API is nicer (imho)
  • It’s actually supported and evolving.
  • It’s based on libffi which is already used in similar situations in
    other languages so the foundations are more solid, more tested,
    support more platform and are more likely to get fixes and improvements.

Wayne may have some other points…maybe it’s faster?

About 4 times faster than DL in my tests.

Gregory S. wrote:

I have been waiting for that feature. Is there a code example of setting up
a variadic function somewhere?

There’s a pretty good collection of specs; that’s probably your best bet
for examples right now, until users start adding examples to the wiki.

https://kenai.com/hg/ruby-ffi~mercurial/file/9132d4854b79/specs/variadic_spec.rb

  • Charlie

Charles Oliver N. wrote:

[…]
https://kenai.com/hg/ruby-ffi~mercurial/file/9132d4854b79/specs/variadic_spec.rb
Better URL (still learning hg myself):

https://kenai.com/hg/ruby-ffi~mercurial/file/tip/specs/variadic_spec.rb

On 4 déc. 08, at 19:01, Gregory S. wrote:

I have been waiting for that feature. Is there a code example of
setting up
a variadic function somewhere?

Here’s a very simply one:

require “rubygems”
require “ffi”

module Test
extend FFI::Library
attach_function :printf, [:string, :varargs], :int
end

Test.printf(“The answer is %d\n”, :int, 42)
Test.printf(“The sum of %.1f and %.1f is %.1f\n”, :double,
2.5, :double, 1.5, :double, 4.0)

[email protected] wrote:

My Rubyforge project request was accepted. You can find all about
Frusdl 0.0.1 here:
http://frusdl.rubyforge.org/

It’s yet untested on jruby, as I’m waiting for the 1.16 release. And
it’s meagerly documented,
no unit test, etc, etc, but I’m sticking to “release early, release
often here”. I hope it’s
of interest or use to someone out there.

JRuby 1.1.6RC1 is out, so now’s a great time to try it :slight_smile:

  • Charlie

I’m getting an error trying to install the gem for ffi 0.2.0. Am I
doing something wrong?

[pbrannan@zem rubygems-1.3.1]$ sudo gem install ffi
Building native extensions. This could take a while…
ERROR: Error installing ffi:
ERROR: Failed to build gem native extension.

rake RUBYARCHDIR=/usr/local/lib/ruby/gems/1.8/gems/ffi-0.2.0/lib
RUBYLIBDIR=/usr/local/lib/ruby/gems/1.8/gems/ffi-0.2.0/lib
internal:gem_prelude:236:in push_gem_version_on_load_path': undefined method<=>’ for nil:NilClass (NoMethodError)
from internal:gem_prelude:10:in gem' from /usr/local/bin/rake:18:in

Gem files will remain installed in
/usr/local/lib/ruby/gems/1.8/gems/ffi-0.2.0 for inspection.
Results logged to
/usr/local/lib/ruby/gems/1.8/gems/ffi-0.2.0/gen/gem_make.out
/usr/local/lib/ruby/1.8/rdoc/parsers/parse_c.rb:204: warning: method
redefined; discarding old progress
[pbrannan@zem rubygems-1.3.1]$ gem --version
1.3.1

On Wed, Dec 03, 2008 at 10:09:03AM +0900, Wayne M. wrote:

Greetings Rubyists.

The JRuby team is proud to announce the release of 0.2.0 of FFI for
Ruby. This release is compatible with the FFI implementation released
in JRuby 1.1.6
[…]
Highlights of changes since 0.1.1:

[…]

I have been waiting for that feature. Is there a code example of setting
up
a variadic function somewhere?

–Greg