1 -------------------------------------------------------------------
2 -- This was changed for Wireshark's use by Hadriel Kaplan.
5 -- * provided 'serialize' option to output serialized info (ie, can be marshaled),
6 -- though note that serializing functions/metatables/userdata/threads will not
7 -- magically make them be their original type when marshaled.
8 -- * provided 'notostring' option, which if true will disabled calling __tostring
9 -- metamethod of tables.
10 -- * made it always print the index number of numbered-array entries, and on separate
11 -- lines like the normal key'd entries (much easier to read this way I think)
12 -- New public functions:
13 -- inspect.compare(first,second[,options])
14 -- inspect.marshal(inString[,options])
15 -- inspect.makeFilter(arrayTable)
18 -- Copyright (c) 2014, Hadriel Kaplan
19 -- My change to the code is in the Public Domain, or the BSD (3 clause) license if
20 -- Public Domain does not apply in your country, or you would prefer a BSD license.
21 -- But the original code is still under Enrique García Cota's MIT license (below).
22 -------------------------------------------------------------------
25 _VERSION
= 'inspect.lua 2.0.0 - with changes',
26 _URL
= 'http://github.com/kikito/inspect.lua',
27 _DESCRIPTION
= 'human-readable representations of tables',
31 Copyright (c) 2013 Enrique García Cota
33 Permission is hereby granted, free of charge, to any person obtaining a
34 copy of this software and associated documentation files (the
35 "Software"), to deal in the Software without restriction, including
36 without limitation the rights to use, copy, modify, merge, publish,
37 distribute, sublicense, and/or sell copies of the Software, and to
38 permit persons to whom the Software is furnished to do so, subject to
39 the following conditions:
41 The above copyright notice and this permission notice shall be included
42 in all copies or substantial portions of the Software.
44 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
45 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
46 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
47 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
48 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
49 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
50 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52 _TINDEX_KEY
= '<index>', -- the key name to use for index number entries for tables
53 _DEPTH_MARKER
= " ['<depth>'] = true " -- instead of printing '...' we print this
56 -- Apostrophizes the string if it has quotes, but not apostrophes
57 -- Otherwise, it returns a regular quoted string
58 local function smartQuote(str
)
59 if str
:match('"') and not str
:match("'") then
60 return "'" .. str
.. "'"
62 return '"' .. str
:gsub('"', '\\"') .. '"'
65 local controlCharsTranslation
= {
66 ["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n",
67 ["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v"
70 local function escapeChar(c
) return controlCharsTranslation
[c
] end
72 local function escape(str
)
73 local result
= str
:gsub("\\", "\\\\"):gsub("(%c)", escapeChar
)
77 local function isIdentifier(str
)
78 return type(str
) == 'string' and str
:match( "^[_%a][_%a%d]*$" )
81 local function isArrayKey(k
, length
)
82 return type(k
) == 'number' and 1 <= k
and k
<= length
85 local function isDictionaryKey(k
, length
)
86 return not isArrayKey(k
, length
)
89 local defaultTypeOrders
= {
90 ['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4,
91 ['function'] = 5, ['userdata'] = 6, ['thread'] = 7
94 local function sortKeys(a
, b
)
95 local ta
, tb
= type(a
), type(b
)
97 -- strings and numbers are sorted numerically/alphabetically
98 if ta
== tb
and (ta
== 'string' or ta
== 'number') then return a
< b
end
100 local dta
, dtb
= defaultTypeOrders
[ta
], defaultTypeOrders
[tb
]
101 -- Two default types are compared according to the defaultTypeOrders table
102 if dta
and dtb
then return defaultTypeOrders
[ta
] < defaultTypeOrders
[tb
]
103 elseif dta
then return true -- default types before custom ones
104 elseif dtb
then return false -- custom types after default ones
107 -- custom types are sorted out alphabetically
111 local function getDictionaryKeys(t
)
112 local keys
, length
= {}, #t
113 for k
,_
in pairs(t
) do
114 if isDictionaryKey(k
, length
) then table.insert(keys
, k
) end
116 table.sort(keys
, sortKeys
)
120 local function getToStringResultSafely(t
, mt
)
121 local __tostring
= type(mt
) == 'table' and rawget(mt
, '__tostring')
123 if type(__tostring
) == 'function' then
124 ok
, str
= pcall(__tostring
, t
)
125 str
= ok
and str
or 'error: ' .. tostring(str
)
127 if type(str
) == 'string' and #str
> 0 then return str
end
130 local maxIdsMetaTable
= {
131 __index
= function(self
, typeName
)
132 rawset(self
, typeName
, 0)
137 local idsMetaTable
= {
138 __index
= function (self
, typeName
)
139 local col
= setmetatable({}, {__mode
= "kv"})
140 rawset(self
, typeName
, col
)
145 local function countTableAppearances(t
, tableAppearances
)
146 tableAppearances
= tableAppearances
or setmetatable({}, {__mode
= "k"})
148 if type(t
) == 'table' then
149 if not tableAppearances
[t
] then
150 tableAppearances
[t
] = 1
151 for k
,v
in pairs(t
) do
152 countTableAppearances(k
, tableAppearances
)
153 countTableAppearances(v
, tableAppearances
)
155 countTableAppearances(getmetatable(t
), tableAppearances
)
157 tableAppearances
[t
] = tableAppearances
[t
] + 1
161 return tableAppearances
164 local function parse_filter(filter
)
165 if type(filter
) == 'function' then return filter
end
166 -- not a function, so it must be a table or table-like
167 filter
= type(filter
) == 'table' and filter
or {filter
}
168 local dictionary
= {}
169 for _
,v
in pairs(filter
) do dictionary
[v
] = true end
170 return function(x
) return dictionary
[x
] end
173 local function makePath(path
, key
)
174 local newPath
, len
= {}, #path
175 for i
=1, len
do newPath
[i
] = path
[i
] end
180 -------------------------------------------------------------------
181 function inspect
.inspect(rootObject
, options
)
182 options
= options
or {}
183 local depth
= options
.depth
or math
.huge
184 local filter
= parse_filter(options
.filter
or {})
185 local serialize
= options
.serialize
187 local depth_marker
= inspect
._DEPTH_MARKER
189 local tableAppearances
= countTableAppearances(rootObject
)
192 local maxIds
= setmetatable({}, maxIdsMetaTable
)
193 local ids
= setmetatable({}, idsMetaTable
)
195 local blen
= 0 -- buffer length
197 local function puts(...)
201 buffer
[blen
] = tostring(args
[i
])
205 -- like puts above, but for things we want as quoted strings
206 -- so they become values, as we do if serializing
207 local function putv(...)
215 -- if serializing, using raw strings is unsafe, so we use the full "['key']" style
216 local function putk(...)
224 -- if not serializing, it's all puts
225 if not serialize
then
231 -- disable using __tostring metamethod
232 local getToStringResultSafely
= getToStringResultSafely
233 if options
.notostring
or serialize
then
234 getToStringResultSafely
= function() return end
237 local function down(f
)
243 local function tabify()
244 puts("\n", string.rep(" ", level
))
247 local function commaControl(needsComma
)
248 if needsComma
then puts(',') end
252 local function alreadyVisited(v
)
253 return ids
[type(v
)][v
] ~= nil
256 local function getId(v
)
258 local id
= ids
[tv
][v
]
267 local putValue
-- forward declaration that needs to go before putTable & putKey
269 local function putKey(k
)
270 if not serialize
and isIdentifier(k
) then return puts(k
) end
276 local function putTable(t
, path
)
277 if alreadyVisited(t
) then
278 putv('<table ', getId(t
), '>')
279 elseif level
>= depth
then
280 puts('{', depth_marker
, '}')
282 if not serialize
and tableAppearances
[t
] > 1 then puts('<', getId(t
), '>') end
284 local dictKeys
= getDictionaryKeys(t
)
286 local mt
= getmetatable(t
)
287 local to_string_result
= getToStringResultSafely(t
, mt
)
291 if to_string_result
then
292 puts(' -- ', escape(to_string_result
))
293 if length
>= 1 then tabify() end -- tabify the array values
296 local needsComma
= false
298 if serialize
and tableAppearances
[t
] > 1 then
303 needsComma
= commaControl(needsComma
)
304 -- just doing puts(' ') made for ugly arrays
308 putValue(t
[i
], makePath(path
, i
))
311 for _
,k
in ipairs(dictKeys
) do
312 needsComma
= commaControl(needsComma
)
316 putValue(t
[k
], makePath(path
, k
))
320 needsComma
= commaControl(needsComma
)
324 putValue(mt
, makePath(path
, '<metatable>'))
328 if #dictKeys
> 0 or mt
then -- dictionary table. Justify closing }
330 elseif length
> 0 then -- array tables have one extra space before closing }
339 -- putvalue is forward-declared before putTable & putKey
340 putValue
= function(v
, path
)
341 if filter(v
, path
) then
346 if tv
== 'string' then
347 puts(smartQuote(escape(v
)))
348 elseif tv
== 'number' and v
== math
.huge
then
350 elseif tv
== 'number' or tv
== 'boolean' or tv
== 'nil' then
352 elseif tv
== 'table' then
355 putv('<',tv
,' ',getId(v
),'>')
360 putValue(rootObject
, {})
362 return table.concat(buffer
)
365 setmetatable(inspect
, { __call
= function(_
, ...) return inspect
.inspect(...) end })
367 -------------------------------------------------------------------
369 -- The above is very close to Enrique's original inspect library.
370 -- Below are my main changes.
372 -------------------------------------------------------------------
373 -- Given a string generated by inspect() with the serialize option,
374 -- this function marshals it back into a Lua table/whatever.
375 -- If the string's table(s) had metatable(s), i.e. "<metatable>" tables,
376 -- then this keeps them as "<metatable>" subtables unless the option
377 -- 'nometa' is set to true.
379 -- This function also removes all "<index>" entries.
381 function inspect
.marshal(inString
, options
)
382 options
= options
or {}
383 local index
= inspect
._TINDEX_KEY
385 local function removeIndex(t
)
386 if type(t
) == 'table' then
388 for _
, v
in pairs(t
) do
394 local function removeMeta(t
)
395 if type(t
) == 'table' then
396 t
['<metatable>'] = nil
397 for _
, v
in pairs(t
) do
403 -- first skip past comments/empty-lines
404 -- warning: super-hack-ish weak
405 local pos
, ok
, dk
= 1, true, true
407 local stop
= string.len(inString
)
409 ok
, fin
= inString
:find("^[%s\r\n]+",pos
)
410 if ok
then pos
= fin
+ 1 end
411 dk
, fin
= inString
:find("^%-%-.-\n",pos
)
412 if dk
then pos
= fin
+ 1 end
415 if not inString
:find("^%s*return[%s%{]",pos
) then
416 inString
= "return " .. inString
419 local t
= assert(load(inString
))()
423 if options
.nometa
then removeMeta(t
) end
428 -------------------------------------------------------------------
430 -------------------------------------------------------------------
431 -- more private functions
433 -- things like '<function>' are equal to '<function 32>'
435 {"^<function ?%d*>", '<function>'},
436 {"^<table ?%d*>", '<table>'},
437 {"^<userdata ?%d*>", '<userdata>'},
438 {"^<thread ?%d*>", '<thread>'}
440 local function normalizeString(s
)
441 for _
,t
in ipairs(mungetypes
) do
450 ['<function>'] = 'function',
451 ['<table>'] = 'table',
452 ['<userdata>'] = 'userdata',
453 ['<thread>'] = 'thread'
455 local function getType(v
)
457 if tv
== 'string' then
458 tv
= typetable
[normalizeString(v
)] or 'string'
463 local function tablelength(t
)
465 for _
in pairs(t
) do count
= count
+ 1 end
469 -- for pretty-printing paths, for debug output
470 -- this is non-optimal, but only gets used in verbose mode anyway
471 local function serializePath(path
)
473 for i
,k
in ipairs(path
) do
475 if isIdentifier(k
) then
476 t
[i
] = ((i
== 1) and k
) or ('.'..k
)
477 elseif tk
== 'string' then
478 t
[i
] = '[' .. smartQuote(escape(k
)) .. ']'
479 elseif tk
== 'number' or tk
== 'boolean' then
480 t
[i
] = '[' .. tostring(k
) .. ']'
482 t
[i
] = "['<" .. tk
.. ">']"
485 if #t
== 0 then t
[1] = '{}' end
486 return table.concat(t
)
489 -------------------------------------------------------------------
491 -------------------------------------------------------------------
492 -- Given one table and another, this function detects if the first is
493 -- completely contained in the second object. The second can have more
494 -- entries, but cannot be missing an entry in the first one. Entry values
495 -- must match as well - i.e., string values are the same, numbers the
496 -- same, booleans the same.
498 -- The function returns true if the first is in the second, false otherwise.
499 -- It also returns a table of the diff, which will be empty if they matched.
500 -- This returned table is structured like the first one passed in,
501 -- so calling print(inspect(returnedTabled)) will make it pretty print.
503 -- The returned table's members have their values replaced with mismatch
504 -- information, explaining what the mismatch was. Setting the option "keep"
505 -- makes it not replace the values, but keep them as they were in the first
508 -- By default, the key's values must match in both tables. If the option
509 -- 'nonumber' is set, then number values are not compared. This is useful
510 -- if they're things that can change (like exported C-code numbers).
512 -- By default, the metatables/"<metatables>" are also compared. If the option
513 -- 'nometa' is set, then metatables are not compared, nor does it matter if
514 -- they exist in either table.
516 -- Like inspect(), there's a 'filter' option, which works the same way:
517 -- it ignores its value completely in terms of matching, so their string values
518 -- can be different, but the keys still have to exist. Sub-tables of
519 -- such keys (i.e., if the key's value is a table) are not checked/compared.
520 -- In other words, it's identical to the filter option for inspect().
522 -- The option 'ignore' is similar to 'filter', except matching ones
523 -- are not checked for existence in the tables at all.
525 -- Setting the 'depth' option applies as in inspect(), to both tables.
527 -- Setting the option 'verbose' makes it print out as it compares, for
528 -- debugging or test purposes.
530 function inspect
.compare(firstTable
, secondTable
, options
)
531 options
= options
or {}
532 local depth
= options
.depth
or math
.huge
533 local filter
= parse_filter(options
.filter
or {})
534 local ignore
= parse_filter(options
.ignore
or {})
536 local function puts(...)
540 buffer
[blen
] = tostring(args
[i
])
544 -- for debug printing
545 local function dprint(...)
547 print(table.concat(args
))
550 local serializePath
= serializePath
552 if not options
.verbose
then
553 dprint
= function() return end
554 serializePath
= function() return end
557 -- for error message replacing key value
558 local function emsg(...)
560 return(table.concat(args
))
564 emsg
= function() return end
567 -- declare checkValue here
570 local function checkTable(f
, s
, path
)
571 dprint("checking ",serializePath(path
)," table contents")
573 for k
, v
in pairs(f
) do
574 local child
= makePath(path
, k
)
576 if not ignore(v
,child
) then
577 local ret
, msg
= checkValue(v
, s
[k
], child
)
582 dprint(serializePath(child
)," ",msg
)
585 dprint("ignoring ",serializePath(child
))
589 return tablelength(f
) == 0
592 -- a wrapper for failure cases in checkValue() that can be handled the same way
593 local function compCheck(f
,s
,func
)
595 return false, emsg("mismatched ",getType(f
)," values: ",tostring(f
)," --> ",tostring(s
))
600 -- kinda ugly, but I wanted pretty information output
601 checkValue
= function(f
, s
, path
)
602 local tf
= getType(f
)
604 dprint("checking ",serializePath(path
)," (",tf
,")")
607 return false, emsg("missing ",tf
,"!")
608 elseif tf
~= getType(s
) then
609 return false, emsg("type mismatch (",tf
,") --> (",getType(s
),")")
610 elseif type(f
) == 'table' then
611 return checkTable(f
, s
, path
)
614 return compCheck(f
,s
,function()
615 if tf
== 'string' or tf
== 'boolean' then
617 elseif tf
== 'number' then
618 return f
== s
or options
.nonumber
620 -- assume they're the same functions/userdata/looped-table
621 -- type matching before would already cover it otherwise
627 -- inspect+serialize both tables, to normalize them, separate their
628 -- metatables, limit depth, etc. Also, since we pass the filter option on,
629 -- the filtered items become "<filtered>" and will by definition match
630 local function normalizeTable(t
)
631 return assert( inspect
.marshal( inspect
.inspect(t
,{serialize
=true,depth
=depth
,filter
=filter
}), {nometa
=options
.nometa
} ))
634 local first
= normalizeTable(firstTable
)
635 local second
= normalizeTable(secondTable
)
637 return checkTable(first
, second
, {}), first
641 -------------------------------------------------------------------
645 -------------------------------------------------------------------
646 -- Given a table of key strings, return a function that can be used for
647 -- the 'filter' option of inspect() and inspect.compare() functions.
648 function inspect
.makeFilter(arrayTable
)
649 local filter
= {} -- our filter lookup tree (tables of tables)
650 local matchNode
= {} -- a table instance we use as a key for nodes which match
651 local wildcard
= {} -- a key table of wildcard match names
653 local function buildFilter(pathname
)
656 -- if the filtered name starts with a '.', it's a wildcard
657 if pathname
:find("^%.") then
658 wildcard
[pathname
:sub(2)] = true
661 for sep
, name
in pathname
:gmatch("([%.%[\"\']*)([^%.%[\"\'%]]+)[\"\'%]]?") do
663 if name
== 'true' then
665 elseif name
== 'false' then
671 -- to be safe, we'll check the key name doesn't mean a table/function/userdata
672 local tn
= getType(name
)
673 if tn
== 'string' then
676 error("filter key '"..pathname
.."' has key '"..name
.."' which is an unsupported type ("..tn
..")")
689 -- we could call serializePath() and do a simple lookup, but it's expensive and
690 -- we'd be calling it a LOT. So instead we break up the filter
691 -- table into true "path" elements, into a filter tree, and compare
692 -- against it... thereby avoiding string concat/manip during compare.
694 for _
, pathname
in ipairs(arrayTable
) do
695 buildFilter(pathname
)
698 return function(value
,path
)
700 if wildcard
[ path
[#path
] ] then
703 for _
,v
in ipairs(path
) do
709 return t
[matchNode
] == true