lua-users home
lua-l archive

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



Hi.

I am still trying to get tracking of table's access working. My previous question is in here:

http://lua-users.org/lists/lua-l/2004-05/msg00120.html

I'm currently trying to add tracking to all new subtables in __newindex metamethod:

local index = {}

local Access, Assign, TrackAll, mt

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

  t = Track(t)
end

function Access(t, k)
  print('*Access', tostring(t), tostring(k))

  return t[index][k]
end

function Assign(t, k, v)
  print('*Assign', tostring(t), tostring(k), tostring(v))

  if type(v) == 'table' then
    TrackAll(v)
  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

A = {}
A = Track(A)

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

A.B.C.D.E = 'e'

--> *Assign table: 006F32C8 B       table: 006F2088
--> *Access table: 006F32C8 B


But as you can see tracking is not working. When function "Assign" has following form I get better results:

function Assign(t, k, v)
  print('*Assign', tostring(t), tostring(k), tostring(v))

  if type(v) == 'table' then
    v = Track(v)
  end

  t[index][k] = v
end

--> *Assign table: 006F50B8 B       table: 006F5138
--> *Access table: 006F50B8 B
--> *Access table: 006F3E88 C


When I modify function "Assign" like this:

function Assign(t, k, v)
  print('*Assign', tostring(t), tostring(k), tostring(v))

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

    for _k, _v in pairs(v) do
      if type(_v) == 'table' then
        _v = track(_v)
      end
    end
  end

  t[index][k] = v
end

--> *Assign table: 006F9EE8 B       table: 006F9F68
--> *Access table: 006F9EE8 B
--> *Access table: 006F8B90 C

The results are still the same. So why can't I add tracking to subtables in __newindex metamethod? Or why can't I use a function like in first example?

floru