From d372d2ab08b4e1664867670aa05e120a703d8bb8 Mon Sep 17 00:00:00 2001 From: Andreas Hofer Date: Tue, 29 Mar 2016 22:14:10 +0200 Subject: [PATCH 1/3] fix problem with recursive tables when using the 'process' option --- inspect.lua | 39 ++++++++++++++++++++++++--------------- spec/inspect_spec.lua | 6 ++++++ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/inspect.lua b/inspect.lua index 39f43af..96194ea 100644 --- a/inspect.lua +++ b/inspect.lua @@ -165,28 +165,37 @@ local function makePath(path, ...) end 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) - if type(processed) == 'table' then - local processedCopy = {} - local processedKey + for k,v in pairs(processed) do + processedKey = processRecursive2(k, makePath(path, k, inspect.KEY)) + if processedKey ~= nil then + processedCopy[processedKey] = processRecursive2(v, makePath(path, processedKey)) + end + end - for k,v in pairs(processed) do - processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY)) - if processedKey ~= nil then - processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey)) + local mt = processRecursive2(getmetatable(processed), makePath(path, inspect.METATABLE)) + setmetatable(processedCopy, mt) + processed = processedCopy end + return processed end - - local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE)) - setmetatable(processedCopy, mt) - processed = processedCopy - end - return processed + + return processRecursive2(item, path) end + ------------------------------------------------------------------- local Inspector = {} diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 9a81642..78b0a7b 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -315,6 +315,12 @@ describe( 'inspect', function() }, items) 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) describe('metatables', function() From 89588bd4d939b81fc5d2d45e010a65cb7e250217 Mon Sep 17 00:00:00 2001 From: Andreas Hofer Date: Thu, 31 Mar 2016 20:13:35 +0200 Subject: [PATCH 2/3] fix problem reported by luacheck --- inspect.lua | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/inspect.lua b/inspect.lua index 96194ea..e3061b5 100644 --- a/inspect.lua +++ b/inspect.lua @@ -167,24 +167,24 @@ end local function processRecursive(process, item, path) local visited = {} - local function processRecursive2(item, path) - if item == nil then return nil end - if visited[item] then return visited[item] end + local function processRecursive2(item2, path2) + if item2 == nil then return nil end + if visited[item2] then return visited[item2] end - local processed = process(item, path) + local processed = process(item2, path2) if type(processed) == 'table' then local processedCopy = {} - visited[item] = processedCopy + visited[item2] = processedCopy local processedKey for k,v in pairs(processed) do - processedKey = processRecursive2(k, makePath(path, k, inspect.KEY)) + processedKey = processRecursive2(k, makePath(path2, k, inspect.KEY)) if processedKey ~= nil then - processedCopy[processedKey] = processRecursive2(v, makePath(path, processedKey)) + processedCopy[processedKey] = processRecursive2(v, makePath(path2, processedKey)) end end - local mt = processRecursive2(getmetatable(processed), makePath(path, inspect.METATABLE)) + local mt = processRecursive2(getmetatable(processed), makePath(path2, inspect.METATABLE)) setmetatable(processedCopy, mt) processed = processedCopy end From 1f54536b5620bf457ee2309eb353b4790434bbec Mon Sep 17 00:00:00 2001 From: Andreas Hofer Date: Sun, 3 Apr 2016 19:43:01 +0200 Subject: [PATCH 3/3] changed function processRecursive --- inspect.lua | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/inspect.lua b/inspect.lua index e3061b5..4b654b2 100644 --- a/inspect.lua +++ b/inspect.lua @@ -164,34 +164,29 @@ local function makePath(path, ...) return newPath end -local function processRecursive(process, item, path) - local visited = {} +local function processRecursive(process, item, path, visited) - local function processRecursive2(item2, path2) - if item2 == nil then return nil end - if visited[item2] then return visited[item2] end - - local processed = process(item2, path2) - if type(processed) == 'table' then - local processedCopy = {} - visited[item2] = processedCopy - local processedKey + 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 - for k,v in pairs(processed) do - processedKey = processRecursive2(k, makePath(path2, k, inspect.KEY)) - if processedKey ~= nil then - processedCopy[processedKey] = processRecursive2(v, makePath(path2, processedKey)) - end + for k,v in pairs(processed) do + processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited) + if processedKey ~= nil then + processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited) end - - local mt = processRecursive2(getmetatable(processed), makePath(path2, inspect.METATABLE)) - setmetatable(processedCopy, mt) - processed = processedCopy end - return processed + + local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE), visited) + setmetatable(processedCopy, mt) + processed = processedCopy end - - return processRecursive2(item, path) + return processed end @@ -324,7 +319,7 @@ function inspect.inspect(root, options) local process = options.process if process then - root = processRecursive(process, root, {}) + root = processRecursive(process, root, {}, {}) end local inspector = setmetatable({