Advertisement
dled

a recursive Lua table inspector

May 16th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.60 KB | None | 0 0
  1. local insp={}
  2.  
  3. assert( type(insp)=="table" )
  4.  
  5. function InspectValue(value,history,indent,inside)
  6.  res=""
  7.  for vkey,vvalue in pairs(value) do
  8.   local vvtype=type(vvalue)
  9.   res=res..(string.rep(" ",indent)..tostring(vkey).." ["..tostring(vvalue).."]").."\n"
  10.   if not history[vvalue] then
  11.    if vvtype=="table" then
  12.     res=res..__Inspect(vvalue,indent+2,history,inside)
  13.    elseif vvtype=="function" then
  14.     res=res..InspectFunction(vvalue,history,indent+2,inside)
  15.    end --if vvtype
  16.   end --if not hist
  17.   history[vvalue]=true
  18.  end --for vkey
  19.  return res
  20. end
  21.  
  22. function InspectFunction(value,history,indent,inside)
  23.  res=""
  24.  local ftab=debug.getinfo(value,"S")
  25.  if ftab~=nil then
  26.   res=res..(string.rep(" ",indent).."Function info:").."\n"
  27.   res=res..__Inspect(ftab,indent+1,history,false,inside)
  28.  end
  29.  return res
  30. end
  31.  
  32. --reverse lookup of values that are defined in an environment
  33. function __InsideG()
  34.  local ins={}
  35.   for key,value in pairs(_G) do
  36.    ins[value]=true
  37.   end
  38.  return ins
  39. end
  40.  
  41. function ProperLevel(T,inside,value)
  42.  if T~=_G and inside[value] then return false end
  43.  return true
  44. end
  45.  
  46. local InBlackList = {
  47.  InspectValue=true,
  48.  InspectFunction=true,
  49.  __InsideG=true,
  50.  ProperLevel=true,
  51.  __Inspect=true,
  52.  Inspect=true,
  53.  InBlackList=true,
  54.  source=true
  55. }
  56.  
  57. function __Inspect(T,ind,hist,meta,InsideG)
  58.  local res=""
  59.  local inside=InsideG or __InsideG()
  60.  if T==nil then return "nil" end
  61.  if (type(T)~="table") then return res end
  62.  
  63.  local indent=ind or 0
  64.  local history=hist or {}
  65.  
  66.  if (type(history)~="table") then history={} end
  67.  
  68.  history[T]=true
  69.  for key,value in pairs(T) do
  70.   if not InBlackList[key] then
  71.    res=res..(string.rep(" ",indent)..tostring(key).." ["..tostring(value).."]").."\n"
  72.    if value and not history[value] and ProperLevel(T,inside,value) then
  73.     history[value]=true
  74.     if type(value)=="table" then
  75.      res=res..InspectValue(value,history,indent+1,inside)
  76.     elseif debug and type(value)=="function" then
  77.      res=res..InspectFunction(value,history,indent+1,inside)
  78.     end --if type(value)
  79.     local mt=nil
  80.     if meta then mt=getmetatable(value) end
  81.     if mt then
  82.      if type(mt)=="table" then
  83.       res=res..(string.rep(" ",indent+1).."Metatable:").."\n"
  84.       res=res..__Inspect(mt,indent+2,history,meta,inside)
  85.      else
  86.       res=res..tostring(mt).."\n"
  87.      end
  88.     end
  89.    end
  90.   end
  91.  end
  92.  return res
  93. end
  94.  
  95. -- inspects the table recursively (Inspect(_G) inspects the environment)
  96. function insp.Inspect(T)
  97.  return __Inspect(T,nil,nil,true,nil)
  98. end
  99.  
  100. --return insp
  101.  
  102. local Inspector=insp --require("insp")
  103. print(Inspector.Inspect(_G))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement