transform back into metatable-based object-orientation

dev
kikito 11 years ago
parent 2961caa14a
commit d07147ed54
  1. 166
      inspect.lua

@ -170,142 +170,148 @@ local function processRecursive(process, item, path)
return processed return processed
end end
-------------------------------------------------------------------
function inspect.inspect(root, options)
options = options or {}
local depth = options.depth or math.huge
local process = options.process
if process then
root = processRecursive(process, root, {})
end
local tableAppearances = countTableAppearances(root) -------------------------------------------------------------------
local buffer = {} local Inspector = {}
local maxIds = setmetatable({}, maxIdsMetaTable) local Inspector_mt = {__index = Inspector}
local ids = setmetatable({}, idsMetaTable)
local level = 0
local blen = 0 -- buffer length
local function puts(...) function Inspector:puts(...)
local args = {...} local args = {...}
local buffer = self.buffer
local len = #buffer
for i=1, #args do for i=1, #args do
blen = blen + 1 len = len + 1
buffer[blen] = tostring(args[i]) buffer[len] = tostring(args[i])
end
end end
end
local function down(f) function Inspector:down(f)
level = level + 1 self.level = self.level + 1
f() f()
level = level - 1 self.level = self.level - 1
end end
local function tabify() function Inspector:tabify()
puts("\n", string.rep(" ", level)) self:puts("\n", string.rep(" ", self.level))
end end
local function commaControl(needsComma) function Inspector:commaControl(needsComma)
if needsComma then puts(',') end if needsComma then self:puts(',') end
return true return true
end end
local function alreadyVisited(v) function Inspector:alreadyVisited(v)
return ids[type(v)][v] ~= nil return self.ids[type(v)][v] ~= nil
end end
local function getId(v) function Inspector:getId(v)
local tv = type(v) local tv = type(v)
local id = ids[tv][v] local id = self.ids[tv][v]
if not id then if not id then
id = maxIds[tv] + 1 id = self.maxIds[tv] + 1
maxIds[tv] = id self.maxIds[tv] = id
ids[tv][v] = id self.ids[tv][v] = id
end end
return id return id
end end
local putValue -- forward declaration that needs to go before putTable & putKey
local function putKey(k) function Inspector:putKey(k)
if isIdentifier(k) then return puts(k) end if isIdentifier(k) then return self:puts(k) end
puts( "[" ) self:puts("[")
putValue(k, {}) self:putValue(k)
puts("]") self:puts("]")
end end
local function putTable(t) function Inspector:putTable(t)
if alreadyVisited(t) then if self:alreadyVisited(t) then
puts('<table ', getId(t), '>') self:puts('<table ', self:getId(t), '>')
elseif level >= depth then elseif self.level >= self.depth then
puts('{...}') self:puts('{...}')
else else
if tableAppearances[t] > 1 then puts('<', getId(t), '>') end if self.tableAppearances[t] > 1 then self:puts('<', self:getId(t), '>') end
local dictKeys = getDictionaryKeys(t) local dictKeys = getDictionaryKeys(t)
local length = #t local length = #t
local mt = getmetatable(t) local mt = getmetatable(t)
local to_string_result = getToStringResultSafely(t, mt) local to_string_result = getToStringResultSafely(t, mt)
puts('{') self:puts('{')
down(function() self:down(function()
if to_string_result then if to_string_result then
puts(' -- ', escape(to_string_result)) self:puts(' -- ', escape(to_string_result))
if length >= 1 then tabify() end -- tabify the array values if length >= 1 then self:tabify() end
end end
local needsComma = false local needsComma = false
for i=1, length do for i=1, length do
needsComma = commaControl(needsComma) needsComma = self:commaControl(needsComma)
puts(' ') self:puts(' ')
putValue(t[i]) self:putValue(t[i])
end end
for _,k in ipairs(dictKeys) do for _,k in ipairs(dictKeys) do
needsComma = commaControl(needsComma) needsComma = self:commaControl(needsComma)
tabify() self:tabify()
putKey(k) self:putKey(k)
puts(' = ') self:puts(' = ')
putValue(t[k]) self:putValue(t[k])
end end
if mt then if mt then
needsComma = commaControl(needsComma) needsComma = self:commaControl(needsComma)
tabify() self:tabify()
puts('<metatable> = ') self:puts('<metatable> = ')
putValue(mt) self:putValue(mt)
end end
end) end)
if #dictKeys > 0 or mt then -- dictionary table. Justify closing } if #dictKeys > 0 or mt then -- dictionary table. Justify closing }
tabify() self:tabify()
elseif length > 0 then -- array tables have one extra space before closing } elseif length > 0 then -- array tables have one extra space before closing }
puts(' ') self:puts(' ')
end
puts('}')
end end
self:puts('}')
end end
end
-- putvalue is forward-declared before putTable & putKey function Inspector:putValue(v)
putValue = function(v)
local tv = type(v) local tv = type(v)
if tv == 'string' then if tv == 'string' then
puts(smartQuote(escape(v))) self:puts(smartQuote(escape(v)))
elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then
puts(tostring(v)) self:puts(tostring(v))
elseif tv == 'table' then elseif tv == 'table' then
putTable(v) self:putTable(v)
else else
puts('<',tv,' ',getId(v),'>') self:puts('<',tv,' ',self:getId(v),'>')
end end
end
-------------------------------------------------------------------
function inspect.inspect(root, options)
options = options or {}
local depth = options.depth or math.huge
local process = options.process
if process then
root = processRecursive(process, root, {})
end end
putValue(root, {}) local inspector = setmetatable({
depth = depth,
buffer = {},
level = 0,
ids = setmetatable({}, idsMetaTable),
maxIds = setmetatable({}, maxIdsMetaTable),
tableAppearances = countTableAppearances(root)
}, Inspector_mt)
inspector:putValue(root)
return table.concat(buffer) return table.concat(inspector.buffer)
end end
setmetatable(inspect, { __call = function(_, ...) return inspect.inspect(...) end }) setmetatable(inspect, { __call = function(_, ...) return inspect.inspect(...) end })

Loading…
Cancel
Save