lua-users home
lua-l archive

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

2012/12/11 Philipp Kraus <>:
> 4   number 123
> 3   string "test"
> 2   boolean "true"
> 1   lua function
> and on the item 1 it would read the name of the function. In my case the Lua
> function and the arguments of the function are pushed by the routines on
> program top, so in the cases there are different Lua function.
> If I iterate on a stack (eg above), I would get only the function name back.
> I can determine that a stack item is a lua function (or a C function) but
> can I get a reference of the name of the function? I push it also with
> a name to the stack, so somewhere must be stored that the item n is a lua
> function with a name or is the stack item of a lua function a pointer to the
> a memory block?

The stack slot 1 is only pointing at the function, it doesn't hold the
name you used to push it there. You can still try to call lua_getinfo
on it. To do that on a function on the C stack, first push the
function on the top of the stack (lua_pushvalue(L, 1) in your case),
then call lua_getinfo with a "what" string that start with the
character ">". See the lua_getinfo manual for more details.

This doesn't guarantee that you will get a function name. I'm not
familiar enough with getinfo to tell the circumstances where you will
or where you won't. I think that usually when you get a function name
in a trace report, the name is infered from the call point, not the
function definition point, so there might be a chance that you cannot
get a proper name in your situation (I don't know if the functions
debug data carry the name used when they were defined). However you
will get a script name and a line number of here the function was
defined, so you can point your user there at the very least.