From b611db6bfa9c12ce35dd4972032fbbd2ad5ba965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Fri, 6 Apr 2018 23:14:00 +0200 Subject: [PATCH] allow using inspect inside __tostring metamethods without errors --- inspect.lua | 16 ---------------- spec/inspect_spec.lua | 37 ++++++++++--------------------------- 2 files changed, 10 insertions(+), 43 deletions(-) diff --git a/inspect.lua b/inspect.lua index f4d0e96..e2e3806 100644 --- a/inspect.lua +++ b/inspect.lua @@ -124,16 +124,6 @@ local function getNonSequentialKeys(t) return keys, keysLength, sequenceLength end -local function getToStringResultSafely(t, mt) - local __tostring = type(mt) == 'table' and rawget(mt, '__tostring') - local str, ok - if type(__tostring) == 'function' then - ok, str = pcall(__tostring, t) - str = ok and str or 'error: ' .. tostring(str) - end - if type(str) == 'string' and #str > 0 then return str end -end - local function countTableAppearances(t, tableAppearances) tableAppearances = tableAppearances or {} @@ -254,15 +244,9 @@ function Inspector:putTable(t) local nonSequentialKeys, nonSequentialKeysLength, sequenceLength = getNonSequentialKeys(t) local mt = getmetatable(t) - local toStringResult = getToStringResultSafely(t, mt) self:puts('{') self:down(function() - if toStringResult then - self:puts(' -- ', escape(toStringResult)) - if sequenceLength >= 1 then self:tabify() end - end - local count = 0 for i=1, sequenceLength do if count > 0 then self:puts(',') end diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 9e61bc1..edea720 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -363,32 +363,17 @@ describe( 'inspect', function() ]]), inspect(bar)) end) - it('includes the __tostring metamethod if it exists', function() - local foo = { foo = 1, __tostring = function() return 'hello\nworld' end } - local bar = setmetatable({a = 1}, foo) + it('can be used on the __tostring metamethod of a table without errors', function() + local f = function(x) return inspect(x) end + local tbl = setmetatable({ x = 1 }, { __tostring = f }) assert.equals(unindent([[ - { -- hello\nworld - a = 1, + { + x = 1, = { - __tostring = , - foo = 1 + __tostring = } } - ]]), inspect(bar)) - end) - - it('includes an error string if __tostring metamethod throws an error', function() - local foo = { foo = 1, __tostring = function() error('hello', 0) end } - local bar = setmetatable({a = 1}, foo) - assert.equals(unindent([[ - { -- error: hello - a = 1, - = { - __tostring = , - foo = 1 - } - } - ]]), inspect(bar)) + ]]), tostring(tbl)) end) it('does not allow collecting weak tables while they are being inspected', function() @@ -397,18 +382,16 @@ describe( 'inspect', function() local shimMetatable = { __mode = 'v', __index = function() return {} end, - __tostring = function() collectgarbage() return 'shim' end } local function shim() return setmetatable({}, shimMetatable) end local t = shim() t.key = shim() assert.equals(unindent([[ - { -- shim - key = { -- shim + { + key = { = <1>{ __index = , - __mode = "v", - __tostring = + __mode = "v" } }, =