-- 标准库函数局部化严格 GNU 下划线命名规范 localdebug_libdebuglocaldebug_getinfodebug_libanddebug_lib.getinfolocaldebug_getlocaldebug_libanddebug_lib.getlocallocaltype_functypelocaltostring_functostringlocalpairs_funcpairslocalstring_formatstring.formatlocalstring_repstring.replocalstring_gsubstring.gsublocalstring_bytestring.bytelocaltable_inserttable.insertlocaltable_concattable.concatlocaltable_sorttable.sortlocalmath_hugemath.hugelocalprint_funcprintlocalio_flushioandio.flush-- Dump 模块核心安全值序列化 -- 预计算转义映射localESCAPE_MAP{[]\\,[\\]\\\\,[\n]\\n,[\r]\\r,[\t]\\t,[\b]\\b,[\f]\\f}-- ASCII 清理保留 32-126localfunctionclean_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,concise_mode)current_indentcurrent_indentor0localttype_func(v)-- 基础类型处理简洁模式复用iftnilthenreturnnilendiftbooleanthenreturnvandtrueorfalseendiftnumberthenifv~vthenreturn0/0endifvmath_hugethenreturnmath.hugeendifv-math_hugethenreturn-math.hugeendreturnstring_format(%.17g,v)endiftstringthenifconcise_modethenlocallimitedv:sub(1,100)localescapedstring_format(%q,limited)returnescaped..(v:len()100and...or)elselocalescapedstring_gsub(v,[%c\\],function(c)localescESCAPE_MAP[c]ifescthenreturnescendlocalbstring_byte(c)returnb32andstring_format(\\%03d,b)orcend)return..escaped..endend-- 简洁模式快速返回类型标识堆栈跟踪专用ifconcise_modetheniftfunctionthenreturnfunctionendiftuserdatathenreturnuserdataendiftthreadthenreturnthreadendifttablethenreturntableendreturn..t..end-- 深度模式完整序列化Dump 专用iftfunctionthenlocaladdr_partclean_comment_ascii(tostring_func(v))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, | )endiftuserdataortthreadthenreturnnil,clean_comment_ascii(tostring_func(v))endifttablethenifvisited[v]thenreturnnil,clean_comment_ascii(cycle ref - ..tostring_func(v))endvisited[v]truelocalnext_indentcurrent_indent4localfield_indentstring_rep( ,next_indent)localparts{}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)key_strk_commentand([..k_val.., -- ..k_comment..])or([..k_val..])endlocalval_str,val_commentvalue_to_string(v[k],visited,next_indent)locallinefield_indent..key_str.. ..val_strifnotis_lastthenlineline..,endifval_commentthenlineline.. -- ..val_commentendtable_insert(parts,line)endvisited[v]nillocalclose_indentstring_rep( ,current_indent)return{\n..table_concat(parts,\n)..\n..close_indent..},nilendreturnnil,clean_comment_ascii(unknown type ..t)end-- Dump 主函数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)localname_partname_commentand([..name_val.., -- ..name_comment..])or([..name_val..])resultresult..name_part.. endendlocalval_str,val_commentvalue_to_string(value,{},base_indent)resultresult..val_strifval_commentthenresultresult.., -- ..val_commentendreturnresultend-- 堆栈跟踪模块复用 value_to_string 简洁模式 localfunctioncapture_stack_trace()localframes{at:}ifnotdebug_getinfothentable_insert(frames,#0: ERROR: debug library unavailable)returntable_concat(frames,\n)endlocallevel2localindex0localcaptured_anyfalsewhiletruedolocalsuccess,infopcall(debug_getinfo,level,nSlu)ifnotsuccessornotinfothenbreakend-- 函数名处理localfunc_nameinfo.nameandinfo.name~andinfo.nameor(info.whatmainandmainorinfo.whatCandCorinfo.whatLuaandanonymousorinfo.whator?)-- 文件/行号处理localfile_name(info.short_srcor?):gsub(^,)localline_num(info.currentlineandinfo.currentline0)andinfo.currentlineor?-- 参数捕获使用简洁模式序列化localparam_parts{}ifdebug_getlocalandinfo.nparamsandinfo.nparams0thenfori1,info.nparamsdolocalok,name,valuepcall(debug_getlocal,level,i)ifokandnameandname~thenlocalval_strvalue_to_string(value,nil,0,true)-- concise_modetruetable_insert(param_parts,string_format(%s%s,name,val_str))elsetable_insert(param_parts,string_format(param%d%s,i,okandunnamedorerror))endendendlocalparams_str#param_parts0andtable_concat(param_parts,, )ornil-- 构建帧字符串localframe_strparams_strandstring_format(#%d: %s (%s:%s) [%s],index,func_name,file_name,line_num,params_str)orstring_format(#%d: %s (%s:%s),index,func_name,file_name,line_num)table_insert(frames,frame_str)captured_anytruelevellevel1indexindex1endifnotcaptured_anythentable_insert(frames,#0: WARNING: stack captured but no frames retrieved)endreturntable_concat(frames,\n)endlocalfunctionprint_stack_trace()localtracecapture_stack_trace()iftraceand#trace0thenprint_func(trace)elseprint_func(at:\n#0: CRITICAL: capture_stack_trace returned empty string)endifio_flushthenio_flush()endend-- 模块导出 localsafe_dump_module{dumpdump,-- 深度转储多行带注释capture_stack_tracecapture_stack_trace,-- 堆栈捕获含简洁参数print_stack_traceprint_stack_trace,-- 直接打印堆栈-- 内部函数导出可选供高级使用value_to_stringvalue_to_string,-- 支持 concise_mode 参数}returnsafe_dump_module