[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: about luaffi
- From: James McKaskill <james@...>
- Date: Thu, 13 Sep 2012 19:08:32 -0400
On Thu, Sep 13, 2012 at 6:57 PM, Martin Krpan <wtxnh-lua@yahoo.com.au> wrote:
> Thank you, that helped! I also had to use explicit cast in next line:
> local res = zlib.compress2(buf, buflen, ffi.cast('const unsigned char*', txt), #txt, 9)
> otherwise I got an error:
> lua: unable to convert argument 3 from lua<string> to cdata<const unsigned char*>
luaffi treats strings as const char* not const unsigned char*. This
might be on my todo list to fixup - can't remember though.
> If I try to pass number to that function I get error:
> dp = voidp(2) -- This is line 32
> lua: unable to convert argument 2 from lua<number> to cdata<pointer>
> stack traceback:
> [C]: in function 'voidp'
> testphysfs.lua:32: in main chunk
> [C]: ?
You need to either cast through a uintptr_t (which I doubt you want)
or create a ffi.new('int[1]', 2).
>
> For table the error is:
> dp = voidp({x=10; y=20}) -- This is line 32
> lua: testphysfs.lua:32: Can't set a pointer member to a struct that's about to be freed
> stack traceback:
> [C]: in function 'voidp'
> testphysfs.lua:32: in main chunk
> [C]: ?
>
> How can I pass table (for example) to my Lua callback function?
You need to create a struct e.g. ffi.new('struct {int x; int y;}',
{x=2, y=3}). Otherwise the ffi has no idea how to marshal it. I must
admit the error could be better. It's actually erroring on a different
problem that comes up when you try and set a pointer directly to a
table, the ffi struct will be immediately gc'd.
> And how do I dereference void* pointer back to lua table in callback function?
You cast back to a ffi struct and then manually unpack.
-- James