lua-users home
lua-l archive

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


On Tue, May 8, 2012 at 2:02 AM, Tezduyar Lindskog, Umut
<Umut.TezduyarLindskog@sonymobile.com> wrote:
> Hi all,
>
>
>
> Is it possible to store lua function in a C struct and later call lua
> function from C using the stored information?
>
>
>
> I am looking for an implementation as following:
>
>
>
> void* myluafunc = luaL_checkluafunction (L, 1);
>
> lua_pushfunctionptr (L, myluafunc);
>
> lua_call (L,0,0);
>
>
>
> I have implemented a workaround where lua function's name is stored in a
> char array in C and then later on lua function is called using following
> APIs. This way is working fine but using function name as the identifier is
> using extra memory.
>
>
>
> char name[128];
>
> int button_lua_new (lua_State *L)
>
> {
>
>    const char* myluafuncname = luaL_checkstring (L,1);
>
>    strcpy (name, myluafuncname);
>
> }
>
> void call_button_event_handler ()
>
> {
>
>   lua_getglobal(L, name);
>
>   lua_call (L,0,0);
>
> }
>
>
>
> Thanks

You can't store the function itself (you can dump it to a string, but
that's not reliable), but you can store a reference. Look at luaL_ref.

-- 
Sent from my Game Boy.