lua-users home
lua-l archive

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


Hi list !

I am experimenting with the _ENV value. From my understanding, _ENV = xxx is now a special lua construct:

1. normal construct

name = 'xxx'  --- _ENV.name = 'xxx'

2. special _ENV construct

_ENV = {}   --- replace _ENV in place

My question is: since the new _ENV is not assigned to any preexisting environment (it is not _ENV._ENV), is it correct to say that it is assigned to the chunk's environment, replacing the previous value ? But then I would expect values in the previous _ENV to be garbage collected but they are not. Any hint ?

=============================== test.lua (5.2)
local print = print
local collectgarbage = collectgarbage
local gc_mt = {}
function gc_mt.__gc(obj)
  print('gc', obj.name)
end

foo = {name = 'foo'}
setmetatable(foo, gc_mt)

_ENV = {}
print(foo)     ---- nil as expected

collectgarbage() ---- does not collect old "foo". Why ? Where is it stored ?

print('end') -- the garbage collection after 'end' removes old "foo".
===============================

Gaspard