lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Philippe Lhoste <> wrote:

[arbitrary calls from C]
>I also though of this, but the problem, as stated in the given article, is
>that arguments have various sizes. Chars are mapped to 4 bytes, so are ints
>of course, but floats and structures are bigger.

This can be handled in C.  To pass a double arg, i.e. an 8-byte arg, you
can pass in 2 longs instead.  It gets messy but it's still very much C:
    double x = 123.456;
    long arg1 = ((long*) &x)[0];
    long arg2 = ((long*) &x)[1];
Then pass in arg1 and arg2.  You can do this for anything, as long as you
know its size and know how alignment and byte order are dealt with.

C's "varargs" mechanism is full of this sort of old and ugly trickery.

As I said, it works for me in Lux, using code which came from Minotaur,
so let me plug those two URLs - with a warning that neither is packaged
for the casual user:

As a general approach, I'd suggest looking at some of the following:
    libffi -
    ffcall -
    ffidl -
    calldll -
The "ff" stands for "foreign functions".  Ffidl is a wrapping of either
of the other low-level libraries into the Tcl scripting language. 
Calldll is for Python.

It's not Christmas time, but here's a wish I'd love to see granted: the
emergence of an extension for Lua, which wraps libffi.  Add it to your
Lua environment, and voila - instant access to all shared libs / dlls, on
a range of platforms.

AFAIK, Squeak ( also integrated libffi, giving it the
ability to use lots of shared-lib code.  A good solution would go a long
way in not having to ever write (nor compile/link!) new C code,
especially for "just" wrapping existing code.  Or requesting OS services.