Skip to content

Instantly share code, notes, and snippets.

@akirayu101
Created December 2, 2014 11:00
Show Gist options
  • Save akirayu101/0ea40660b89a0af7f128 to your computer and use it in GitHub Desktop.
Save akirayu101/0ea40660b89a0af7f128 to your computer and use it in GitHub Desktop.
require "src/modules/traceback.lua"
hook_tb = {}
local function hook_index(o, key)
local function_name, filename, line = traceback.getsource(4)
logger:fatal("access hook_tb in funtion[%s] filename[%s] line[%d]", function_name, filename, line)
if type(o["__data"][key]) ~= "table" then
return o["__data"][key]
else
local tb = hook_tb:create()
tb["__data"] = o["__data"][key]
return tb
end
end
local function hook_newindex(o, key, value)
local function_name, filename, line = traceback.getsource(4)
logger:fatal("modify hook_tb in funtion[%s] filename[%s] line[%d]", function_name, filename, line)
print("set", key, value)
o["__data"][key] = value
end
function hook_tb:create()
local tb = {}
tb["__data"] = {}
setmetatable(tb,{__index = hook_index,__newindex = hook_newindex })
return tb
end
function hook_tb:create_from_native_table(native_table)
local tb = hook_tb:create()
tb['__data'] = native_table
return tb
end
function hook_test()
t = hook_tb:create()
print(t.hello)
t.hello = 1
t.hello = {1,2,3}
print(t.hello[1])
local st = t.hello
print(st[1])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment