Creates a new version of a table that prints setting (__newindex
) and getting (__index
) operations to the log, that is table = value
and value = table
. This can help you identify why a value in your table has vanished or appeared. May not work if the table already has a metatable.
-- unique keys
local inner_key = function() end
local name_key = function() end
local function write_key(self, k)
return (rawget(self, name_key) or "") .. ""
end
local mt = {
__newindex = function(self, k, v)
mw.logObject(v, "setting " .. write_key(self, k))
rawset(rawget(self, inner_key), k, v)
end,
__index = function(self, k)
local v = rawget(rawget(self, inner_key), k)
mw.logObject(v, "getting " .. write_key(self, k))
return v
end,
__pairs = function(self)
return pairs(self)
end,
__ipairs = function(self)
return ipairs(self)
end,
}
return function(t, name)
return setmetatable({ = t, = name }, mt)
end