forked from cloudwu/lua-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsnapshot_utils.lua
More file actions
252 lines (238 loc) · 6.22 KB
/
snapshot_utils.lua
File metadata and controls
252 lines (238 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
local M = {
omit_excess_refrence = true
}
-- see snapshot.c:L16
local REF_TYPE_TABLE_VALUE = 1
local REF_TYPE_TABLE_KEY = 2
local REF_TYPE_LOCAL = 3
local REF_TYPE_UPVALUE = 4
local REF_TYPE_META = 5
function M.objtypename(objtype)
if type(objtype) == "string" then
return objtype
end
if objtype == 5 then
return "table"
elseif objtype == 6 then
return "function"
elseif objtype == 7 then
return "userdata"
elseif objtype == 8 then
return "thread"
else
error("invalid objtype: " .. objtype)
end
end
function M.reftypename(reftype)
if reftype == REF_TYPE_TABLE_VALUE then
return "tablevalue"
elseif reftype == REF_TYPE_TABLE_KEY then
return "tablekey"
elseif reftype == REF_TYPE_LOCAL then
return "local"
elseif reftype == REF_TYPE_UPVALUE then
return "upvalue"
elseif reftype == REF_TYPE_META then
return "meta"
else
error("invalid reftype: " .. reftype)
end
end
M.translate = {
["[registry].mainthread"] = "_M", -- lua5.1
["[registry].[1]"] = "_M",
["[registry].[2]"] = "_G",
["[registry]._LOADED"] = "package.loaded",
["[registry]._PRELOAD"] = "package.preload",
}
function M.refname(ref)
local name
if ref.type == REF_TYPE_TABLE_VALUE then
name = ref.name
elseif ref.type == REF_TYPE_TABLE_KEY then
name = ref.name
elseif ref.type == REF_TYPE_LOCAL then
name = string.format("<%s %s>",M.reftypename(ref.type),ref.name)
elseif ref.type == REF_TYPE_UPVALUE then
name = string.format("<%s %s>",M.reftypename(ref.type),ref.name)
elseif ref.type == REF_TYPE_META then
name = ref.name
else
assert(false)
end
return name
end
function M.objname(object,S)
if object.name then
return object.name
end
local name
local ref = object.reflist[object.refindex]
if object.depth == 0 then
name = ref.name
else
local refobj = S[ref.obj]
name = string.format("%s.%s",M.objname(refobj,S),M.refname(ref))
end
local trans = M.translate[name]
if trans then
name = trans
end
assert(name ~= nil)
object.name = name
return name
end
function M.pretty_object(object,S)
object.name = M.objname(object,S)
object.type = M.objtypename(object.type)
object.refindex = nil
object.depth = nil
if object.source == "" then
object.source = nil
end
end
function M.pretty_object_reflist(object,S)
for i,ref in ipairs(object.reflist) do
if ref.obj == "(nil)" then -- root
object.reflist[i] = M.refname(ref)
else
local refobj = S[ref.obj]
object.reflist[i] = string.format("%s.%s",refobj.name,M.refname(ref))
end
if M.omit_excess_refrence and i >= 10 then
if i == 10 then
object.reflist[i] = "..."
else
object.reflist[i] = nil
end
end
end
end
function M.diff(S1,S2,pretty)
local diff = {}
for id,object in pairs(S2) do
if S1[id] == nil then
diff[id] = object
end
end
if not pretty then
return diff
end
for id,object in pairs(S2) do
M.pretty_object(object, S2)
end
for id,object in pairs(diff) do
M.pretty_object_reflist(object,S2)
end
for id,object in pairs(diff) do
object.name = nil
end
return diff
end
function M.dump(t,space,name)
if type(t) ~= "table" then
return tostring(t)
end
space = space or ""
name = name or ""
local cache = { [t] = "."}
local function _dump(t,space,name)
local temp = {}
for k,v in pairs(t) do
local key = tostring(k)
if cache[v] then
table.insert(temp,"+" .. key .. " {" .. cache[v].."}")
elseif type(v) == "table" then
local new_key = name .. "." .. key
cache[v] = new_key
table.insert(temp,"+" .. key .. _dump(v,space .. (next(t,k) and "|" or " " ).. string.rep(" ",#key),new_key))
else
table.insert(temp,"+" .. key .. " [" .. tostring(v).."]")
end
end
return table.concat(temp,"\n"..space)
end
return _dump(t,space,name)
end
function M.sortN(n,tbl,cmp)
local ret = {}
local len = 0
local function bubble(pos,v)
while pos > 1 do
if cmp(v,ret[pos-1]) then
ret[pos] = ret[pos-1]
pos = pos - 1
else
break
end
end
ret[pos] = v
end
for k,v in pairs(tbl) do
if len < n then
len = len + 1
bubble(len,v)
elseif cmp(v,ret[len]) then
bubble(len,v)
end
end
return ret
end
function M.refcount_topN(n,S,pretty)
local list = {}
for id,object in pairs(S) do
list[#list+1] = object
end
list = M.sortN(n,list,function (a,b)
local refcount1 = #a.reflist
local refcount2 = #b.reflist
if refcount1 == refcount2 then
return false
end
return refcount1 > refcount2
end)
if not pretty then
return list
end
for id,object in pairs(S) do
M.pretty_object(object, S)
end
for i,object in ipairs(list) do
M.pretty_object_reflist(object,S)
end
for i,object in ipairs(list) do
object.name = nil
end
return list
end
function M.tablecount_topN(n,S,pretty)
local list = {}
local g = tostring(_G):sub(8,-1)
for id,object in pairs(S) do
if id ~= g then
list[#list+1] = object
end
end
list = M.sortN(n,list,function (a,b)
local tablecount1 = a.tablecount
local tablecount2 = b.tablecount
if tablecount1 == tablecount2 then
return false
end
return tablecount1 > tablecount2
end)
if not pretty then
return list
end
for id,object in pairs(S) do
M.pretty_object(object, S)
end
for i,object in ipairs(list) do
M.pretty_object_reflist(object,S)
end
for i,object in ipairs(list) do
object.name = nil
end
return list
end
return M