Merge pull request #27 from andreashofer123/tostring

fix endless recursion when using inspect to reimplement global tostring
This commit is contained in:
Enrique García 2016-04-08 19:26:28 +02:00
commit 3300ae7847
2 changed files with 13 additions and 2 deletions

View File

@ -28,6 +28,8 @@ local inspect ={
]] ]]
} }
local tostring = tostring
inspect.KEY = setmetatable({}, {__tostring = function() return 'inspect.KEY' end}) inspect.KEY = setmetatable({}, {__tostring = function() return 'inspect.KEY' end})
inspect.METATABLE = setmetatable({}, {__tostring = function() return 'inspect.METATABLE' end}) inspect.METATABLE = setmetatable({}, {__tostring = function() return 'inspect.METATABLE' end})
@ -202,7 +204,7 @@ function Inspector:puts(...)
local len = #buffer local len = #buffer
for i=1, #args do for i=1, #args do
len = len + 1 len = len + 1
buffer[len] = tostring(args[i]) buffer[len] = args[i]
end end
end end
@ -228,7 +230,7 @@ function Inspector:getId(v)
self.maxIds[tv] = id self.maxIds[tv] = id
self.ids[tv][v] = id self.ids[tv][v] = id
end end
return id return tostring(id)
end end
function Inspector:putKey(k) function Inspector:putKey(k)

View File

@ -417,4 +417,13 @@ describe( 'inspect', function()
end) end)
end) end)
end) end)
it('allows changing the global tostring', function()
local save = _G.tostring
_G.tostring = inspect
local s = tostring({1, 2, 3})
_G.tostring = save
assert.equals("{ 1, 2, 3 }", s)
end)
end) end)