arbitrary type support (e.g., LuaJIT's cdata)

This commit is contained in:
Cheyi Lin 2013-06-19 19:33:16 +08:00
parent 091b62cd5d
commit f9575fd443

View File

@ -5,6 +5,17 @@
-- inspired by http://lua-users.org/wiki/TableSerialization -- inspired by http://lua-users.org/wiki/TableSerialization
----------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------
local type = type
local ipairs, pairs = ipairs, pairs
local string = string
local table = table
local tostring = tostring
local rawget = rawget
local pcall = pcall
local getmetatable, setmetatable = getmetatable, setmetatable
local rawget, rawset = rawget, rawset
local math = math
local inspect ={} local inspect ={}
inspect.__VERSION = '1.2.0' inspect.__VERSION = '1.2.0'
@ -41,10 +52,15 @@ local function isDictionaryKey(k, length)
return not isArrayKey(k, length) return not isArrayKey(k, length)
end end
local sortOrdersByType = { local sortOrdersByType = setmetatable({
['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4, ['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4,
['function'] = 5, ['userdata'] = 6, ['thread'] = 7 ['function'] = 5, ['userdata'] = 6, ['thread'] = 7
} },
{ __index = function (t, k)
if not rawget(t, k) then
return math.huge
end
end })
local function sortKeys(a, b) local function sortKeys(a, b)
local ta, tb = type(a), type(b) local ta, tb = type(a), type(b)
@ -73,6 +89,26 @@ local function getToStringResultSafely(t, mt)
return string return string
end end
local inspectorMaxIdsMetaTable = {
__index = function (t, k)
if not rawget(t, k) then
rawset(t, k, 0)
end
return 0
end
}
local inspectorIdsMetaTable = {
__index = function (t, k)
local v = rawget(t, k)
if not v then
rawset(t, k, setmetatable({}, {__mode = "kv"}))
v = rawget(t, k)
end
return v
end
}
local Inspector = {} local Inspector = {}
function Inspector:new(t, depth) function Inspector:new(t, depth)
@ -80,18 +116,8 @@ function Inspector:new(t, depth)
buffer = {}, buffer = {},
depth = depth, depth = depth,
level = 0, level = 0,
maxIds = { maxIds = setmetatable({}, inspectorMaxIdsMetaTable),
['function'] = 0, ids = setmetatable({}, inspectorIdsMetaTable),
['userdata'] = 0,
['thread'] = 0,
['table'] = 0
},
ids = {
['function'] = setmetatable({}, {__mode = "kv"}),
['userdata'] = setmetatable({}, {__mode = "kv"}),
['thread'] = setmetatable({}, {__mode = "kv"}),
['table'] = setmetatable({}, {__mode = "kv"})
},
tableAppearances = setmetatable({}, {__mode = "k"}) tableAppearances = setmetatable({}, {__mode = "k"})
} }