当前位置 : 主页 > 网络编程 > lua >

Lua中巧妙的获取函数的参数个数

来源:互联网 收集:自由互联 发布时间:2021-06-23
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 na
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
网友评论