luatraverse.lua

Wed, 23 Sep 2020 14:25:20 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 23 Sep 2020 14:25:20 +0100
changeset 13
9bd292b35f23
parent 12
e3e3cbe544ec
permissions
-rw-r--r--

Add dump.lua to write state graphs to file

1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
1 -------------------------------------------------------------------------------
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
2 -- This module implements a function that traverses all live objects.
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
3 -- You can implement your own function to pass as a parameter of traverse
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
4 -- and give you the information you want. As an example we have implemented
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
5 -- countreferences and findallpaths
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
6 --
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
7 -- Alexandra Barros - 2006.03.15
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
8 -------------------------------------------------------------------------------
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
9
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
10 --luacheck: ignore 143/debug 113/getfenv
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
11
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
12 local List = {}
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
13
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
14 function List.new ()
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
15 return {first = 0, last = -1}
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
16 end
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
17
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
18 function List.push (list, value)
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
19 local last = list.last + 1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
20 list.last = last
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
21 list[last] = value
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
22 end
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
23
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
24 function List.pop (list)
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
25 local first = list.first
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
26 if first > list.last then error("list is empty") end
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
27 local value = list[first]
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
28 list[first] = nil
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
29 list.first = first + 1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
30 return value
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
31 end
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
32
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
33 function List.isempty (list)
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
34 return list.first > list.last
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
35 end
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
36
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
37 local traverse, edge;
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
38 local types = {};
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
39 local get_metatable, get_environment, get_registry, get_locals, upvalues
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
40
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
41
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
42 -- select implementation of get_metatable depending on available API
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
43 if type( debug ) == "table" and
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
44 type( debug.getmetatable ) == "function" then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
45
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
46 local get_mt = debug.getmetatable
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
47 function get_metatable( val, enabled )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
48 if enabled then return get_mt( val ) end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
49 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
50
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
51 elseif type( getmetatable ) == "function" then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
52
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
53 function get_metatable( val, enabled )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
54 if enabled then return getmetatable( val ) end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
55 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
56
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
57 else
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
58
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
59 function get_metatable() end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
60
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
61 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
62
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
63
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
64 -- select implementation of get_environment depending on available API
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
65 if type( debug ) == "table" and
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
66 type( debug.getfenv ) == "function" then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
67
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
68 local get_fe = debug.getfenv
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
69 function get_environment( val, n, enabled )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
70 if enabled and n == 1 then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
71 local uv = get_fe( val )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
72 return uv, uv ~= nil
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
73 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
74 return nil, false
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
75 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
76
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
77 elseif type( debug ) == "table" and
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
78 type( debug.getuservalue ) == "function" then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
79
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
80 local get_uv = debug.getuservalue
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
81 if _VERSION < "Lua 5.4" then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
82 local get_uv_noindex = get_uv
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
83 function get_uv( val, n )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
84 if n == 1 then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
85 local uv = get_uv_noindex( val )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
86 return uv, uv ~= nil
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
87 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
88 return nil, false
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
89 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
90 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
91 function get_environment( val, n, enabled )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
92 if enabled then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
93 -- getuservalue in Lua 5.2 throws on light userdata!
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
94 local ok, res1, res2 = pcall( get_uv, val, n )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
95 if ok then return res1, res2 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
96 return nil, false
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
97 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
98 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
99
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
100 elseif type( getfenv ) == "function" then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
101
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
102 function get_environment( val, n, enabled )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
103 if enabled and n == 1 and type( val ) == "function" then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
104 local uv = getfenv( val )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
105 return uv, uv ~= nil
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
106 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
107 return nil, false
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
108 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
109
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
110 else
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
111
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
112 function get_environment() return nil, false end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
113
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
114 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
115
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
116
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
117 -- select implementation of get_registry
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
118 if type( debug ) == "table" and
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
119 type( debug.getregistry ) == "function" then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
120 get_registry = debug.getregistry
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
121 else
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
122 function get_registry() end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
123 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
124
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
125
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
126 -- select implementation of get_locals
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
127 -- locs = {
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
128 -- {
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
129 -- name = "example_function";
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
130 -- func = <function>;
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
131 -- [1] = { "local_name", <local value> };
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
132 -- [2] = ...;
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
133 -- };
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
134 -- }
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
135 if type( debug ) == "table" and
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
136 type( debug.getinfo ) == "function" and
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
137 type( debug.getlocal ) == "function" then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
138
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
139 local getinfo, getlocal = debug.getinfo, debug.getlocal
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
140
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
141 local function getinfo_nothread( _, func, what )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
142 return getinfo( func, what )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
143 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
144
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
145 local function getlocal_nothread( _, level, loc )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
146 return getlocal( level, loc )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
147 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
148
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
149 function get_locals( thread, enabled )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
150 if enabled then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
151 local locs = {}
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
152 local start = 1
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
153 local gi, gl = getinfo, getlocal
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
154 if not thread then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
155 gi, gl = getinfo_nothread, getlocal_nothread
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
156 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
157 local info, i = gi( thread, 0, "nf" ), 0
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
158 while info do
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
159 local t = { name = info.name, func = info.func }
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
160 local j, n,v = 1, gl( thread, i, 1 )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
161 while n ~= nil do
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
162 t[ j ] = { n, v }
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
163 j = j + 1
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
164 n,v = gl( thread, i, j )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
165 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
166 i = i + 1
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
167 locs[ i ] = t
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
168 -- We skip the currently-executing traverse() function
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
169 -- and anything that is only referenced through it
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
170 if info.func == traverse then start = i+1 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
171 info = gi( thread, i, "nf" )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
172 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
173 return locs, start
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
174 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
175 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
176
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
177 else
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
178
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
179 function get_locals() end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
180
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
181 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
182
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
183
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
184 -- select implementation of upvalues depending on available API
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
185 local function dummy_iter() end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
186 if type( debug ) == "table" and
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
187 type( debug.getupvalue ) == "function" then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
188
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
189 local get_up, uv_iter = debug.getupvalue
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
190 if _VERSION == "Lua 5.1" then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
191
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
192 function uv_iter( state )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
193 local name, uv = get_up( state.value, state.n )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
194 state.n = state.n + 1
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
195 return name, uv, nil
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
196 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
197
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
198 else -- Lua 5.2 (and later) mixes upvalues and environments
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
199
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
200 local get_upid
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
201 if type( debug.upvalueid ) == "function" then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
202 get_upid = debug.upvalueid
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
203 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
204
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
205 function uv_iter( state )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
206 local name, uv = get_up( state.value, state.n )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
207 state.n = state.n + 1
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
208 if name == "_ENV" and not state.show_env then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
209 return uv_iter( state )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
210 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
211 local id = nil
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
212 if get_upid ~= nil and name ~= nil then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
213 id = get_upid( state.value, state.n - 1 )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
214 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
215 return name, uv, id
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
216 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
217 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
218
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
219 function upvalues( val, enabled, show_env )
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
220 if enabled then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
221 return uv_iter, { value = val, n = 1, show_env = show_env }
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
222 else
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
223 return dummy_iter
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
224 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
225 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
226
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
227 else
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
228
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
229 function upvalues()
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
230 return dummy_iter
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
231 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
232
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
233 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
234
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
235 local function handle_metatable(env, obj)
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
236 local mtable = get_metatable(obj)
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
237 if mtable ~= nil then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
238 edge(env, obj, mtable, "ismetatable", nil)
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
239 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
240 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
241
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
242 local function handle_environment(env, obj)
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
243 local n = 0
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
244 repeat
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
245 n = n + 1
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
246 local fenv, has_env = get_environment(obj, n)
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
247 if has_env and fenv ~= nil then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
248 edge(env, obj, fenv, "environment", nil)
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
249 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
250 until not has_env
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
251 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
252
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
253 local function handle_locals(env, obj)
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
254 local stack = get_locals(obj);
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
255 if not stack then return; end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
256 for _, frame in ipairs(stack) do
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
257 for local_entry in ipairs(frame) do
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
258 local local_name, local_value = local_entry[1], local_entry[2];
12
e3e3cbe544ec Add thread as referencing object for locals
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
259 edge(env, obj, local_name, "isname", nil);
e3e3cbe544ec Add thread as referencing object for locals
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
260 edge(env, obj, local_value, "local", local_name);
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
261 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
262 end
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
263 end
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
264
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
265 -- Main function
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
266 -- 'funcs' is a table that contains a funcation for every lua type and also the
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
267 -- function edge edge (traverseedge).
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
268 function traverse(funcs, ignoreobjs, seenobjs)
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
269
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
270 -- The keys of the marked table are the objects (for example, table: 00442330).
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
271 -- The value of each key is true if the object has been found and false
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
272 -- otherwise.
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
273 local env = {marked = seenobjs or {}, list=List.new(), funcs=funcs}
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
274
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
275 if ignoreobjs then
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
276 for i=1, #ignoreobjs do
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
277 env.marked[ignoreobjs[i]] = true
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
278 end
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
279 end
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
280
3
842f3003db2a Rename module 'gc' -> 'traverse'
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
281 env.marked["traverse"] = true
842f3003db2a Rename module 'gc' -> 'traverse'
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
282 env.marked[traverse] = true
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
283
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
284 -- marks and inserts on the list
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
285 edge(env, nil, "_G", "isname", nil)
7
258aedad056c Clear up the confusing key/value/name issue
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
286 edge(env, nil, _G, "value", "_G")
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
287
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
288 -- traverse the registry
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
289 local registry = get_registry();
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
290 if registry ~= nil then
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
291 edge(env, nil, registry, "value", "REGISTRY");
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
292 end
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
293
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
294 -- traverses the active thread
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
295 -- inserts the local variables
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
296 -- interates over the function on the stack, starting from the one that
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
297 -- called traverse
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
298 handle_locals(env, nil);
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
299
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
300 while not List.isempty(env.list) do
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
301
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
302 local obj = List.pop(env.list)
3
842f3003db2a Rename module 'gc' -> 'traverse'
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
303 local t = type(obj)
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
304 types[t](env, obj)
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
305
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
306 end
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
307 end
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
308
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
309 function types.table(env, obj)
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
310 local f = env.funcs.table
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
311 if f then f(obj) end
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
312
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
313 for key, value in pairs(obj) do
7
258aedad056c Clear up the confusing key/value/name issue
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
314 edge(env, obj, key, "key", nil)
258aedad056c Clear up the confusing key/value/name issue
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
315 edge(env, obj, value, "value", key)
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
316 end
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
317
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
318 handle_metatable(env, obj)
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
319 end
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
320
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
321 function types.string(env, obj)
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
322 local f = env.funcs.string
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
323 if f then f(obj) end
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
324 end
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
325
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
326 function types.userdata(env, obj)
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
327 local f = env.funcs.userdata
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
328 if f then f(obj) end
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
329
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
330 handle_metatable(env, obj)
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
331 handle_environment(env, obj)
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
332 end
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
333
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
334 types["function"] = function (env, obj)
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
335 local f = env.funcs.func
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
336 if f then f(obj) end
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
337
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
338 handle_environment(env, obj)
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
339
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
340 -- handle upvalues
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
341 for name, value in upvalues(obj, true, true) do
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
342 edge(env, obj, name, "isname", nil)
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
343 edge(env, obj, value, "upvalue", name)
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
344 end
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
345 end
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
346
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
347 function types.thread(env, t)
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
348 local f = env.funcs.thread
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
349 if f then f(t) end
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
350
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
351 handle_environment(env, t)
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
352 handle_locals(env, t);
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
353
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
354 end
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
355
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
356
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
357 -- 'how' is a string that identifies the content of 'to' and 'value':
7
258aedad056c Clear up the confusing key/value/name issue
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
358 -- if 'how' is "key", then 'to' is a key and 'name' is nil.
258aedad056c Clear up the confusing key/value/name issue
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
359 -- if 'how' is "value", then 'to' is an object and 'name' is the name of the
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
360 -- key.
7
258aedad056c Clear up the confusing key/value/name issue
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
361 function edge(env, from, to, how, name)
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
362
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
363 local t = type(to)
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
364
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
365 if to and (t~="boolean") and (t~="number") and (t~="new") then
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
366 -- If the destination object has not been found yet
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
367 if not env.marked[to] then
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
368 env.marked[to] = true
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
369 List.push(env.list, to) -- puts on the list to be traversed
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
370 end
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
371
1
036168493972 Converting to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
372 local f = env.funcs.edge
7
258aedad056c Clear up the confusing key/value/name issue
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
373 if f then f(from, to, how, name) end
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
374
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
375 end
4
a4f608729bbf Whitespace and various fixes, return _M
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
376 end
a4f608729bbf Whitespace and various fixes, return _M
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
377
11
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
378 return {
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
379 traverse = traverse;
1e2c945346ca Major update and refactor to support Lua 5.1, 5.2, 5.3 and 5.4.
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
380 };

mercurial