Lua 调试打印表数据(LUA DUMP TABLE)

📅 发布时间:2026/7/16 11:57:31 👁️ 浏览次数:
Lua 调试打印表数据(LUA DUMP TABLE)
-- -- Safe Dump v2.8 - DEBUG-- 核心特性-- 表首行注释-- table: 0x... 直接使用 tostring 清理后结果-- 循环引用注释-- cycle ref - table: 0x... 地址与目标表首行完全匹配-- 函数注释含地址/类型/名称/参数/文件/行号完整调试信息-- 严格无尾随逗号 精准4空格缩进-- 所有注释 ASCII 安全100% 语法合法-- -- 标准库函数局部化严格下划线命名localdebug_getinfodebug.getinfolocaltype_functypelocaltostring_functostringlocalpairs_funcpairslocalstring_formatstring.formatlocalstring_repstring.replocalstring_gsubstring.gsublocalstring_bytestring.bytelocaltable_inserttable.insertlocaltable_concattable.concatlocaltable_sorttable.sortlocalmath_hugemath.huge-- 预计算转义映射localESCAPE_MAP{[]\\,[\\]\\\\,[\n]\\n,[\r]\\r,[\t]\\t,[\b]\\b,[\f]\\f}-- 清理注释仅保留 ASCII 32-126确保所有注释安全可解析localfunctionclean_comment_ascii(s)return(string_gsub(s,[^\32-\126], ))end-- 键排序规则localfunctioncompare_keys(a,b)localta,tbtype_func(a),type_func(b)iftanumberandtbnumberthenreturnabendiftanumberthenreturntrueendiftbnumberthenreturnfalseendiftastringandtbstringthenreturnabendreturntostring_func(a)tostring_func(b)end-- 核心转换函数localfunctionvalue_to_string(v,visited,current_indent)current_indentcurrent_indentor0localttype_func(v)-- 基础类型iftnilthenreturnnilelseiftbooleanthenreturnvandtrueorfalseelseiftnumberthenifv~vthenreturn0/0endifvmath_hugethenreturnmath.hugeendifv-math_hugethenreturn-math.hugeendreturnstring_format(%.17g,v)elseiftstringthenlocalescapedstring_gsub(v,[%c\\],function(c)localescESCAPE_MAP[c]ifescthenreturnescendlocalbstring_byte(c)returnb32andstring_format(\\%03d,b)orcend)return..escaped..-- 函数完整调试信息地址/类型/名称/参数/文件/行号elseiftfunctionthenlocaladdr_partclean_comment_ascii(tostring_func(v))-- function: 0x...localdesc_parts{addr_part}localok,infopcall(debug_getinfo,v,Slnu)ifokandinfothentable_insert(desc_parts,(info.whatCandCorLua).. func)ifinfo.nameandinfo.name~?thentable_insert(desc_parts,name..clean_comment_ascii(info.name)..)endifinfo.nparamsthentable_insert(desc_parts,info.nparams..(info.isvarargandor).. params)endifinfo.short_srcandinfo.linedefinedandinfo.linedefined0thenlocalsrcclean_comment_ascii(info.short_src)ifinfo.linedefinedinfo.lastlinedefinedthentable_insert(desc_parts,string_format(file%s:%d,src,info.linedefined))elsetable_insert(desc_parts,string_format(file%s:%d-%d,src,info.linedefined,info.lastlinedefined))endendendreturnnil,table_concat(desc_parts, | )-- userdata / thread直接使用 tostring 地址elseiftuserdatathenreturnnil,clean_comment_ascii(tostring_func(v))-- userdata: 0x...elseiftthreadthenreturnnil,clean_comment_ascii(tostring_func(v))-- thread: 0x...-- 表处理核心纯净地址注释 循环引用精准指向elseifttablethen-- 检测循环引用返回指向目标表的注释地址与目标表首行完全匹配ifvisited[v]thenreturnnil,clean_comment_ascii(cycle ref - ..tostring_func(v))endvisited[v]truelocalnext_indentcurrent_indent4localfield_indentstring_rep( ,next_indent)localparts{}-- 【关键】首行添加表地址注释-- table: 0x... 直接使用 tostring 清理结果localtable_descclean_comment_ascii(tostring_func(v))table_insert(parts,field_indent..-- ..table_desc)-- 收集并排序键localkeys{}forkinpairs_func(v)dotable_insert(keys,k)endtable_sort(keys,compare_keys)-- 生成字段精准处理末行逗号localkey_count#keysforidx,kinipairs(keys)dolocalis_last(idxkey_count)-- 处理键localkey_striftype_func(k)stringandk:match(^[a-zA-Z_][a-zA-Z0-9_]*$)thenkey_strkelselocalk_val,k_commentvalue_to_string(k,visited,next_indent)ifk_commentthenkey_str[..k_val.., -- ..k_comment..]elsekey_str[..k_val..]endend-- 处理值localval_str,val_commentvalue_to_string(v[k],visited,next_indent)-- 构建字段行末行无逗号locallineifis_lastthenifval_commentthenlinefield_indent..key_str.. ..val_str.. -- ..val_commentelselinefield_indent..key_str.. ..val_strendelseifval_commentthenlinefield_indent..key_str.. ..val_str.., -- ..val_commentelselinefield_indent..key_str.. ..val_str..,endendtable_insert(parts,line)endvisited[v]nil-- 清除标记-- 闭合括号精准对齐localclose_indentstring_rep( ,current_indent)return{\n..table_concat(parts,\n)..\n..close_indent..},nilelsereturnnil,clean_comment_ascii(unknown type ..t)endend-- 主转储函数localfunctiondump(value,name,base_indent)base_indentbase_indentor0localprefixstring_rep( ,base_indent)localresultprefixifname~niltheniftype_func(name)stringandname:match(^[a-zA-Z_][a-zA-Z0-9_]*$)thenresultresult..name.. elselocalname_val,name_commentvalue_to_string(name,{},base_indent)ifname_commentthenresultresult..[..name_val.., -- ..name_comment..] elseresultresult..[..name_val..] endendendlocalval_str,val_commentvalue_to_string(value,{},base_indent)ifval_commentthenresultresult..val_str.., -- ..val_commentelseresultresult..val_strendreturnresultend-- 验证函数localfunctionvalidate_dump()localtest_data{strA\B\nC\tTest,num3.141592653589793,nested{xtrue,y{zend}},[sp ace]99,[true]bool-key,fnprint,cycle{}}test_data.cycle.reftest_data.cyclelocalcodereturn ..dump(test_data)localloader,errload(code,safe_dump_validate,t,{})ifnotloaderthenio.stderr:write(语法验证失败错误:\n..tostring(err)..\n\n生成的代码:\n..code..\n)returnfalseendlocalok,respcall(loader)ifnotokthenio.stderr:write(执行验证失败: ..tostring(res)..\n)returnfalseendifnot(res.strA\B\nC\tTestandres.num3.141592653589793andres.nested.xtrueandres.nested.y.zendandres[sp ace]99andres[true]bool-keyandres.fnnilandres.cycle.refnil)thenio.stderr:write(语义验证失败数据还原不匹配\n)returnfalseendio.write(SafeDump 验证通过语法与语义均正确\n)print(\n生成的验证代码示例调试)print(code:sub(8,-1))returntrueend-- 模块导出localsafe_dump_module{dumpdump,validatevalidate_dump,_VERSIONSafeDump v2.8,_DESCRIPTION调试表地址 | 函数全信息 | 循环精准指向 | 零语法风险}-- 直接运行测试含 Lua 函数示例ifargandarg[0]nilthenprint( SafeDump v2.8 测试 \n)-- Lua 函数详细信息测试核心示例print(-- Lua 函数含完整调试信息--)localfunctioncalculate_sum(a,b,...)localtotalabfori1,select(#,...)dototaltotalselect(i,...)endreturntotalendprint(safe_dump_module.dump(calculate_sum,calculate_sum))-- C 函数测试print(\n-- C 函数print--)print(safe_dump_module.dump(print,printFunc))-- 表地址标注测试print(\n-- 表/数组标准地址标注--)localconfig{name测试配置,timeout30,servers{127.0.0.1,192.168.1.1},callbackfunction()end}print(safe_dump_module.dump(config,config))-- 循环引用精准指向测试print(\n-- 循环引用地址精准匹配--)localroot{idROOT_TABLE}localchild{idCHILD_TABLE,parentroot}root.childchild root.self_refrootprint(safe_dump_module.dump(root,root))-- 验证测试print(\n-- 语法与语义验证 --)safe_dump_module.validate()os.exit(0)endreturnsafe_dump_module