lua-users home
lua-l archive

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


On 1/25/08, Mike Pall <mikelu-0801@mike.de> wrote:
> E. Wing wrote:
> > I've been trying to use the Lua debug library to tell me what the
> > current function name is. But I keep getting a blank/empty value. I
> > had done this before with Lua 5.0, so I have created a simple test
> > case. It seems that I do get the function name under Lua 5.0.3, but
> > not under Lua 5.1.2.
>
> Both Lua 5.0 and 5.1 try to infer function names for the debug
> library by symbolic execution of Lua code in the previous
> (calling) frame. Basically this works its way backwards from the
> function slot to find a name for its origin. This works fine in
> most cases, but not in yours, because you are calling it from C
> code (i.e. no guess possible).
>
> In this case only Lua 5.0 does some additional (expensive)
> traversal of the globals table to find a matching entry. This
> happens to work in your case because "f" is a global.
>
> The extra code was dropped in Lua 5.1. Probably because it's both
> expensive and useless in the general case. Most library functions
> are in extra tables and any reasonably complex program does not
> store functions in the globals table (hopefully).
>
> [Please remember that debug.getinfo can be quite slow and is only
> intended for debugging. It's not a programming technique.]


Thanks for the reply. It's unfortunate for me that this doesn't work
any more. I certainly found it useful and it seemed general case for
me.

The original way I used it was for an error/logging system. I had a
main C-based engine that allowed scripting. The API was 'open' for
people to write/add their own scripts. When an error occurred, I would
use the Lua C debug API to get the lua function name and line number
and spit it out to a error logging mechanism also in the engine. I
also exposed my own 'print' (logging) function that could be used for
debugging and it would automatically include the function
name/debugging info. In my design, it is not necessarily uncommon to
be in a top-level C-called-Lua-function. The design was akin to an
event driven model where a Lua function could be used to override some
default behavior for an event. Different entities got their own Lua
states with their own scripts, so I never saw the need to nest the
event functions in tables  and getglobal is an easy way to call
things. (It worked in 5.0 so I didn't think it would be a problem.)

Since these were only triggered on error conditions, debug.getinfo
would only be called in limited situations (basically for the exact
purpose of debugging) so performance was not an issue.

So is my only recourse to put everything in a table? Can you show me
how I should modify the 'f' example to do this 'correctly'?

Thanks,
Eric