lua-users home
lua-l archive

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

On Sat, May 19, 2012 at 03:25:04PM -0400, Dubiousjim wrote:
> I'm trying to understand the exact behavior of thread environments in
> Lua 5.1. (I am familiar with the changes introduced in 5.2.)
> >From the Lua 5.1 ref manual, I would expect the following to print
> false, however it prints true:
> flag = true
> local newG = { flag = false}
> local coroutine, print,setfenv = coroutine,print,setfenv
> coroutine.wrap(function()
>     setfenv(0, newG)
>     print(flag)
> end)()
> Can someone explain? And what could be written in place of `print(flag)`
> that would rely on newG for the lookup? getfenv(0) does return newG,
> but any functions or new coroutine calls I insert there still seem to be
> using the original _G.

I hadn't tried exactly this code, but was simplifying some other tests I
had run. Apparently, though, calling print will fail with an error if
tostring isn't visible in the active environment (even if print's
arguments are all strings). So you need to make newG = { flag = false,
tostring = tostring }.

But it still is printing true, contrary to what the documents and my
understanding of thread environments seems to imply.

Jim Pryor