lua-users home
lua-l archive

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


I've also been pretty confused on what _G does specifically.

The docs are a little unclear about its role, and the book Programming
in Lua (third edition) isn't much more clear. The docs for _G says
"Lua itself does not use this variable; changing its value does not
affect any environment, nor vice-versa." From this, it's not clear to
me what operations do and do not affect it.

It also says in section 2.2, "Lua keeps a distinguished environment
called the global environment. This value is kept at a special index
in the C registry (see §4.5). In Lua, the variable _G is initialized
with this same value." This makes it clear to me that _G just
references a table somewhere in LUA_REGISTRYINDEX, at least I think
so.

But then this next part of the manual confuses me: "When Lua compiles
a chunk, it initializes the value of its _ENV upvalue with the global
environment (see load). Therefore, by default, global variables in Lua
code refer to entries in the global environment." -- Does this mean we
can assume that the literal code "_ENV = _G" is being executed, and
thus _ENV is by default literally just another reference to _G?

Assuming that's true, this part makes sense to me: "If you change the
global environment in the registry (through C code or the debug
library), all chunks loaded after the change will get the new
environment. Previously loaded chunks are not affected, however, as
each has its own reference to the environment in its _ENV variable."
because _ENV is an upvalue referencing the table that was previously
also referenced by _G (i.e. a special spot in LUA_REGISTRYINDEX).

And if it's true, then I /think/ I understand the final sentence of
section 2.2: "Moreover, the variable _G (which is stored in the
original global environment) is never updated by Lua." -- so if the
variable _G is just a reference to the same table that exists in
LUA_REGISTRYINDEX, then this implies that all operations /on/ this
table will affect "the global environment", whereas changing what this
variable itself points to will do nothing, since Lua does not actually
reference a global called "_G" in its code.

Is this the correct interpretation? If so, the manual could definitely
use some clarifications here, ideally relying on familiar terminology
that's not so ambiguous.

Disclaimer: I'm pretty sure I understand how _ENV works perfectly
fine, my email is only about _G, unlike the original email. The manual
and Programming in Lua (third edition) were very clear and elaborated
at great length about how _ENV works and how to use it.

On Mon, Aug 11, 2014 at 11:39 AM, Mason Mackaman <masondeanm@aol.com> wrote:
> So I was reading the reference manual to try to understand what role _G plays in a Lua program and I’m not having much luck. Adding an element to _G adds it to _ENV, but setting _G=nil doesn’t delete _ENV, so I know they aren’t referencing the same thing. If you do take the time to help this noob out, could you please include in your answer the importance having both _ENV and _G, thanks.