[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: C function replacing global environment - why is calling scopeunaffected?
- From: "Wim Couwenberg" <wcou@...>
- Date: Mon, 14 Jun 2004 09:29:30 +0200
> If it bothers you though, it's easy enough to redefine loadstring to
> use the caller environment.
> do
> local ls = loadstring
> function loadstring(s, n)
> return setfenv(ls(s, n), getfenv(2))
> end
> end
(note: setfenv does not return its first argument, in contrast to
setmetatable)
It is not so easy: try the following example
do
local ls = loadstring
function loadstring(s, n)
local c = ls(s, n)
setfenv(c, getfenv(2))
return c
end
end
function oops()
print "testing my loadstring..."
return loadstring "print(100)"
end
oops()()
--
Wim