fix problem with recursive tables when using the 'process' option

This commit is contained in:
Andreas Hofer 2016-03-29 22:14:10 +02:00
parent a998635207
commit d372d2ab08
2 changed files with 30 additions and 15 deletions

View File

@ -165,28 +165,37 @@ local function makePath(path, ...)
end end
local function processRecursive(process, item, path) local function processRecursive(process, item, path)
if item == nil then return nil end local visited = {}
local function processRecursive2(item, path)
if item == nil then return nil end
if visited[item] then return visited[item] end
local processed = process(item, path)
if type(processed) == 'table' then
local processedCopy = {}
visited[item] = processedCopy
local processedKey
local processed = process(item, path) for k,v in pairs(processed) do
if type(processed) == 'table' then processedKey = processRecursive2(k, makePath(path, k, inspect.KEY))
local processedCopy = {} if processedKey ~= nil then
local processedKey processedCopy[processedKey] = processRecursive2(v, makePath(path, processedKey))
end
end
for k,v in pairs(processed) do local mt = processRecursive2(getmetatable(processed), makePath(path, inspect.METATABLE))
processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY)) setmetatable(processedCopy, mt)
if processedKey ~= nil then processed = processedCopy
processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey))
end end
return processed
end end
local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE)) return processRecursive2(item, path)
setmetatable(processedCopy, mt)
processed = processedCopy
end
return processed
end end
------------------------------------------------------------------- -------------------------------------------------------------------
local Inspector = {} local Inspector = {}

View File

@ -315,6 +315,12 @@ describe( 'inspect', function()
}, items) }, items)
end) end)
it('handles recursive tables correctly', function()
local tbl = { 1,2,3}
tbl.loop = tbl
inspect(tbl, { process=function(x) return x end})
end)
end) end)
describe('metatables', function() describe('metatables', function()