lua-users home
lua-l archive

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

On Thu, 2008-08-07 at 07:55 -0400, Bryan White wrote:
> I think that globals should be handled differently.  Perhaps there 
> should be an internal 'C' array of globals, maybe just pointers to the 
> internals of the _G table.  Then compiled references to said globals 
> would just contain indexes into the array.

But then they wouldn't work, because the global table --- the current
environment --- can be *any* table, complete with a metatable. It's
entirely possible to do something like this:

local print = print
local t = {}
setmetatable(t, {__index = function(t, k) return k end })
setfenv(0, t)

=> foo

(Don't do that from the Lua shell, it'll do really weird things.)

The compiler can't optimise the reference to foo in the call to print(),
because it has no way of knowing what weird semantics the global
environment table has attached to it; that's one of the things that
makes Lua so powerful.

David Given