如何转储所有_G表内容

我想要转储_G表。在_G表中含有其他表也需要转储(内联表)。并且我想要一个良好的格式。我有一个例子,但使用它来转储_G表会出现一些问题

function print_table(node)
-- 使输出变得美观
local function tab(amt)
    local str = ""
    for i=1,amt do
        str = str .. "\t"
    end
    return str
end

local cache, stack, output = {},{},{}
local depth = 1
local output_str = "{\n"

while true do
    local size = 0
    for k,v in pairs(node) do
        size = size + 1
    end

    local cur_index = 1
    for k,v in pairs(node) do
        if (cache[node] == nil) or (cur_index >= cache[node]) then

            if (string.find(output_str,"}",output_str:len())) then
                output_str = output_str .. ",\n"
            elseif not (string.find(output_str,"\n",output_str:len())) then
                output_str = output_str .. "\n"
            end

            -- 这对于使用巨大表进行工作是必要的,否则我们使用concat在巨大的字符串上运行出了内存
            table.insert(output,output_str)
            output_str = ""

            local key
            if (type(k) == "number" or type(k) == "boolean") then
                key = "[" ..type(k).. ":"..tostring(k).."]"
            else
                key = "['"..type(k).. ":"..tostring(k).."']"
            end

            if (type(v) == "number" or type(v) == "boolean") then
                output_str = output_str .. tab(depth) .. key .. " = "..tostring(v)
            elseif (type(v) == "table") then
                output_str = output_str .. tab(depth) .. key .. " = {\n"
                table.insert(stack,node)
                table.insert(stack,v)
                cache[node] = cur_index+1
                break
            else
                output_str = output_str .. tab(depth) .. key ..  " = '"..tostring(v).."'"
            end

            if (cur_index == size) then
                output_str = output_str .. "\n" .. tab(depth-1) .. "}"
            else
                output_str = output_str .. ","
            end
        else
            -- 关闭表
            if (cur_index == size) then
                output_str = output_str .. "\n" .. tab(depth-1) .. "}"
            end
        end

        cur_index = cur_index + 1
    end

    if (size == 0) then
        output_str = output_str .. "\n" .. tab(depth-1) .. "}"
    end

    if (#stack > 0) then
        node = stack[#stack]
        stack[#stack] = nil
        depth = cache[node] == nil and depth + 1 or depth - 1
    else
        break
    end
end

-- 这对于使用巨大表进行工作是必要的,否则我们使用concat在巨大的字符串上运行出了内存
table.insert(output,output_str)
output_str = table.concat(output)

print(output_str) end

这是结果日志

------开始-------
['function:add_msg_define'] = 'function: 0x7c6baa98',
['number:BLEND_DST_RGB'] = 32968,
['function:_attachShader'] = 'function: 0x7c626cf0',
['number:INVALID_OPERATION'] = 1282,
['function:drawArrays'] = 'function: 0x7c627480',
['number:SRGB8_ALPHA8_EXT'] = 35907,
['number:CCW'] = 2
------结束-------

我不知道如何修复这个函数

点赞
用户71376
用户71376

我使用这个:

-- deeply dumps the contents of the table and its contents' contents
function deepdump(tbl)
    local checklist = {}
    local function innerdump(tbl, indent)
        checklist[tostring(tbl)] = true
        for k, v in pairs(tbl) do
            print(indent .. k, v, type(v), checklist[tostring(tbl)])
            if type(v) == "table" and not checklist[tostring(v)] then
                innerdump(v, indent .. "    ")
            end
        end
    end
    print("=== DEEPDUMP -----")
    checklist[tostring(tbl)] = true
    innerdump(tbl, "")
    print("------------------")
end

https://gist.github.com/HoraceBury/9321964

2017-02-22 09:20:43