lua-users home
lua-l archive

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

On 7 May 2013 10:01, Dirk Laurie <> wrote:
2013/5/7 Geoff Leyland <>:

>> function a() print(debug.getinfo(2, "n").name) end

I can't believe I missed that, so I looked carefully at what I did.
Relative to your example, the minimal difference is this:

    function a() print(debug.getinfo(2, "n").name) end
    function b() return a() end

It is instructive to work out the reason why in this case there
is no name.

Quoting the manua [1]:

name: a reasonable name for the given function. Because functions in Lua are first-class values, they do not have a fixed name: some functions can be the value of multiple global variables, while others can be stored only in a table field. The lua_getinfo function checks how the function was called to find a suitable name. If it cannot find a name, then name is set to NULL.

The problem in this case is the tailcall in function b(). The tail call removes the activation record for b() and replaces it with activation record for a(), so the information on how the function was called is lost.

A simple change to b() that gets rid of the tailcall will yield the desired results:

function b() return (a()) end