lua-users home
lua-l archive

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


Thank you all, I was suspecting it was an optimization. Unfortunately I'd have preferred the behavior of LuaJIT or Lua5.1, but it's also because I'm in a deviant case (use of debug.setmetatable). Here is a simplified version of what I'm doing, just to give you the true motivation of my question:

local proxy = {}

local mt = {
    __index = function(self, key)
        return proxy[self][key]
    end,
    __newindex = function(self, key, value)
        if not proxy[self] then
            proxy[self] = {}
        end
        proxy[self][key] = value
    end,
}

debug.setmetatable(function()end, mt)

local funcs = {}
for i=0,5 do
    local f = function() end
    f.att = i
    table.insert(funcs, f)
end

print(funcs[1].att) -- I'd expect 0
print(funcs[2].att) -- I'd expec 1

I think I'll try to convert my functions to tables with metamethod __call instead.

2014-09-15 23:39 GMT+02:00 Enrico Colombini <erix@erix.it>:
On 15/09/2014 22.09, Coda Highland wrote:
I assume this is to minimize the amount of state stored in the closure.

I just noticed that (i.e. that only referenced upvalues are stored) while doing some check to see exactly what was visible to whom in a program of mime.
In retrospect it's obvious, to avoid filling every function with all file-local variables as upvalues.

--
  Enrico