Skip to content

Instantly share code, notes, and snippets.

@akirayu101
Created December 2, 2014 11:00

Revisions

  1. akirayu101 created this gist Dec 2, 2014.
    57 changes: 57 additions & 0 deletions hook_tb.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@

    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