-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump.lua
More file actions
139 lines (131 loc) · 3.5 KB
/
dump.lua
File metadata and controls
139 lines (131 loc) · 3.5 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
--4 8 24
--8 8 24
--by yyonce
--a function dump data.
local put = table.insert
local format = string.format
local rep = string.rep
local function ondump(o,opar,r,add,added,deep,par,meta,cache) --onpublic
local loaded = package.loaded
put(r,"{")
for k,v in pairs(o) do
local dot --false
local index = tostring(k)
local _k = index or "nil" --nonnil
--k
if true then
local n = rep(" ",add)
if k == _G then
put(r,format('\r\n%s[_G] = ',n))
else
local t = type(k)
if t == "string" then
put(r,format('\r\n%s[%q] = ',n,k))
dot = true --true
goto v
end
if t == "table" then
local mt = getmetatable(k)
if mt and mt.__tostring then
put(r,format('\r\n%s[%q] = ',n,index))
goto v
end
end
put(r,format('\r\n%s[%s] = ',n,index))
if k == v then
put(r,index)
put(r," ;")
goto c
end
end
end ::v::
--v
if true then
if v == _G then
put(r,"_G ;")
else
local t = type(v)
if t == "table" then
local mt = getmetatable(v)
local mtto = mt and mt.__tostring
if deep ~= nil and (deep == "_G" or deep == _k) then --can deep
if mtto and meta then
put(r,format("%q",tostring(v)))
goto c
end
--cache
local parent = par and par..(dot and "." or "/").._k or error("no par",2)
if not mtto then --need stable cache key
local _v = tostring(v) --nonnil
local cached = cache[_v]
if cached == nil then --no cache
cache[_v] = parent
else
--cached
put(r,cached)
put(r," ;")
goto c
end
end
--can deep
if v == loaded then --handle
ondump(v,o,r,add+added,added,nil,parent,meta,cache)
else
ondump(v,o,r,add+added,added,"_G",parent,meta,cache)
end
else
--no deep
if mtto then
put(r,format("%q ;",tostring(v)))
else
put(r,tostring(v))
put(r," ;")
end
end
elseif t == "string" then
put(r,format("%q ;",v))
else
put(r,tostring(v))
put(r," ;")
end
end
end ::c:: --continue
end
put(r,format("\r\n%s} ;",rep(" ",add-added)))
end
local function _dump(o,r,add,added,deep,par,concat,meta) --can dump table
if meta then
local mt = getmetatable(o)
if mt and mt.__tostring then
put(r,o == _G and "_G ;" or format("%q ;",tostring(o)))
return concat and table.concat(r) or nil
end
end
ondump(o,nil,r,add,added,deep,par or "index",meta,{})
return concat and table.concat(r) or nil
end
local function dump(o,deep)
if o == nil then
return "nil ;" end
local t = type(o)
if t == "table" then
return _dump(o,{},2,4,deep,nil,true,true)
elseif t == "string" then
return format("%q ;",o)
else return tostring(o).." ;"
end
end
if true then
print(dump(nil)) --nil ;
print(dump(true)) --true ;
print(dump(1.120)) --1.12 ;
print(dump("Lua")) --"Lua" ;
print(dump(dump))
print(dump(this))
print(dump(coroutine.running()))
print(dump(_G))
print(dump(_G,"package"))
print(dump(_G,"_G"))
end
local function r(o) return dump(o,"_G") end
return r