[ANN] FFI 0.2.0

Discussion in 'Ruby' started by Wayne Meissner, Dec 3, 2008.

  1. 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 <>)

    - nil can be passed as a string argument (from Luc Heinrich
    <>)

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

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

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

    - 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 <>)
     
    Wayne Meissner, Dec 3, 2008
    #1
    1. Advertising

  2. On Wed, Dec 3, 2008 at 1:09 AM, 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
    >

    [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
     
    Sean O'Halpin, Dec 3, 2008
    #2
    1. Advertising

  3. Awesome, and thanks for the continued work in parallel on keeping
    JRuby's FFI feature-for-feature compatible :)

    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 <>)
    >
    > - nil can be passed as a string argument (from Luc Heinrich
    > <>)
    >
    > - FFI libraries can now be mixed in to other modules. (from Luc
    > Heinrich <>)
    >
    > - 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
    > <>)
    >
    > - 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 <>)
    >
    > - 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 <>)
    >
     
    Charles Oliver Nutter, Dec 3, 2008
    #3
  4. On Wed, Dec 3, 2008 at 3:42 AM, Charles Oliver Nutter
    <> 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? :)

    Regards,
    Sean
     
    Sean O'Halpin, Dec 3, 2008
    #4
  5. Sean O'Halpin wrote:
    > On Wed, Dec 3, 2008 at 3:42 AM, Charles Oliver Nutter
    > <> 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? :)


    RC tomorrow, final 1.1.6 sometime next week!

    - Charlie
     
    Charles Oliver Nutter, Dec 3, 2008
    #5
  6. Wayne Meissner

    marc Guest

    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
     
    marc, Dec 3, 2008
    #6
  7. 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
    :) 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:

    http://blog.headius.com/2008/10/ffi-for-ruby-now-available.html

    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!
     
    Charles Oliver Nutter, Dec 3, 2008
    #7
  8. 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 :) 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. ^_^
    > Currently, I don't understand how to use them.


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

    - Charlie
     
    Charles Oliver Nutter, Dec 3, 2008
    #8
  9. * Wayne Meissner <> [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
    --
     
    Saji N. Hameed, Dec 4, 2008
    #9
  10. Saji N. Hameed wrote:
    > * Wayne Meissner <> [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
     
    Charles Oliver Nutter, Dec 4, 2008
    #10
  11. Wayne Meissner

    Heesob Park Guest

    Re: FFI 0.2.0

    Charles Oliver Nutter wrote:
    > Saji N. Hameed wrote:
    >> * Wayne Meissner <> [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/.
     
    Heesob Park, Dec 4, 2008
    #11
  12. Re: FFI 0.2.0

    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
     
    Charles Oliver Nutter, Dec 4, 2008
    #12
  13. Wayne Meissner

    Luc Heinrich Guest

    On 4 d=E9c. 08, at 05:18, Charles Oliver Nutter 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 =20=

    other languages so the foundations are more solid, more tested, =20
    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.

    --=20
    Luc Heinrich -
     
    Luc Heinrich, Dec 4, 2008
    #13
  14. 2008/12/4 Charles Oliver Nutter <>:
    > 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.
     
    Wayne Meissner, Dec 4, 2008
    #14
  15. 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 :)

    www.jruby.org

    - Charlie
     
    Charles Oliver Nutter, Dec 4, 2008
    #15
  16. 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 <>)

    [...]

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

    --Greg
     
    Gregory Seidman, Dec 4, 2008
    #16
  17. 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 <>)

    > [...]
    >
    > 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 Nutter, Dec 4, 2008
    #17
  18. Charles Oliver Nutter wrote:
    > 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 <>)

    >> [...]
    >>
    >> 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
     
    Charles Oliver Nutter, Dec 4, 2008
    #18
  19. Wayne Meissner

    Luc Heinrich Guest

    On 4 d=E9c. 08, at 19:01, Gregory Seidman wrote:

    > I have been waiting for that feature. Is there a code example of =20
    > 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, =20
    2.5, :double, 1.5, :double, 4.0)

    --=20
    Luc Heinrich -
     
    Luc Heinrich, Dec 4, 2008
    #19
  20. Wayne Meissner

    Paul Brannan Guest

    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
     
    Paul Brannan, Dec 5, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles Oliver Nutter
    Replies:
    60
    Views:
    608
    Sean O'Halpin
    Dec 3, 2008
  2. Sean O'Halpin

    [ANN] ffi-ncurses version 0.3.0

    Sean O'Halpin, Feb 16, 2009, in forum: Ruby
    Replies:
    7
    Views:
    102
    Sean O'Halpin
    Feb 17, 2009
  3. Sean O'Halpin

    [ANN] ffi-ncurses version 0.3.1

    Sean O'Halpin, Feb 16, 2009, in forum: Ruby
    Replies:
    3
    Views:
    118
    Thomas Enebo
    Feb 17, 2009
  4. Andrea Fazzi

    [ANN] ffi-wiiuse 0.1.0

    Andrea Fazzi, Feb 16, 2009, in forum: Ruby
    Replies:
    1
    Views:
    97
    James Britt
    Feb 17, 2009
  5. John Croisant
    Replies:
    0
    Views:
    114
    John Croisant
    Oct 25, 2009
Loading...

Share This Page