lua-users home
lua-l archive

Re: Lua 5 getglobals

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


> 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?
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").
-- Roberto

AltStyle によって変換されたページ (->オリジナル) /