Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local insp={}
- assert( type(insp)=="table" )
- function InspectValue(value,history,indent,inside)
- res=""
- for vkey,vvalue in pairs(value) do
- local vvtype=type(vvalue)
- res=res..(string.rep(" ",indent)..tostring(vkey).." ["..tostring(vvalue).."]").."\n"
- if not history[vvalue] then
- if vvtype=="table" then
- res=res..__Inspect(vvalue,indent+2,history,inside)
- elseif vvtype=="function" then
- res=res..InspectFunction(vvalue,history,indent+2,inside)
- end --if vvtype
- end --if not hist
- history[vvalue]=true
- end --for vkey
- return res
- end
- function InspectFunction(value,history,indent,inside)
- res=""
- local ftab=debug.getinfo(value,"S")
- if ftab~=nil then
- res=res..(string.rep(" ",indent).."Function info:").."\n"
- res=res..__Inspect(ftab,indent+1,history,false,inside)
- end
- return res
- end
- --reverse lookup of values that are defined in an environment
- function __InsideG()
- local ins={}
- for key,value in pairs(_G) do
- ins[value]=true
- end
- return ins
- end
- function ProperLevel(T,inside,value)
- if T~=_G and inside[value] then return false end
- return true
- end
- local InBlackList = {
- InspectValue=true,
- InspectFunction=true,
- __InsideG=true,
- ProperLevel=true,
- __Inspect=true,
- Inspect=true,
- InBlackList=true,
- source=true
- }
- function __Inspect(T,ind,hist,meta,InsideG)
- local res=""
- local inside=InsideG or __InsideG()
- if T==nil then return "nil" end
- if (type(T)~="table") then return res end
- local indent=ind or 0
- local history=hist or {}
- if (type(history)~="table") then history={} end
- history[T]=true
- for key,value in pairs(T) do
- if not InBlackList[key] then
- res=res..(string.rep(" ",indent)..tostring(key).." ["..tostring(value).."]").."\n"
- if value and not history[value] and ProperLevel(T,inside,value) then
- history[value]=true
- if type(value)=="table" then
- res=res..InspectValue(value,history,indent+1,inside)
- elseif debug and type(value)=="function" then
- res=res..InspectFunction(value,history,indent+1,inside)
- end --if type(value)
- local mt=nil
- if meta then mt=getmetatable(value) end
- if mt then
- if type(mt)=="table" then
- res=res..(string.rep(" ",indent+1).."Metatable:").."\n"
- res=res..__Inspect(mt,indent+2,history,meta,inside)
- else
- res=res..tostring(mt).."\n"
- end
- end
- end
- end
- end
- return res
- end
- -- inspects the table recursively (Inspect(_G) inspects the environment)
- function insp.Inspect(T)
- return __Inspect(T,nil,nil,true,nil)
- end
- --return insp
- local Inspector=insp --require("insp")
- print(Inspector.Inspect(_G))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement