lua-users home
lua-l archive

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


* On 2006-12-05 Jose Marin <jose_marin2@yahoo.com.br> wrote  :

> With Lua 5.1, is there some "proper" way of store
> references to user Lua functions, to be called later?
> 
> I know that this was already discussed, but it was 
> before the 5.1 version. I wonder if this version has a
> better solution to this problem.
>
> 1) provide a "SetOnKeyPressed" to Lua, and in the code
> it would be called like this:
> 
>    SetOnKeyPressed(OnKeyPressed)
> 
> Then, in the C main window callback, in the message
> WM_KEYDOWN, the function would be called, passing the
> key pressed code.
> 
> 2)provide the same SetOnKeyPressed function, but the
> user would pass a string, like this:
> 
> SetOnKeyPressed("OnKeyPressed")
> 
> Then, in the C main window callback, in the message
> WM_KEYDOWN, the function would be called, using the
> lua_getglobal function, passing the function name
> provided by the user. And then calling the function
> lua_pcall.
> 
> In Lua 5.1, what's the pro's and con's of these 2
> methods?

My $0.02: The pro of #2 would be that there's less fiddling around to
do in C, since you can store the name of the function in a regular
*char. The biggest - and I think unacceptable - con of #2 would be that
this will not work with anonymous functions, local functions or anything
which is not in _G.

> Is there a better way of do this?

I'm relatively new to Lua and to this list, so I'm not aware of what has
been discussed before. I solved this by creating a table that holds both
the callback function and some additional callback data, and putting
this table in the registry. It's a bit more work to do from C, but
results in much more flexibility.

-- 
:wq
^X^Cy^K^X^C^C^C^C