lua-users home
lua-l archive

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

--- In lua-l@y..., "Chris Tavares" <ctavares@d...> wrote:
> Well, the problem is that you MUST use assembly to do this sort of 
thing -
> higher level languages don't give you enough control over the stack 
> registers to have any hope of setting up a call properly.

I once wrote a FFI for Tinyscheme that basically went like this:

1. Allocate a char buffer -- arg_buf[256], say.
2. Copy the arguments into this buffer, allowing for word-alignment. 
For instance for foreign function foo(int a, float b) do a memcpy
(arg_buf, &a, sizeof(int)) followed by a memcpy(arg_buf + sizeof
(int), &b, sizeof(float)).
3. Get the function pointer to foo and call int result = foo(arg_buf).

Amazingly enough it worked in all cases I bothered to test. I still 
have the source code lying around, and can post it if anyone's 
interested, but the underlying idea's pretty simple.

-- Elliott