FFI 0.1.1 (Foreign Function Interface) for Ruby 1.8.6/7 and 1.9

On Wed, Nov 5, 2008 at 5:11 PM, Nit K. [email protected] wrote:

Eagerly awaiting your ncurses port…

I’m aiming to get a first cut out this week (just the very basics) to
get things moving, but it will take a while to get it all done,
especially the fiddly bits. I’m going to concentrate on the core
ncurses functionality. I’m not interested in panels or forms (better
done in Ruby IMHO).

Is it possible for you to document the procedure, how you go about doing
it, so one can follow that for another port, rather than be totally
lost.

Sure, as long as you don’t mind long-winded stream of consciousness :slight_smile:

Regards,
Sean

Sean O’Halpin wrote:

it, so one can follow that for another port, rather than be totally
lost.

Sure, as long as you don’t mind long-winded stream of consciousness :slight_smile:

Oh and please, please guys, update anything that’s not well-documented
here:

http://kenai.com/projects/ruby-ffi/pages/Home

It’s wide open…let me know if you can’t edit.

  • Charlie

Sean O’halpin wrote:

I’m aiming to get a first cut out this week (just the very basics) to
get things moving, but it will take a while to get it all done,
especially the fiddly bits. I’m going to concentrate on the core
ncurses functionality. I’m not interested in panels or forms (better
done in Ruby IMHO).

I am using panels and forms - heavily. I understand your last
statement to mean implementing p & f’s in ruby itself, avoiding what
ncurses provides. Has anyone actually done that – i would like to see
it and see how its better and if it can be reused.

This blog posting has the most information I have seen so far on FFI and
ruby

Nit K. wrote:

ARCH = case CPU.downcase
when /i?86|x86|i86pc|powerpc/
  "i386"
when /amd64|x86_64/
  "x86_64"

hmm… that code is broken. “x86_64” will match the first regex and
result in “i386”

Charles Oliver N. wrote:

Actually structs are already supported!

Sorry if you see this twice, posting difficulties.

I’m having a bash at wrapping freetds. This library
has a Context struct which contains three callback
function pointers. POLS says these should look like
this:

callback :handle_message, [ :pointer, :pointer, :pointer ], :in #
TDSCONTEXT, TSSSOCKET, TDSMESSAGE
callback :handle_int, [ :pointer ], :in # void*

class Context < FFI:Struct
layout
:locale, :pointer, 0, # TDSLOCALE
:parent, :pointer, 4, # void *
:msg_handler, :handle_message, 8, # callback(TDSCONTEXT,
TDSSOCKET, TDSMESSAGE)
:err_handler, :handle_message, 12, # callback(TDSCONTEXT,
TDSSOCKET, TDSMESSAGE)
:int_handler, :handle_int, 16 # callback(void*)
end

Am I on the right track, or is this not possible yet?

Clifford H…

Charles Oliver N. wrote:

There’s no support at the moment for callbacks-in-structs, but it’s just
a bug report (and ideally a patch) away :slight_smile:
http://kenai.com/projects/ruby-ffi

Thanks Charlie. I registered at Kenai.com and can log in there
though only by emaill address, username login doesn’t work), but
the Bugzilla instalce doesn’t recognise the login details at all.
I’ll try again later, in case there’s a delayed processing step.
I can’t see any separate registration for Bugzilla…

Clifford H…

Clifford H. wrote:

Charles Oliver N. wrote:

There’s no support at the moment for callbacks-in-structs, but it’s
just a bug report (and ideally a patch) away :slight_smile:
http://kenai.com/projects/ruby-ffi

Thanks Charlie. I registered at Kenai.com and can log in there
though only by emaill address, username login doesn’t work), but
the Bugzilla instalce doesn’t recognise the login details at all.
I’ll try again later, in case there’s a delayed processing step.
I can’t see any separate registration for Bugzilla…

I’ll make sure the Kenai guys know about the problem. Thanks for giving
it a shot. If nothing else, email the dev mailing list.

  • Charlie

On 11 nov. 08, at 06:27, Clifford H. wrote:

Thanks Charlie. I registered at Kenai.com and can log in there
though only by emaill address, username login doesn’t work)

Speaking of that Kenai thing, am I the only one who think it’s a
major pain in the derrière to use ?

Clifford H. wrote:

callback :handle_message, [ :pointer, :pointer, :pointer ], :in #
TDSSOCKET, TDSMESSAGE)
:int_handler, :handle_int, 16 # callback(void*)
end
Am I on the right track, or is this not possible yet?

There’s no support at the moment for callbacks-in-structs, but it’s just
a bug report (and ideally a patch) away :slight_smile:

http://kenai.com/projects/ruby-ffi

  • Charlie

Charles Oliver N. wrote:

Clifford H. wrote:

callback :handle_message, [ :poi…
class Context < FFI:Struct
layout
:msg_handler, :handle_message, 8,
There’s no support at the moment for callbacks-in-structs

While I’m on a roll, my example above creates a new type
(handle_message),
and the ability to define other kinds of types similarly would be
excellent.
For example, FreeTDS also has a string struct that contains a char* and
a
length, and it uses that type liberally. A typedef would rock, and the
above
example would just create a function-pointer typedef.

In a previous life, we implemented a generic structure pack/unpack
subsystem.
It had a table for every type containing the alignment and size of that
type.
Some architectures need more than one table depending on compile
options, but
we supported 30 different architectures using tables whose values were
filled
in by the C compiler. The rule of C don’t allow re-ordering structure
elements,
or at least, the require that two structures that start with the same
elements
use the same alignment for those elements.

You can discover the alignment of a type by declaring a struct z { char
x; TYPE y; }
and asking for offsetof(struct z, y); Mere byte-counting and padding
suffices
to calculate alignment for the members of any structure. Nested
structures are
padded to the largest of either the maximum alignment of the members, or
some
size specific to the architecture. This can also be populated into the
alignment
tables by the C compiler.

If FFI could include such a facility for Struct definitions, that would
be magic,
as the current system is almost impossible to use for cross-platform
support.
Unless you use the awkward templating system, of course… That requires
that
you have the development versions of the target libraries when you want
to gem
install a wrapper.

Just some random thoughts, anyhow. I might have time to contribute code
to this
effort in December.

Clifford H…

Clifford H. wrote:

Just some random thoughts, anyhow. I might have time to contribute code
to this
effort in December.

Sounds like you’ve dealt with a lot of this stuff before. Can you
re-post this to the main FFI mailing list?

  • Charlie

I get the following when I try to compile on windows - any
suggestions? Not sure what srcdir should be or where I should set it.

========================================================================

C:\ffi>gem install ffi-0.1.0.gem
Building native extensions. This could take a while…
ERROR: Error installing ffi-0.1.0.gem:
ERROR: Failed to build gem native extension.

C:/ruby/bin/ruby.exe extconf.rb install ffi-0.1.0.gem
creating Makefile

nmake

Microsoft ® Program Maintenance Utility Version 9.00.30729.01
Copyright © Microsoft Corporation. All rights reserved.

makefile(182) : fatal error U1052: file ‘$(srcdir)/ffi.mk’ not found
Stop.

Gem files will remain installed in C:/ruby/lib/ruby/gems/1.8/gems/
ffi-0.1.0 for inspection.
Results logged to C:/ruby/lib/ruby/gems/1.8/gems/ffi-0.1.0/ext/
gem_make.out

brett wrote:

I get the following when I try to compile on windows - any
suggestions? Not sure what srcdir should be or where I should set it.

There needs to be a pre-built gem for win32, but we haven’t published
one yet. I think someone’s working on getting it wired up.

  • Charlie

On Nov 11, 5:53 am, Luc H. [email protected] wrote:

On 11 nov. 08, at 06:27, Clifford H. wrote:

Thanks Charlie. I registered at Kenai.com and can log in there
though only by emaill address, username login doesn’t work)

Speaking of that Kenai thing, am I the only one who think it’s a
major pain in the derrière to use ?

Nop, count me on that too.

Tried to use bugzilla to provide some patches, and is a no go, only
fall back to mailing list seems the alternative.

Very cool!

I don’t know how I missed this before, somehow slipped past my mental
filter (and now everything you were talking about at RubyConf starts to
make a lot more sense… :).

One question: a complaint I’ve had with DL is that it requires that I
specify the dynamic library that contains symbols I’m interested in. Is
this necessary with FFI? Can FFI be used to call functions in the Ruby
standard library when Ruby is not build with --enable-shared?

If so, I’d be interested in using this for Ludicrous to avoid having to
hard-code all the function pointers to the Ruby API functions I want to
call. That would mean, though, I would need FFI to be able to give me a
real function pointer (so I can pass the pointer to libjit… I can
imagine this being useful for other cases as well).

Paul

On Wed, Nov 5, 2008 at 5:11 PM, Nit K. [email protected] wrote:

Eagerly awaiting your ncurses port…

Is it possible for you to document the procedure, how you go about doing
it, so one can follow that for another port, rather than be totally
lost.

Hi,

I’ve just uploaded the ~very raw~ initial port of the core ncurses lib
to ruby-ffi. You can find it at:

git://github.com/seanohalpin/ffi-ncurses.git

This works with ffi-0.0.2 (which I got from the kenai project) - 0.0.1
left out typedefs for short ints.

To use:

require ‘rubygems’
require ‘ffi’
require 'ffi-ncurses

notes:

  • example.rb shows basic usage
  • to access stdscr, use the return value from NCurses.initscr
  • I have not implemented any variadic functions (not supported by libffi
    AFAIK)
  • there’s also a wrapper for libdl called dlfcn.rb
  • there’s an experimental script (ex01-ffi-dl-stdscr.rb) which uses
    dlfcn.rb to access the exported variables from the ncurses shared lib
    (stdscr, curscr, etc.). This also shows how to access values returned
    through pointers (see NCurses.wattr_get).

There are some rambling notes on implementation in
notes-on-ffi-curses.org. I’ll tidy these up when I get some spare
time. Basically, I wrote a script
(generate-ffi-ncurses-function-signatures.rb) that mechanically
recovered the function signatures from the ncurses.h on my machine and
added some constants. I wasted a lot of time figuring out how to
access exported variables.

I’ve only just done a final test on OS X Tiger - haven’t checked Linux
yet (should work but…)

I have not set up a project for this yet so please send any bug
reports, questions, etc. directly to me for the moment.

There are no tests - I wonder if anyone has any ideas how best to
write specs/tests for checking this kind of output?

Regards,
Sean

I tried to run all examples but no one worked. It prints tons of:
/usr/lib/ruby/gems/1.8/gems/ffi-0.1.1/lib/ffi/ffi.rb:232: warning:
instance variable @ffi_convention not initialized

and last line:
example-printw-variadic.rb:7: undefined method `printw’ for
NCurses:Module (NoMethodError)


Pozdrawiam

Rados³aw Bu³at
http://radarek.jogger.pl - mój blog

On Tue, Nov 18, 2008 at 11:56 PM, Sean O’Halpin [email protected]
wrote:

my wrapper requires ffi-0.2.0
I notice I got the version number wrong in my earlier post (I said
0.0.2) - apologies for any confusion caused.

Regards,
Sean

2008/11/18 Rados³aw Bu³at [email protected]:

Rados³aw Bu³at
http://radarek.jogger.pl - mój blog

Hi Rados³aw,

my wrapper requires ffi-0.2.0 which you’ll need to install from
source. You can get the source from the kenai project site at
http://kenai.com/projects/ruby-ffi (though you’ll need to install
mercurial to get it).

The example ex01-ffi-dl-stdscr.rb does not currently work under Linux

  • I’m looking into that. The only platform I know it works on is Mac
    OS X Tiger. The other examples work on both OS X and Linux (with
    ffi-0.2.0).

Regards,
Sean