lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



Mark Hamburg wrote:

The first issue that leaps out at me is that TrackAll generates proxy tables
for subtables which it then throws away. You probably want to copy the
contents of table to be tracked over into a new table, empty the original
table, and use the original table as the proxy. (It would be nice if there
were a way to force Lua to shrink the original table after doing so.)

Thanks for help. I think I got things working the way I wanted.

Here is sample code of my table tracking (case somebody is interested):
local index = {}
local info  = {}
local mt

function Access(t, k)
  print('* Access to element ' .. tostring(k))

  return t[index][k]
end

function Assign(t, k, v)
  print('* Update of element ' .. tostring(k) .. ' to ' .. tostring(v))

  if type(v) == 'table' then
    TrackAll(v)

    if not getmetatable(v) then
      local tNew = v
      v = {}
      v[index] = tNew

      setmetatable(v, mt)
    end
  end

  t[index][k] = v
end

mt = {
  __index = Access,
  __newindex = Assign
}

function Track(t)
  local proxy = {}
  proxy[index] = t
  setmetatable(proxy, mt)

  return proxy
end

function TrackAll(t)
  for k, v in pairs(t) do
    if type(v) == 'table' then
      TrackAll(v)

      if not getmetatable(t[k]) then
        local tNew = t[k]
        t[k] = {}
        t[k][index] = tNew

        setmetatable(t[k], mt)
      end
    end
  end
end

-- Test
A = {}
A = Track(A)

A.B = {
  C = {
    D = 'd'
  }
}

print(A.B.C.D)
A.B.C.D = 'D'

--> * Update of element B to table: 0037A720
--> * Access to element B
--> * Access to element C
--> * Access to element D
--> d
--> * Access to element B
--> * Access to element C
--> * Update of element D to D


floru