Merge pull request #27 from andreashofer123/tostring

fix endless recursion when using inspect to reimplement global tostring
master
Enrique García 9 years ago
commit 3300ae7847
  1. 6
      inspect.lua
  2. 9
      spec/inspect_spec.lua

@ -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)

@ -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)

Loading…
Cancel
Save