lua-users home
lua-l archive

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


Wim Couwenberg:
> In a particular function I want to retrieve the globals table of the
> _calling_ function, so I do a getglobals(2).  (I want to "switch" to the
> caller's context.)  However, sometimes this does _not_ return the callers
> globals!  After some fiddling I tracked  the problem down to Lua's use of
> optimized tail calls.  Because tail calls reuse a stackframe instead of
> creating a new one, a getglobals(n) will effectively skip all tail calls
> and end up at a higher level than expected.
>
> Is there a way around this, or is there another (better) way to get the
> callers globals table?

Roberto Ierusalimschy:
> There is no way around this. The essence of proper tail call is to keep
> no information about a function that does a tail call. However, we can
> keep a counter, so that Lua will know about the tail calls, and will
> keep stack levels correct. Then, a call to "getglobals(n)" where "n" did
> a tail call may at least raise an error (avoiding the "fiddling").

Is this "getglobals" of a *number* a good idea? It sounds rather dangerous
(especially since it is prey to optimisation). =:-O

How far back does one normally look on the call stack for globals???

Would people not just be better off passing in the global table that they
wanted? Eg:
    MyFunc(arg1,arg2,getglobals())
or does this not work for some reason?

This "getglobals" of a number makes me nervous :-O.

*cheers*
Peter Hill.

Ki: Contractors... high-paid leeches stealing our work.
Fooker: If you think of them as disposable employees, you'll feel much
better...