Created
December 18, 2012 07:16
-
-
Save prafulliu/4325769 to your computer and use it in GitHub Desktop.
tracking table acceses in lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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