1 -------------------------------------------------------------------------------
2 -- This module implements a function that traverses all live objects.
3 -- You can implement your own function to pass as a parameter of traverse
4 -- and give you the information you want. As an example we have implemented
5 -- countreferences and findallpaths
7 -- Alexandra Barros - 2006.03.15
8 -------------------------------------------------------------------------------
10 module("traverse", package.seeall)
15 return {first = 0, last = -1}
18 function List.push (list, value)
19 local last = list.last + 1
24 function List.pop (list)
25 local first = list.first
26 if first > list.last then error("list is empty") end
27 local value = list[first]
29 list.first = first + 1
33 function List.isempty (list)
34 return list.first > list.last
37 -- Counts all references for a given object
38 function countreferences(value)
40 local f = function(from, to, how, v)
45 traverse({edge=f}, {count, f})
50 -- 'funcs' is a table that contains a funcation for every lua type and also the
51 -- function edge edge (traverseedge).
52 function traverse(funcs, ignoreobjs)
54 -- The keys of the marked table are the objetcts (for example, table: 00442330).
55 -- The value of each key is true if the object has been found and false
57 local env = {marked = {}, list=List.new(), funcs=funcs}
60 for i=1, #ignoreobjs do
61 env.marked[ignoreobjs[i]] = true
65 env.marked["traverse"] = true
66 env.marked[traverse] = true
68 -- marks and inserts on the list
69 edge(env, nil, "_G", "isname", nil)
70 edge(env, nil, _G, "value", "_G")
72 -- traverses the active thread
73 -- inserts the local variables
74 -- interates over the function on the stack, starting from the one that
78 local info = debug.getinfo(i, "f")
79 if not info then break end
81 local n, v = debug.getlocal(i, j)
82 if not n then break end
84 edge(env, nil, n, "isname", nil)
85 edge(env, nil, v, "local", n)
89 while not List.isempty(env.list) do
91 local obj = List.pop(env.list)
93 _M["traverse" .. t](env, obj)
99 function traversetable(env, obj)
101 local f = env.funcs.table
104 for key, value in pairs(obj) do
105 edge(env, obj, key, "key", nil)
106 edge(env, obj, value, "value", key)
109 local mtable = debug.getmetatable(obj)
110 if mtable then edge(env, obj, mtable, "ismetatable", nil) end
114 function traversestring(env, obj)
115 local f = env.funcs.string
120 function traverseuserdata(env, obj)
121 local f = env.funcs.userdata
124 local mtable = debug.getmetatable(obj)
125 if mtable then edge(env, obj, mtable, "ismetatable", nil) end
127 local fenv = debug.getfenv(obj)
128 if fenv then edge(env, obj, fenv, "environment", nil) end
132 function traversefunction(env, obj)
133 local f = env.funcs.func
139 local n, v = debug.getupvalue(obj, i)
140 if not n then break end -- when there is no upvalues
141 edge(env, obj, n, "isname", nil)
142 edge(env, obj, v, "upvalue", n)
146 local fenv = debug.getfenv(obj)
147 edge(env, obj, fenv, "environment", nil)
151 function traversethread(env, t)
152 local f = env.funcs.thread
155 for i=1, math.huge do
156 local info = debug.getinfo(t, i, "f")
157 if not info then break end
158 for j=1, math.huge do
159 local n, v = debug.getlocal(t, i , j)
160 if not n then break end
163 edge(env, nil, n, "isname", nil)
164 edge(env, nil, v, "local", n)
168 local fenv = debug.getfenv(t)
169 edge(env, t, fenv, "environment", nil)
174 -- 'how' is a string that identifies the content of 'to' and 'value':
175 -- if 'how' is "key", then 'to' is a key and 'name' is nil.
176 -- if 'how' is "value", then 'to' is an object and 'name' is the name of the
178 function edge(env, from, to, how, name)
182 if to and (t~="boolean") and (t~="number") and (t~="new") then
183 -- If the destination object has not been found yet
184 if not env.marked[to] then
185 env.marked[to] = true
186 List.push(env.list, to) -- puts on the list to be traversed
189 local f = env.funcs.edge
190 if f then f(from, to, how, name) end