Reflection 检查Lua 5.1中的功能签名
在本发明中,提供了一种用于检查Lua函数的签名的方法。答复指出: 该算法适用于Lua5.2。旧版本类似,但不相同: Lua5.1中的等价物是什么Reflection 检查Lua 5.1中的功能签名,reflection,lua,lua-5.1,Reflection,Lua,Lua 5.1,在本发明中,提供了一种用于检查Lua函数的签名的方法。答复指出: 该算法适用于Lua5.2。旧版本类似,但不相同: Lua5.1中的等价物是什么 function funcsign(f) assert(type(f) == 'function', "bad argument #1 to 'funcsign' (function expected)") local p = {} pcall( function() local oldhook
function funcsign(f)
assert(type(f) == 'function', "bad argument #1 to 'funcsign' (function expected)")
local p = {}
pcall(
function()
local oldhook
local delay = 2
local function hook(event, line)
delay = delay - 1
if delay == 0 then
for i = 1, math.huge do
local k, v = debug.getlocal(2, i)
if type(v) == "table" then
table.insert(p, "...")
break
elseif (k or '('):sub(1, 1) == '(' then
break
else
table.insert(p, k)
end
end
if debug.getlocal(2, -1) then
table.insert(p, "...")
end
debug.sethook(oldhook)
error('aborting the call')
end
end
oldhook = debug.sethook(hook, "c")
local arg = {}
for j = 1, 64 do arg[#arg + 1] = true end
f((table.unpack or unpack)(arg))
end)
return "function("..table.concat(p, ",")..")"
end
用法:
local function somefunc(a, b, c, ...)
return
end
print(funcsign(somefunc))
Lua5.1中的链接答案几乎是正确的。唯一的区别在于vararg。签名
函数(x,y,…)
将显示为函数(x,y,arg)
。这对你很重要吗?