Lua中如何巧妙地获取函数参数个数,有高招吗?
- 内容介绍
- 文章标签
- 相关推荐
本文共计148个文字,预计阅读时间需要1分钟。
plaintextfunction getArgs(fun) local args={} local hook=debug.gethook() local argHook=function(...) for k, v in pairs(debug.getinfo(3)) do print(k, v) end if pcall ~=info.name then return end end for i=1, math.huge do local na endend
function getArgs(fun) local args = {} local hook = debug.gethook() local argHook = function(...) local info = debug.getinfo(3) for k,v in pairs(info) do print(k,v) end if "pcall" ~= info.name then return end for i = 1, math.huge do local name, value = debug.getlocal(2, i) print(i, name, value) if "(*temporary)" == name then debug.sethook(hook) error("") return end table.insert(args, name) end end debug.sethook(argHook, "c") pcall(fun) return args end function test(a, b, c) print(a,b,c) end local t = getArgs(test) for k,v in pairs(t) do print(k,v) end
本文共计148个文字,预计阅读时间需要1分钟。
plaintextfunction getArgs(fun) local args={} local hook=debug.gethook() local argHook=function(...) for k, v in pairs(debug.getinfo(3)) do print(k, v) end if pcall ~=info.name then return end end for i=1, math.huge do local na endend
function getArgs(fun) local args = {} local hook = debug.gethook() local argHook = function(...) local info = debug.getinfo(3) for k,v in pairs(info) do print(k,v) end if "pcall" ~= info.name then return end for i = 1, math.huge do local name, value = debug.getlocal(2, i) print(i, name, value) if "(*temporary)" == name then debug.sethook(hook) error("") return end table.insert(args, name) end end debug.sethook(argHook, "c") pcall(fun) return args end function test(a, b, c) print(a,b,c) end local t = getArgs(test) for k,v in pairs(t) do print(k,v) end

