lua-users home
lua-l archive

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

W. C. Bubel wrote:
> I have a question about making calls from C into Lua functions. The
> manual is quite clear on what to do when you know the identifier of
> the function, however I'm wondering if the function itself (a
> pointer) can be sent to the C side and somehow stored to be called at
> a later time.    
> Specifically, I'm wondering if this syntax is possible, assuming
> setcallback is a C Function: 
> setcallback( function() ... end )
> Is there a specific provision for this, that I overlooked, or would
> trick need to be involved? 

You cannot store a pointer to a Lua closure (an anonymous function).
However you can store a reference to it in a table, such as the globals,
the registry, or the environment/metatable of one of your userdata.
There are functions in Lua aux library to easily create such references:
luaL_ref, luaL_unref. These functions return an integer. You can then
store the integer along with the lua_State pointer in a small structure
to find and call the callback.