[ann] 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 Katz
Luc Heinrich
Andrea Fazzi
Mike Dalessio
Hongli Lai

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 Heinrich <luc@honk-honk.com>)

- nil can be passed as a string argument (from Luc Heinrich
<luc@honk-honk.com>)

- FFI libraries can now be mixed in to other modules. (from Luc
Heinrich <luc@honk-honk.com>)

- 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 Lai
<hongli@phusion.nl>)

- 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 Fazzi <andrea.fazzi@alcacoop.it>)

- 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 Dalessio)

- Library handle caching (from Luc Heinrich <luc@honk-honk.com>)

[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

···

On Wed, Dec 3, 2008 at 1:09 AM, Wayne Meissner <wmeissner@gmail.com> 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

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?

Wayne Meissner 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

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 Katz
Luc Heinrich
Andrea Fazzi
Mike Dalessio
Hongli Lai

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 Heinrich <luc@honk-honk.com>)

- nil can be passed as a string argument (from Luc Heinrich
<luc@honk-honk.com>)

- FFI libraries can now be mixed in to other modules. (from Luc
Heinrich <luc@honk-honk.com>)

- 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 Lai
<hongli@phusion.nl>)

- 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 Fazzi <andrea.fazzi@alcacoop.it>)

- 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 Dalessio)

- Library handle caching (from Luc Heinrich <luc@honk-honk.com>)

Wayne Meissner 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.

···

--
Cheers,
Marc

* Wayne Meissner <wmeissner@gmail.com> [2008-12-03 10:09:03 +0900]:

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

···

--

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:

[...]

- variadic function support (from Luc Heinrich <luc@honk-honk.com>)

[...]

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

--Greg

···

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

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 `<main>'

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

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

···

On Wed, Dec 3, 2008 at 3:42 AM, Charles Oliver Nutter <charles.nutter@sun.com> wrote:

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

marc wrote:

Wayne Meissner 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.

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!

beoran@gmail.com 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

Saji N. Hameed wrote:

* Wayne Meissner <wmeissner@gmail.com> [2008-12-03 10:09:03 +0900]:

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

Gregory Seidman wrote:

···

On Wed, Dec 03, 2008 at 10:09:03AM +0900, Wayne Meissner 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:

[...]

- variadic function support (from Luc Heinrich <luc@honk-honk.com>)

[...]

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

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)

···

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

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

--
Luc Heinrich - luc@honk-honk.com

Sean O'Halpin wrote:

···

On Wed, Dec 3, 2008 at 3:42 AM, Charles Oliver Nutter > <charles.nutter@sun.com> 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:

RC tomorrow, final 1.1.6 sometime next week!

- Charlie

Charles Oliver Nutter wrote:

Saji N. Hameed wrote:

* Wayne Meissner <wmeissner@gmail.com> [2008-12-03 10:09:03 +0900]:

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 Heesob

···

--
Posted via http://www.ruby-forum.com/\.

* 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.

···

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

--
Luc Heinrich - luc@honk-honk.com

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

···

2008/12/4 Charles Oliver Nutter <charles.nutter@sun.com>:

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)

Charles Oliver Nutter wrote:

Gregory Seidman 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:

[...]

- variadic function support (from Luc Heinrich <luc@honk-honk.com>)

[...]

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

Better URL (still learning hg myself):

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

···

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

Heesob Park 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

beoran@gmail.com 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