lua-users home
lua-l archive

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


2012/3/13 Rena <hyperhacker@gmail.com>:
> On Mon, Mar 12, 2012 at 19:02, Xavier Wang <weasley.wx@gmail.com> wrote:
>> i have a similar problem, that is there are only one void poiner argument to
>> callback, but i need pass two (lua_State and userdata),,. :(
>>
>
> You can't store Lua functions themselves in your C library, but you
> can store references using luaL_ref(). So if the function you get is a
> Lua function (see lua_iscfunction()), store a reference to it.
>
> Or was the problem something else? I'm not clear exactly what you're
> having trouble with.
>
> --
> Sent from my toaster.
>

say, we have a function:
void foo_register(foo* f, foo_callback cb, void *ud);
which foo_callback is:
typedef void (*foo_callback)(void *ud);

but, if I want call lua function in the foo_callback in my binding
library, first I need to know lua state, second I need a function, but
I can only got one void *ud, so how can I pass both lua_State* and ref
to function in one void *ud parameter?

So the answer maybe this: think this lua code:
local f = foo()
f:register(function()end)

f is a userdata, I put a lua_State* in this userdata, and pass f to
void *ud, and get functions from f (maybe from f's env table).