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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author OpportunityLiu
-- @file dump.lua
--
-- define module
local dump = dump or {}
-- load modules
local todisplay = require("base/todisplay")
-- print string
function dump._print_string(str, as_key)
io.write(todisplay._print_string(str, as_key))
end
-- print keyword
function dump._print_keyword(keyword)
io.write(todisplay._print_keyword(keyword))
end
-- print function
function dump._print_function(func, as_key)
if as_key then
io.write(todisplay._translate("${reset}${color.dump.function}"),
todisplay._format("text.dump.default_format", "%s", func),
todisplay._translate("${reset}"))
else
io.write(todisplay._print_function(func))
end
end
-- print scalar value
function dump._print_scalar(value, as_key)
if type(value) == "string" then
dump._print_string(value, as_key)
elseif type(value) == "function" then
dump._print_function(value, as_key)
else
io.write(todisplay._print_scalar(value))
end
end
-- print anchor
function dump._print_anchor(printed_set_value)
io.write(todisplay._translate("${color.dump.anchor}"),
todisplay._format("text.dump.anchor", "&%s", printed_set_value.id),
todisplay._translate("${reset}"))
end
-- print reference
function dump._print_reference(printed_set_value)
io.write(todisplay._translate("${color.dump.reference}"),
todisplay._format("text.dump.reference", "*%s", printed_set_value.id),
todisplay._translate("${reset}"))
end
-- print anchor and store to printed_set
function dump._print_table_anchor(value, printed_set)
io.write(" ")
if printed_set[value].id then
dump._print_anchor(printed_set[value])
printed_set.refs[value] = printed_set[value]
end
end
-- print metatable of value
function dump._print_metatable(value, metatable, inner_indent, printed_set, print_archor)
if not metatable then
return false
end
-- print metamethods
local has_record = false
local has_index_table = false
for k, v in pairs(metatable) do
if k == "__index" and type(v) == "table" then
has_index_table = true
elseif k:startswith("__") then
if not has_record then
has_record = true
if print_archor then
dump._print_table_anchor(value, printed_set)
end
end
io.write("\n", inner_indent)
local funcname = k:sub(3)
dump._print_keyword(funcname)
io.write(todisplay._translate("${reset} ${dim}=${reset} "))
if funcname == "tostring" or funcname == "len" or funcname == "todisplay" then
local ok, result = pcall(v, value, value)
if ok then
if funcname == "todisplay" and type(result) == "string" then
io.write(todisplay._translate(result))
else
dump._print_scalar(result)
end
io.write(todisplay._translate("${dim} (evaluated)${reset}"))
else
dump._print_scalar(v)
end
elseif v and printed_set.refs[v] then
dump._print_reference(printed_set.refs[v])
else
dump._print_scalar(v)
end
io.write(",")
end
end
if not has_index_table then
return has_record
end
-- print index methods
local index_table = metatable and rawget(metatable, "__index")
for k, v in pairs(index_table) do
-- hide private interfaces
if type(k) ~= "string" or not k:startswith("_") then
if not has_record then
has_record = true
if print_archor then
dump._print_table_anchor(value, printed_set)
end
end
io.write("\n", inner_indent)
dump._print_keyword("(")
dump._print_scalar(k, true)
dump._print_keyword(")")
io.write(todisplay._translate("${reset} ${dim}=${reset} "))
if v and printed_set.refs[v] then
dump._print_reference(printed_set.refs[v])
else
dump._print_scalar(v)
end
io.write(",")
end
end
return has_record
end
-- init printed_set
function dump._init_printed_set(printed_set, value)
assert(type(value) == "table")
for k, v in pairs(value) do
if type(v) == "table" then
-- has reference? v -> printed_set[v].obj
if printed_set[v] then
local obj = printed_set[v].obj
if not printed_set[obj].id then
printed_set.id = printed_set.id + 1
printed_set[obj].id = printed_set.id
end
else
printed_set[v] = {obj = v, name = k}
dump._init_printed_set(printed_set, v)
end
end
end
end
-- returns printed_set, is_first_level
function dump._get_printed_set(printed_set, value)
local first_level = not printed_set
if type(printed_set) ~= "table" then
printed_set = {id = 0, refs = {}}
if type(value) == "table" then
printed_set[value] = {obj = value}
dump._init_printed_set(printed_set, value)
end
end
return printed_set, first_level
end
-- print udata
function dump._print_udata(value, first_indent, remain_indent, printed_set)
local first_level
local metatable = debug.getmetatable(value)
printed_set, first_level = dump._get_printed_set(printed_set, metatable)
io.write(first_indent)
if not first_level then
io.write(todisplay._print_udata_scalar(value))
end
local inner_indent = remain_indent .. " "
-- print open brackets
io.write(todisplay._translate("${reset}${color.dump.udata}[${reset}"))
-- print metatable
local no_value = not dump._print_metatable(value, metatable, inner_indent, printed_set, false)
-- print close brackets
if no_value then
io.write(todisplay._translate(" ${color.dump.udata}]${reset}"))
else
io.write("\b \n", remain_indent, todisplay._translate("${reset}${color.dump.udata}]${reset}"))
end
end
-- print table
function dump._print_table(value, first_indent, remain_indent, printed_set)
local first_level
printed_set, first_level = dump._get_printed_set(printed_set, value)
io.write(first_indent)
local metatable = debug.getmetatable(value)
local tostringmethod = metatable and (rawget(metatable, "__todisplay") or rawget(metatable, "__tostring"))
if not first_level and tostringmethod then
return dump._print_scalar(value)
end
local inner_indent = remain_indent .. " "
local first_value = true
-- print open brackets
io.write(todisplay._translate("${reset}${color.dump.table}{${reset}"))
local function print_newline()
if first_value then
dump._print_table_anchor(value, printed_set)
first_value = false
end
io.write("\n", inner_indent)
end
-- print metatable
if first_level then
first_value = not dump._print_metatable(value, metatable, inner_indent, printed_set, true)
end
-- print array items
local is_arr = (value[1] ~= nil) and (table.maxn(value) < 2 * #value)
if is_arr then
for i = 1, table.maxn(value) do
print_newline()
local v = value[i]
if type(v) == "table" then
if printed_set.refs[v] then
dump._print_reference(printed_set.refs[v])
else
dump._print_table(v, "", inner_indent, printed_set)
end
else
dump._print_scalar(v)
end
io.write(",")
end
end
-- print data
for k, v in pairs(value) do
if not is_arr or type(k) ~= "number" then
print_newline()
dump._print_scalar(k, true)
io.write(todisplay._translate("${reset} ${dim}=${reset} "))
if type(v) == "table" then
if printed_set.refs[v] then
dump._print_reference(printed_set.refs[v])
else
dump._print_table(v, "", inner_indent, printed_set)
end
else
dump._print_scalar(v)
end
io.write(",")
end
end
-- print close brackets
if first_value then
io.write(todisplay._translate(" ${color.dump.table}}${reset}"))
else
io.write("\b \n", remain_indent, todisplay._translate("${reset}${color.dump.table}}${reset}"))
end
end
-- print value
function dump._print(value, indent, verbose)
indent = tostring(indent or "")
if type(value) == "table" then
dump._print_table(value, indent, indent:gsub(".", " "), not verbose)
elseif type(value) == "userdata" then
dump._print_udata(value, indent, indent:gsub(".", " "), not verbose)
else
io.write(indent)
dump._print_scalar(value)
end
io.write("\n")
end
return dump._print
|