From d051ae061c85309d2200c838cddeb6887553001a Mon Sep 17 00:00:00 2001 From: Andreas Hofer Date: Tue, 29 Mar 2016 22:53:26 +0200 Subject: [PATCH 1/3] fix endless recursion when using inspect to reimplement global tostring --- inspect.lua | 12 +++++++----- spec/inspect_spec.lua | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/inspect.lua b/inspect.lua index 39f43af..7616fc5 100644 --- a/inspect.lua +++ b/inspect.lua @@ -28,6 +28,8 @@ local inspect ={ ]] } +inspect.tostring = _G.tostring + inspect.KEY = setmetatable({}, {__tostring = function() return 'inspect.KEY' end}) inspect.METATABLE = setmetatable({}, {__tostring = function() return 'inspect.METATABLE' end}) @@ -110,7 +112,7 @@ local function getToStringResultSafely(t, mt) local str, ok if type(__tostring) == 'function' then ok, str = pcall(__tostring, t) - str = ok and str or 'error: ' .. tostring(str) + str = ok and str or 'error: ' .. inspect.tostring(str) end if type(str) == 'string' and #str > 0 then return str end end @@ -198,7 +200,7 @@ function Inspector:puts(...) local len = #buffer for i=1, #args do len = len + 1 - buffer[len] = tostring(args[i]) + buffer[len] = args[i] end end @@ -224,7 +226,7 @@ function Inspector:getId(v) self.maxIds[tv] = id self.ids[tv][v] = id end - return id + return inspect.tostring(id) end function Inspector:putKey(k) @@ -236,7 +238,7 @@ end function Inspector:putTable(t) if t == inspect.KEY or t == inspect.METATABLE then - self:puts(tostring(t)) + self:puts(inspect.tostring(t)) elseif self:alreadyVisited(t) then self:puts('') elseif self.level >= self.depth then @@ -296,7 +298,7 @@ function Inspector:putValue(v) if tv == 'string' then self:puts(smartQuote(escape(v))) elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then - self:puts(tostring(v)) + self:puts(inspect.tostring(v)) elseif tv == 'table' then self:putTable(v) else diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 9a81642..e702e9f 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -411,4 +411,19 @@ describe( 'inspect', function() end) end) end) + + it('allows changing the global tostring', function() + local save = _G.tostring + _G.tostring = function(x) + if type(x) == "string" then + return x + else + return inspect(x) + end + end + local s = tostring({1, 2, 3}) + _G.tostring = save + assert.equals("{ 1, 2, 3 }", s) + end) + end) From 61d02902b3219098cf64f3261a76a5d1129fc3d3 Mon Sep 17 00:00:00 2001 From: Andreas Hofer Date: Thu, 31 Mar 2016 20:20:50 +0200 Subject: [PATCH 2/3] fix problem reported by coverage analysis --- spec/inspect_spec.lua | 4 ---- 1 file changed, 4 deletions(-) diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index e702e9f..5aa1934 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -415,11 +415,7 @@ describe( 'inspect', function() it('allows changing the global tostring', function() local save = _G.tostring _G.tostring = function(x) - if type(x) == "string" then - return x - else return inspect(x) - end end local s = tostring({1, 2, 3}) _G.tostring = save From e091fcd43dae8e94fe194093ace1a56a6b5556d0 Mon Sep 17 00:00:00 2001 From: Andreas Hofer Date: Sun, 3 Apr 2016 20:00:37 +0200 Subject: [PATCH 3/3] changed 'tostring' from a field of inspect to a local variable --- inspect.lua | 10 +++++----- spec/inspect_spec.lua | 4 +--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/inspect.lua b/inspect.lua index 7616fc5..08603a2 100644 --- a/inspect.lua +++ b/inspect.lua @@ -28,7 +28,7 @@ local inspect ={ ]] } -inspect.tostring = _G.tostring +local tostring = tostring inspect.KEY = setmetatable({}, {__tostring = function() return 'inspect.KEY' end}) inspect.METATABLE = setmetatable({}, {__tostring = function() return 'inspect.METATABLE' end}) @@ -112,7 +112,7 @@ local function getToStringResultSafely(t, mt) local str, ok if type(__tostring) == 'function' then ok, str = pcall(__tostring, t) - str = ok and str or 'error: ' .. inspect.tostring(str) + str = ok and str or 'error: ' .. tostring(str) end if type(str) == 'string' and #str > 0 then return str end end @@ -226,7 +226,7 @@ function Inspector:getId(v) self.maxIds[tv] = id self.ids[tv][v] = id end - return inspect.tostring(id) + return tostring(id) end function Inspector:putKey(k) @@ -238,7 +238,7 @@ end function Inspector:putTable(t) if t == inspect.KEY or t == inspect.METATABLE then - self:puts(inspect.tostring(t)) + self:puts(tostring(t)) elseif self:alreadyVisited(t) then self:puts('
') elseif self.level >= self.depth then @@ -298,7 +298,7 @@ function Inspector:putValue(v) if tv == 'string' then self:puts(smartQuote(escape(v))) elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then - self:puts(inspect.tostring(v)) + self:puts(tostring(v)) elseif tv == 'table' then self:putTable(v) else diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 5aa1934..8d50c3f 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -414,9 +414,7 @@ describe( 'inspect', function() it('allows changing the global tostring', function() local save = _G.tostring - _G.tostring = function(x) - return inspect(x) - end + _G.tostring = inspect local s = tostring({1, 2, 3}) _G.tostring = save assert.equals("{ 1, 2, 3 }", s)