[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: C function replacing global environment - why is calling scope unaffected?
- From: Jamie Webb <j@...>
- Date: Mon, 14 Jun 2004 02:26:20 +0100
On Monday 14 June 2004 01:13, Bruce Dodson wrote:
> OK, I think I follow that. And in the following case, when
> the new chunk is loaded, it gets the global table (which has
> changed) rather than inheriting the environment of the
Yes, because loadstring is a C function and so uses the global table. In
general, the environment of the caller is irrelevant to the environment of
the called function; it's where the function is defined that counts (modulo
If it bothers you though, it's easy enough to redefine loadstring to use the
caller environment. Just be aware that the resulting behaviour is not very
nice: if you start calling loadstring indirectly via functions with different
environments, you're probably going to confuse yourself.
local ls = loadstring
function loadstring(s, n)
return setfenv(ls(s, n), getfenv(2))
> Seems like this could cause some wierd things to happen.
> Anything further to help qualify this as documented /
> undocumented / bug / feature?
It's kind of implied, but the manual could certainly be more clear about the
whole issue of function environments.
-- Jamie Webb