Debug information about extra arguments from __call

'debug.getinfo' can return number of extra arguments added to a call by
a chain of __call metavalues. That information is being used to improve
error messages about errors in these extra arguments.
This commit is contained in:
Roberto Ierusalimschy
2024-11-19 14:09:18 -03:00
parent b117bdb344
commit 50c7c915ee
9 changed files with 83 additions and 12 deletions

View File

@@ -117,6 +117,31 @@ else
return 1
]]
assert(string.find(res, "xuxu.-main chunk"))
do -- tests for error messages about extra arguments from __call
local function createobj (n)
-- function that raises an error on its n-th argument
local code = string.format("argerror %d 'msg'", n)
local func = T.makeCfunc(code)
-- create a chain of 2 __call objects
local M = setmetatable({}, {__call = func})
M = setmetatable({}, {__call = M})
-- put it as a method for a new object
return {foo = M}
end
_G.a = createobj(1) -- error in first (extra) argument
checkmessage("a:foo()", "bad extra argument #1")
_G.a = createobj(2) -- error in second (extra) argument
checkmessage("a:foo()", "bad extra argument #2")
_G.a = createobj(3) -- error in self (after two extra arguments)
checkmessage("a:foo()", "bad self")
_G.a = createobj(4) -- error in first regular argument (after self)
checkmessage("a:foo()", "bad argument #1")
end
end