From 2b502e1a8113e15338f008acd6214a71aa148475 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 21 Jan 2013 00:49:22 +0100 Subject: [PATCH] fixed metatable-related loop when a table is its own index. References #4 --- inspect.lua | 2 +- spec/inspect_spec.lua | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/inspect.lua b/inspect.lua index f01a6b2..b507663 100644 --- a/inspect.lua +++ b/inspect.lua @@ -64,7 +64,7 @@ local function getDictionaryKeys(t) end local function getToStringResultSafely(t, mt) - local __tostring = type(mt) == 'table' and mt.__tostring + local __tostring = type(mt) == 'table' and rawget(mt, '__tostring') local string, status if type(__tostring) == 'function' then status, string = pcall(__tostring, t) diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 174a735..71a15f3 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -192,14 +192,26 @@ describe( 'inspect', function() }]]) end) - it('accepts a table that is its own metatable without stack overflowing', function() - local x = {} - setmetatable(x,x) - assert.equals(inspect(x), [[<1>{ + describe('When a table is its own metatable', function() + it('accepts a table that is its own metatable without stack overflowing', function() + local x = {} + setmetatable(x,x) + assert.equals(inspect(x), [[<1>{ = }]]) - end) + end) + it('can invoke the __tostring method without stack overflowing', function() + local t = {} + t.__index = t + setmetatable(t,t) + assert.equals(inspect(t), [[<1>{ + __index =
, + =
+}]]) + end) + + end) end) end) end)