Skip to content

Instantly share code, notes, and snippets.

@outsinre
Last active March 18, 2024 07:01
Show Gist options
  • Save outsinre/37dcac5b289f17511a1832903e901924 to your computer and use it in GitHub Desktop.
Save outsinre/37dcac5b289f17511a1832903e901924 to your computer and use it in GitHub Desktop.
Monitor Lua table access and update
-- table to monitor
ori_t = { a = "b" }
-- a proxy table - must be empty
proxy_t = {}
local mt
do
mt = {
__index = function (self, k)
print("*access to element " .. tostring(k))
return ori_t[k] -- access the original table
end,
__newindex = function (self, k, v)
print("*update of element " .. tostring(k) ..
" to " .. tostring(v))
ori_t[k] = v -- update original table
end
}
end
setmetatable(proxy_t, mt)
proxy_t[2] = 'hello'
print(proxy_t[2])
proxy_t["a"] = 'hello'
print(proxy_t["a"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment