The module allows you to interface between Pure and C functions (both
directions) without writing a single line of C code. In particular, you
can turn a Pure function into a C function pointer which can then be
passed as a callback to another C function. C structs are also fully
supported, including the ability to construct and inspect such values
and pass them (by value) to C functions in a type-safe manner.
More information here:
http://pure-lang.googlecode.com/svn/trunk/pure-ffi/README
Source tarball here:
http://pure-lang.googlecode.com/files/pure-ffi-0.1.tar.gz
libffi can be found here (but most gcc-based distros already have it):
http://sourceware.org/libffi/
I forgot to mention this in the README, but you'll need a fairly recent
svn version of Pure in order to use this, or you'll have to wait until
pure-0.17 comes out.
This is the very first release and I've only tested this on Linux so
far, so your bug reports are appreciated. ;-)
Enjoy!
Albert
PS1: Scott, you can find the quicksort example redone with libffi in the
examples subdir of the pure-ffi sources. I think that this should be
good enough to implement the callbacks needed by OpenGL, please let me
know if there's anything missing.
PS2: Eddie, you will probably find it useful that pure-ffi also enables
you to call C functions involving ISOC (or GSL) complex numbers being
passed by value; see the last example in the README.
PS3: Toni, I'm cc'ing you because I hope that you'll consider packaging
this as an addon module. It compiles without hitches on SUSE 11.1 (just
make && make install, no configure), the only dependencies are pure and
the libffi43 from OpenSUSE. It does require a fairly recent svn version
of Pure though, but this won't be problem when pure-0.17 comes out.
--
Dr. Albert Gr"af
Dept. of Music-Informatics, University of Mainz, Germany
Email: Dr.G...@t-online.de, a...@muwiinfa.geschichte.uni-mainz.de
WWW: http://www.musikinformatik.uni-mainz.de/ag
... and version 0.2 is out already. Some minor bugfixes, long double
support, and additional struct operations.
http://pure-lang.googlecode.com/files/pure-ffi-0.2.tar.gz
Albert
P.S.: Libor, I don't recall off hand whether your calendar script uses
some broken-down time functions. If so then you might want to take a
look at pure-ffi/examples/time.pure. Maybe it would be useful to adapt
some of the date/time routines in system.pure accordingly?
Yes, I see that you're using gmtime in examples/libor/date.pure and have
to decode the information because the gmtime wrapper in system.pure
returns a string.
I think it would be more useful to have localtime and gmtime in
system.pure return the broken-down time representation as a pointer,
which could then either be passed to asctime or strftime to format it as
a string, or be decoded using the pure-ffi struct functions.
In fact, it might be useful to have pure-ffi included in the main
distribution.
Opinions?
Albert
Include it in the main distribution because:
1. Just about every add-on module is going to need this anyway.
2. It is kind of odd to have most of the support for C built in but have a
separate library to deal with structs and some other pointer issues.
e.r.
Well, the separate library is useful anyway because it offers a
lightweight capability to create both Pure-callable C functions and
C-callable Pure functions dynamically, without the need to generate and
compile any Pure or C code.
It's conveivable to also add the struct and C callback features to Pure
itself, but this would need additional declaration syntax for C struct
and function parameters. I'm not sure whether I want to pollute Pure
with that. ;-) And I'm not sure whether I want the core of the
interpreter to depend on libffi. It seems to be available for most
Unix-like systems, but AFAICT there are no ready-made Windows packages
so I'll first have to see whether I can get it to work there.
I didn't understand the issues and tradeoffs. Sorry.
> It seems to be available for most
> Unix-like systems, but AFAICT there are no ready-made Windows packages
> so I'll first have to see whether I can get it to work there.
I keep forgetting about Windoze ;-)
Probably because I sleep better at night when I don't have to deal with M$ stuff ;-)
e.r.
In fact it's even simpler than that. Broken-down time is just a pointer
to a struct with nine ints, which can readily be translated to a matrix
using 'int_matrix 9', no libffi required.
Ok, I rewrote those now (r690). Your date script is already adapted
accordingly. The differences aren't big, see the ChangeLog for details.
As a pleasant side-effect, the time functions in system.pure now really
work the POSIX way. :)
Gee, that is Libor's date.pure script of course. I shouldn't be talking
to myself late at night. :)