metoo icon indicating copy to clipboard operation
metoo copied to clipboard

tostring打印lua table时,遇到循环引用的时候,会陷入死循环,服务器会挂掉!

Open cpejlgc opened this issue 8 years ago • 2 comments

luaext.lua

cpejlgc avatar Nov 14 '16 06:11 cpejlgc

@cpejlgc 可以参考云风的print_r函数(没有循环引用问题), 自己做一下修改, 代码见http://blog.codingnow.com/cloud/LuaPrintR

cls1991 avatar Nov 25 '16 10:11 cls1991

这个问题我也遇到了,这里贡献下我修改后的版本,可以解决循环引用的BUG:


-- @brief: table 格式化为字符串
-- @param: obj:table, 要 dump 的表
-- @param: maxLev:number, 打印的最大层级
-- @return: string, 返回格式化后的字符串
local function dumptable(obj, maxLev)
    maxLev = maxLev or 10
    --[[
    -- 触发元表
    local mt = getmetatable(obj)
    if mt and mt.__tostring then
        return mt.__tostring(obj)
    end]]

    local used = {}
    local getIndent, quoteStr, wrapKey, wrapVal, dumpObj
    getIndent = function(level)
        return string.rep("\t", level)
    end
    quoteStr = function(str)
        return '"' .. string.gsub(str, '"', '\\"') .. '"'
    end
    wrapKey = function(val)
        if type(val) == "number" then
            return "[" .. val .. "]"
        elseif type(val) == "string" then
            return "[" .. quoteStr(val) .. "]"
        else
            return "[" .. tostring(val) .. "]"
        end
    end
    wrapVal = function(val, level)
        if type(val) == "table" then
            return dumpObj(val, level)
        elseif type(val) == "number" then
            return val
        elseif type(val) == "string" then
            return quoteStr(val)
        else
            return tostring(val)
        end
    end
    dumpObj = function(obj, level)
        if type(obj) ~= "table" then
            return wrapVal(obj)
        end

        if used[obj] then
            return "*REF*" -- 重复引用
        end

        -- local objTostring = rawget(obj, "tostring")
        -- if objTostring then
        --     return objTostring
        -- end

        level = level + 1

        if level >= maxLev then
            return "*MAX LEVEL*"
        end

        used[obj] = true

        local tokens = {}
        tokens[#tokens + 1] = "{"
        for k, v in pairs(obj) do
            tokens[#tokens + 1] = getIndent(level) .. wrapKey(k) .. " = " .. wrapVal(v, level) .. ","
        end
        tokens[#tokens + 1] = getIndent(level - 1) .. "}"
        return table.concat(tokens, "\n")
    end
    return dumpObj(obj, 0)
end

zhouyanlt avatar Apr 08 '18 09:04 zhouyanlt