luatraverse.lua

Mon, 26 Oct 2009 02:26:39 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 26 Oct 2009 02:26:39 +0000
changeset 0
907015aa722f
child 1
036168493972
permissions
-rw-r--r--

Initial commit of luatraverse.lua by Alexandra Barros

0
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -------------------------------------------------------------------------------
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- This module implements a function that traverses all live objects.
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- You can implement your own function to pass as a parameter of traverse
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- and give you the information you want. As an example we have implemented
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- countreferences and findallpaths
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 --
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 -- Alexandra Barros - 2006.03.15
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 -------------------------------------------------------------------------------
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 module("gc", package.seeall)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local List = {}
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 function List.new ()
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 return {first = 0, last = -1}
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 function List.push (list, value)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local last = list.last + 1
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 list.last = last
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 list[last] = value
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 function List.pop (list)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local first = list.first
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if first > list.last then error("list is empty") end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local value = list[first]
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 list[first] = nil
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 list.first = first + 1
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 return value
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 function List.isempty (list)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 return list.first > list.last
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 -- Counts all references for a given object
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 function countreferences(value)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local count = -1
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local f = function(from, to, how, v)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 if to == value then
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 count = count + 1
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 traverse({edge=f}, {count, f})
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 return count
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 -- Prints all paths to an object
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 function findallpaths(obj)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local comefrom = {}
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 local f = function(from, to, how, value)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 if not comefrom[to] then comefrom[to] = {} end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 table.insert(comefrom[to], 1, {f = from, h = how, v=value})
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 traverse({edge=f}, {comefrom, f})
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local function printpath(to)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 if not to or comefrom[to].visited or to == _G then
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 print("-----")
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 return
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 comefrom[to].visited = true
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 for i=1, #comefrom[to] do
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 local tfrom = comefrom[to][i].f
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 print("from: ", tfrom, "\nhow:", comefrom[to][i].h,
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 "\nvalue:", comefrom[to][i].v)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 printpath(tfrom)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 printpath(obj)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 -- Main function
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 -- 'funcs' is a table that contains a funcation for every lua type and also the
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 -- function edge edge (traverseedge).
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 function traverse(funcs, ignoreobjs)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 -- The keys of the marked table are the objetcts (for example, table: 00442330).
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 -- The value of each key is true if the object has been found and false
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 -- otherwise.
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 local env = {marked = {}, list=List.new(), funcs=funcs}
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 if ignoreobjs then
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 for i=1, #ignoreobjs do
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 env.marked[ignoreobjs[i]] = true
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 env.marked["gc"] = true
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 env.marked[gc] = true
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 -- marks and inserts on the list
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 edge(env, nil, "_G", "isname", nil)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 edge(env, nil, _G, "key", "_G")
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 -- traverses the active thread
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 -- inserts the local variables
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 -- interates over the function on the stack, starting from the one that
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 -- called traverse
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 for i=2, math.huge do
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 local info = debug.getinfo(i, "f")
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 if not info then break end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 for j=1, math.huge do
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 local n, v = debug.getlocal(i, j)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 if not n then break end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 edge(env, nil, n, "isname", nil)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 edge(env, nil, v, "local", n)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 while not List.isempty(env.list) do
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 local obj = List.pop(env.list)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 local t = type(obj)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 gc["traverse" .. t](env, obj)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 function traversetable(env, obj)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 local f = env.funcs.table
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 if f then f(obj) end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 for key, value in pairs(obj) do
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 edge(env, obj, key, "iskey", nil)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 edge(env, obj, value, "key", key)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 local mtable = debug.getmetatable(obj)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 if mtable then edge(env, obj, mtable, "ismetatable", nil) end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 function traversestring(env, obj)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 local f = env.funcs.string
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 if f then f(obj) end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 function traverseuserdata(env, obj)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 local f = env.funcs.userdata
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 if f then f(obj) end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 local mtable = debug.getmetatable(obj)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 if mtable then edge(env, obj, mtable, "ismetatable", nil) end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 local fenv = debug.getfenv(obj)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 if fenv then edge(env, obj, fenv, "environment", nil) end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 function traversefunction(env, obj)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 local f = env.funcs.func
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 if f then f(obj) end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 -- gets the upvalues
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 local i = 1
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 while true do
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 local n, v = debug.getupvalue(obj, i)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 if not n then break end -- when there is no upvalues
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 edge(env, obj, n, "isname", nil)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 edge(env, obj, v, "upvalue", n)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 i = i + 1
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 local fenv = debug.getfenv(obj)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 edge(env, obj, fenv, "enviroment", nil)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 function traversethread(env, t)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 local f = env.funcs.thread
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 if f then f(t) end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 for i=1, math.huge do
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 local info = debug.getinfo(t, i, "f")
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 if not info then break end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 for j=1, math.huge do
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 local n, v = debug.getlocal(t, i , j)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 if not n then break end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 print(n, v)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 edge(env, nil, n, "isname", nil)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 edge(env, nil, v, "local", n)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 local fenv = debug.getfenv(t)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 edge(env, t, fenv, "enviroment", nil)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 -- 'how' is a string that identifies the content of 'to' and 'value':
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 -- if 'how' is "iskey", then 'to' é is a key and 'value' is nil.
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 -- if 'how' is "key", then 'to' is an object and 'value' is the name of the
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 -- key.
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 function edge(env, from, to, how, value)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 local t = type(to)
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 if to and (t~="boolean") and (t~="number") and (t~="new") then
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 -- If the destination object has not been found yet
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 if not env.marked[to] then
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 env.marked[to] = true
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 List.push(env.list, to) -- puts on the list to be traversed
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 local f = env.funcs.edge
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 if f then f(from, to, how, value) end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 end
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222
907015aa722f Initial commit of luatraverse.lua by Alexandra Barros
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 end

mercurial