lua-users home
lua-l archive

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


On 22 March 2012 21:58, Cosmin Apreutesei <cosmin.apreutesei@gmail.com> wrote:
> I noticed strange behavior with debug.getinfo(1).name, in particular,
> it doesn't get set if the current function f (for which you want to
> get the name for) was called with return f() instead of just f(). Eg.:
>
> function f()
>        local t,kt={},{}
>        for k,v in pairs(debug.getinfo(1)) do t[k]=v end
>        for k in pairs(t) do table.insert(kt,k) end
>        table.sort(kt)
>        for _,k in ipairs(kt) do print(k,t[k]) end
> end
>
> function g() f() end
> function h() return f() end
>
> g()
> print('-------')
> h()
>
>
> The above gets the name of f if called from g() but not from h() -
> happens with both 5.1 and 5.2; luajit2 even finds the name to be 'h'
> in the second case which is even more misleading.
>
> So how can one implement a simple argcheck'ing function with this mess? :)
>

The difference between the calls is the second is a tail call and that
information is not available after the call yet it is before. So you
can set a debug hook for 'c'[1][2]

[1] http://www.lua.org/manual/5.1/manual.html#pdf-debug.sethook
[2] this is for Lua 5.1 yet I also assume it holds for 5.2 although
there were some changes for tail calls in 5.2 but IIRC this was only
in respect of reporting a return from a tail call.

Liam