From ec524dfc6a89c51c95825372cef34856b5145282 Mon Sep 17 00:00:00 2001 From: nymphium Date: Fri, 13 Nov 2015 05:35:01 +0900 Subject: [PATCH 1/3] avoid __len metamethod --- inspect.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inspect.lua b/inspect.lua index b7107d4..60606e6 100644 --- a/inspect.lua +++ b/inspect.lua @@ -84,7 +84,7 @@ local function sortKeys(a, b) end local function getNonSequentialKeys(t) - local keys, length = {}, #t + local keys, length = {}, rawlen(t) for k,_ in pairs(t) do if not isSequenceKey(k, length) then table.insert(keys, k) end end @@ -232,7 +232,7 @@ function Inspector:putTable(t) if self.tableAppearances[t] > 1 then self:puts('<', self:getId(t), '>') end local nonSequentialKeys = getNonSequentialKeys(t) - local length = #t + local length = rawlen(t) local mt = getmetatable(t) local toStringResult = getToStringResultSafely(t, mt) From 6c1a22c207cd8f07493eed3ed37449f1555d4562 Mon Sep 17 00:00:00 2001 From: nymphium Date: Fri, 13 Nov 2015 05:58:04 +0900 Subject: [PATCH 2/3] implement simple rawlen function for Lua 5.1 --- inspect.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/inspect.lua b/inspect.lua index 60606e6..fce3f48 100644 --- a/inspect.lua +++ b/inspect.lua @@ -31,6 +31,19 @@ local inspect ={ inspect.KEY = setmetatable({}, {__tostring = function() return 'inspect.KEY' end}) inspect.METATABLE = setmetatable({}, {__tostring = function() return 'inspect.METATABLE' end}) +local rawlen = rawlen or function(t) + local _M, len = getmetatable(t) + if _M then + setmetatable(t, {}) + len = #t + setmetatable(t, _M) + else + len = #t + end + + return len +end + -- Apostrophizes the string if it has quotes, but not aphostrophes -- Otherwise, it returns a regular quoted string local function smartQuote(str) From 3392fa6314dff8568327936ab0455b15ca1c95eb Mon Sep 17 00:00:00 2001 From: nymphium Date: Fri, 20 Nov 2015 23:51:44 +0900 Subject: [PATCH 3/3] just use # for Lua 5.1 --- inspect.lua | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/inspect.lua b/inspect.lua index fce3f48..d6c159a 100644 --- a/inspect.lua +++ b/inspect.lua @@ -32,16 +32,7 @@ inspect.KEY = setmetatable({}, {__tostring = function() return 'inspect.KE inspect.METATABLE = setmetatable({}, {__tostring = function() return 'inspect.METATABLE' end}) local rawlen = rawlen or function(t) - local _M, len = getmetatable(t) - if _M then - setmetatable(t, {}) - len = #t - setmetatable(t, _M) - else - len = #t - end - - return len + return #t end -- Apostrophizes the string if it has quotes, but not aphostrophes