From f9575fd443b124053f4e4be21916323326be943e Mon Sep 17 00:00:00 2001 From: Cheyi Lin Date: Wed, 19 Jun 2013 19:33:16 +0800 Subject: [PATCH 1/4] arbitrary type support (e.g., LuaJIT's cdata) --- inspect.lua | 60 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/inspect.lua b/inspect.lua index 6320f91..db5230e 100644 --- a/inspect.lua +++ b/inspect.lua @@ -5,6 +5,17 @@ -- inspired by http://lua-users.org/wiki/TableSerialization ----------------------------------------------------------------------------------------------------------------------- +local type = type +local ipairs, pairs = ipairs, pairs +local string = string +local table = table +local tostring = tostring +local rawget = rawget +local pcall = pcall +local getmetatable, setmetatable = getmetatable, setmetatable +local rawget, rawset = rawget, rawset +local math = math + local inspect ={} inspect.__VERSION = '1.2.0' @@ -34,19 +45,24 @@ local function isIdentifier(str) end local function isArrayKey(k, length) - return type(k)=='number' and 1 <= k and k <= length + return type(k) == 'number' and 1 <= k and k <= length end local function isDictionaryKey(k, length) return not isArrayKey(k, length) end -local sortOrdersByType = { +local sortOrdersByType = setmetatable({ ['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4, ['function'] = 5, ['userdata'] = 6, ['thread'] = 7 -} +}, +{ __index = function (t, k) + if not rawget(t, k) then + return math.huge + end +end }) -local function sortKeys(a,b) +local function sortKeys(a, b) local ta, tb = type(a), type(b) if ta ~= tb then return sortOrdersByType[ta] < sortOrdersByType[tb] end if ta == 'string' or ta == 'number' then return a < b end @@ -57,7 +73,7 @@ local function getDictionaryKeys(t) local length = #t local keys = {} for k,_ in pairs(t) do - if isDictionaryKey(k, length) then table.insert(keys,k) end + if isDictionaryKey(k, length) then table.insert(keys, k) end end table.sort(keys, sortKeys) return keys @@ -73,6 +89,26 @@ local function getToStringResultSafely(t, mt) return string end +local inspectorMaxIdsMetaTable = { + __index = function (t, k) + if not rawget(t, k) then + rawset(t, k, 0) + end + return 0 + end +} + +local inspectorIdsMetaTable = { + __index = function (t, k) + local v = rawget(t, k) + if not v then + rawset(t, k, setmetatable({}, {__mode = "kv"})) + v = rawget(t, k) + end + return v + end +} + local Inspector = {} function Inspector:new(t, depth) @@ -80,18 +116,8 @@ function Inspector:new(t, depth) buffer = {}, depth = depth, level = 0, - maxIds = { - ['function'] = 0, - ['userdata'] = 0, - ['thread'] = 0, - ['table'] = 0 - }, - ids = { - ['function'] = setmetatable({}, {__mode = "kv"}), - ['userdata'] = setmetatable({}, {__mode = "kv"}), - ['thread'] = setmetatable({}, {__mode = "kv"}), - ['table'] = setmetatable({}, {__mode = "kv"}) - }, + maxIds = setmetatable({}, inspectorMaxIdsMetaTable), + ids = setmetatable({}, inspectorIdsMetaTable), tableAppearances = setmetatable({}, {__mode = "k"}) } From 2a0214c0b707c22ea13233e5394bad0da5db894f Mon Sep 17 00:00:00 2001 From: Cheyi Lin Date: Wed, 19 Jun 2013 22:59:34 +0800 Subject: [PATCH 2/4] remove duplicated declaration --- inspect.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/inspect.lua b/inspect.lua index db5230e..dd86321 100644 --- a/inspect.lua +++ b/inspect.lua @@ -10,7 +10,6 @@ local ipairs, pairs = ipairs, pairs local string = string local table = table local tostring = tostring -local rawget = rawget local pcall = pcall local getmetatable, setmetatable = getmetatable, setmetatable local rawget, rawset = rawget, rawset From 59e989480dbfebfb43f539323fba2ce95928af6d Mon Sep 17 00:00:00 2001 From: Cheyi Lin Date: Thu, 27 Jun 2013 18:03:26 +0800 Subject: [PATCH 3/4] remove local declarations at the top --- inspect.lua | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/inspect.lua b/inspect.lua index dd86321..acb1392 100644 --- a/inspect.lua +++ b/inspect.lua @@ -5,16 +5,6 @@ -- inspired by http://lua-users.org/wiki/TableSerialization ----------------------------------------------------------------------------------------------------------------------- -local type = type -local ipairs, pairs = ipairs, pairs -local string = string -local table = table -local tostring = tostring -local pcall = pcall -local getmetatable, setmetatable = getmetatable, setmetatable -local rawget, rawset = rawget, rawset -local math = math - local inspect ={} inspect.__VERSION = '1.2.0' From e6e4d07e5178c038241a5426e631ed5485c3643d Mon Sep 17 00:00:00 2001 From: Cheyi Lin Date: Thu, 27 Jun 2013 18:05:19 +0800 Subject: [PATCH 4/4] add luajit cdata test --- spec/inspect_spec.lua | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 71a15f3..c0ae8c8 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -1,4 +1,5 @@ local inspect = require 'inspect' +local is_luajit, ffi = pcall(require, 'ffi') describe( 'inspect', function() @@ -42,6 +43,14 @@ describe( 'inspect', function() assert.equals(inspect(false), 'false') end) + if is_luajit then + describe('luajit cdata', function() + it('works with luajit cdata', function() + assert.equals(inspect({ ffi.new("int", 1), ffi.typeof("int"), ffi.typeof("int")(1) }), '{ , , }') + end) + end) + end + describe('tables', function() it('works with simple array-like tables', function() @@ -61,7 +70,7 @@ describe( 'inspect', function() [print] = 1, ["buy more"] = 1, a = 1, [14] = 1, [{c=2}] = 1, [true]= 1 } - assert.equals(inspect(t), [[{ 1, 2, 3, + local s = [[{ 1, 2, 3, [14] = 1, [true] = 1, a = 1, @@ -69,8 +78,12 @@ describe( 'inspect', function() [{ c = 2 }] = 1, - [] = 1 -}]]) + [] = 1]] + if is_luajit then + t[ffi.new("int", 1)] = 1 + s = s .. ",\n [] = 1" + end + assert.equals(inspect(t), s .. "\n}") end) it('works with nested dictionary tables', function()