Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
tracking table acceses in lua
local index = {}
-- create private index
local mt = {
__index = function ( t, k )
print("*access to element " .. tostring(k))
return t[index][k] --access the original table
end,
__newindex = function ( t, k, v)
print("*update of element " .. tostring(k) ..
" to " .. tostring(v))
t[index][k] = v
end
}
function track( t )
local proxy = {}
proxy[index] = t
setmetatable(proxy, mt)
return proxy
end
tab = track(tab)
tab[2] = "hello"
print(tab[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment