Skip to content

Instantly share code, notes, and snippets.

@prafulliu
Created December 18, 2012 07:16
Show Gist options
  • Save prafulliu/4325769 to your computer and use it in GitHub Desktop.
Save prafulliu/4325769 to your computer and use it in GitHub Desktop.
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