From daaefbd87055d47cb2c7dc202cb90d3c8224c1ef Mon Sep 17 00:00:00 2001 From: mpeterv Date: Fri, 27 Nov 2015 14:04:02 +0300 Subject: [PATCH] Do not allow collecting weak tables while they are being inspected Fixes #22 --- inspect.lua | 4 ++-- spec/inspect_spec.lua | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/inspect.lua b/inspect.lua index 186730e..a611558 100644 --- a/inspect.lua +++ b/inspect.lua @@ -114,14 +114,14 @@ local maxIdsMetaTable = { local idsMetaTable = { __index = function (self, typeName) - local col = setmetatable({}, {__mode = "kv"}) + local col = {} rawset(self, typeName, col) return col end } local function countTableAppearances(t, tableAppearances) - tableAppearances = tableAppearances or setmetatable({}, {__mode = "k"}) + tableAppearances = tableAppearances or {} if type(t) == 'table' then if not tableAppearances[t] then diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 5f9d46e..d44030f 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -350,6 +350,31 @@ describe( 'inspect', function() ]]), inspect(bar)) end) + it('does not allow collecting weak tables while they are being inspected', function() + collectgarbage('stop') + finally(function() collectgarbage('restart') end) + 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 + = <1>{ + __index = , + __mode = "v", + __tostring = + } + }, + = + } + ]]), inspect(t)) + end) + describe('When a table is its own metatable', function() it('accepts a table that is its own metatable without stack overflowing', function() local x = {}