美文网首页
Lua语法 dump()函数

Lua语法 dump()函数

作者: 白小白152 | 来源:发表于2020-12-16 17:54 被阅读0次

cocos2d-x 3.4版本lua新增了方法dump(),
dump()为输出堆栈方法,可以方便的打印表table结构和对象内容,
如果使用低于3.4的版本,可以载入导入以下代码,自定义dump()函数

代码

function dump(value, description, nesting)
    if type(nesting) ~= "number" then nesting = 3 end

    local lookupTable = {}
    local result = {}

    local traceback = string.split(debug.traceback("", 2), "\n")
    print("dump from: " .. string.trim(traceback[3]))

    local function dump_(value, description, indent, nest, keylen)
        description = description or "<var>"
        local spc = ""
        if type(keylen) == "number" then
            spc = string.rep(" ", keylen - string.len(dump_value_(description)))
        end
        if type(value) ~= "table" then
            result[#result +1 ] = string.format("%s%s%s = %s", indent, dump_value_(description), spc, dump_value_(value))
        elseif lookupTable[tostring(value)] then
            result[#result +1 ] = string.format("%s%s%s = *REF*", indent, dump_value_(description), spc)
        else
            lookupTable[tostring(value)] = true
            if nest > nesting then
                result[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, dump_value_(description))
            else
                result[#result +1 ] = string.format("%s%s = {", indent, dump_value_(description))
                local indent2 = indent.."    "
                local keys = {}
                local keylen = 0
                local values = {}
                for k, v in pairs(value) do
                    keys[#keys + 1] = k
                    local vk = dump_value_(k)
                    local vkl = string.len(vk)
                    if vkl > keylen then keylen = vkl end
                    values[k] = v
                end
                table.sort(keys, function(a, b)
                    if type(a) == "number" and type(b) == "number" then
                        return a < b
                    else
                        return tostring(a) < tostring(b)
                    end
                end)
                for i, k in ipairs(keys) do
                    dump_(values[k], k, indent2, nest + 1, keylen)
                end
                result[#result +1] = string.format("%s}", indent)
            end
        end
    end
    dump_(value, description, "- ", 1)

    for i, line in ipairs(result) do
        print(line)
    end
end

-- input:要分割的字符串
-- delimiter:分隔符
function string.split(input, delimiter)
    input = tostring(input)
    delimiter = tostring(delimiter)
    if (delimiter=='') then return false end
    local pos,arr = 0, {}
    -- for each divider found
    for st,sp in function() return string.find(input, delimiter, pos, true) end do
        table.insert(arr, string.sub(input, pos, st - 1))
        pos = sp + 1
    end
    table.insert(arr, string.sub(input, pos))
    return arr
end

-- 删除字符串两端的空白字符
function string.trim(input)
    input = string.gsub(input, "^[ \t\n\r]+", "")
    return string.gsub(input, "[ \t\n\r]+$", "")
end

示例:

dump(self.keywords_info,"keywords_info=====")
输出结果为:
"keywords_info=====" = {
[LUA-print] -     1 = {
[LUA-print] -         "audio_cn"             = "scenes/story/2014/content/audio/word/chs/word_01.mp3"
[LUA-print] -         "audio_en"             = "scenes/story/2014/content/audio/word/eng/word_01.mp3"
[LUA-print] -         "audio_us"             = "scenes/story/2014/content/audio/word/us/word_01.mp3"
[LUA-print] -         "image_csb"            = "scenes/story/2014/content/game/option_huaban.csb"
[LUA-print] -         "image_parent_node"    = userdata: 0x011cd0f318
[LUA-print] -         "target_area"          = userdata: 0x01206e7c00
[LUA-print] -         "target_area_is_empty" = true
[LUA-print] -         "word"                 = "skateboard"
[LUA-print] -         "word_id"              = 1
[LUA-print] -     }
[LUA-print] -     2 = {
[LUA-print] -         "audio_cn"             = "scenes/story/2014/content/audio/word/chs/word_02.mp3"
[LUA-print] -         "audio_en"             = "scenes/story/2014/content/audio/word/eng/word_02.mp3"
[LUA-print] -         "audio_us"             = "scenes/story/2014/content/audio/word/us/word_02.mp3"
[LUA-print] -         "image_csb"            = "scenes/story/2014/content/game/option_qiuqian.csb"
[LUA-print] -         "image_parent_node"    = userdata: 0x011cd0f3d8
[LUA-print] -         "target_area"          = userdata: 0x01206e7b10
[LUA-print] -         "target_area_is_empty" = true
[LUA-print] -         "word"                 = "swing"
[LUA-print] -         "word_id"              = 2
[LUA-print] -     }
[LUA-print] - }

相关文章

  • Lua语法 dump()函数

    cocos2d-x 3.4版本lua新增了方法dump(),dump()为输出堆栈方法,可以方便的打印表table...

  • Lua 快速笔记(二) syntax

    参考programing in lua 语法 函数 常用函数 语法 局部变量和代码块 控制语句 Lua 认为 fa...

  • lua函数调用

    Lua 中的函数调用的语法如下: functioncall ::= prefixexp args 函数调用时,第一...

  • suricata lua脚本

    lua 脚本 语法: lua:[!] ; 脚本文件名将附加到您的默认规则位置。该脚本有2个部分,一个init函数和...

  • lua进阶

    lua官网在线运行代码 table面向对象语法糖 lua对table中的函数调用做了优化,使用起来像类方法,增加了...

  • 【IOS开发高级系列】Lua与OC交互专题

    1 Lua语法 Lua教程 http://www.yiibai.com/lua/lua_environment.h...

  • Lua语法基础之函数

    介绍演示

  • Lua

    让Xcode 支持 Lua 语法高亮 1.让Xcode支援Lua语法高亮(Syntax Highlighting)...

  • Lua中的CAPI概述

    头文件lua.h: Lua提供的基础函数,包括创建Lua环境,调用Lua函数,读写Lua环境中的全局变量,以及注册...

  • Lua 完全教程

    Lua 环境安装 Lua 基本语法 Lua 数据类型 Lua 数据类型:nil(空) Lua 数据类型:boole...

网友评论

      本文标题:Lua语法 dump()函数

      本文链接:https://www.haomeiwen.com/subject/eplogktx.html