Skip to content

Instantly share code, notes, and snippets.

@sofar
Created June 21, 2015 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sofar/88435ba1fad327e9b76a to your computer and use it in GitHub Desktop.
Save sofar/88435ba1fad327e9b76a to your computer and use it in GitHub Desktop.
estimate size of _G table elements in minetest
local s = ""
local function dd(ss)
s = s .. ss
end
-- alt version2, handles cycles, functions, booleans, etc
-- - abuse to http://richard.warburton.it
-- output almost identical to print(table.show(t)) below.
function print_r (t, name, indent)
local tableList = {}
function table_r (t, name, indent, full)
local serial=string.len(full) == 0 and name
or type(name)~="number" and '["'..tostring(name)..'"]' or '['..name..']'
dd(indent,serial,' = ')
if type(t) == "table" then
if tableList[t] ~= nil then dd('{}; -- ',tableList[t],' (self reference)\n')
else
tableList[t]=full..serial
if next(t) then -- Table not empty
dd('{\n')
for key,value in pairs(t) do table_r(value,key,indent..'\t',full..serial) end
dd(indent,'};\n')
else dd('{};\n') end
end
else dd(type(t)~="number" and type(t)~="boolean" and '"'..tostring(t)..'"'
or tostring(t),';\n') end
end
table_r(t,name or '__unnamed__',indent or '','')
local ss = s
s = ""
return ss
end
--
minetest.register_chatcommand("dump", {
params = "",
description = "Dump all objects in the global table",
func = function()
print("_G has "..#_G.." elements")
for k,v in pairs(_G) do
if k~="_G" then
if type(v)=="string" or type(v)=="number" then
print("G["..k.."]="..v)
elseif type(v)=="table" then
print("G["..k.."]=("..type(v)..")="..string.len(print_r(v)))
else
print("G["..k.."]=("..type(v)..")")
end
end
end
return true, "Table dumped to console"
end
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment