Better error messages for calling non-callable objects

When available, use the calling code to find a suitable name for what
was being called; this is particularly useful for errors of non-callable
metamethods. This commit also improved the debug information for
order metamethods.
This commit is contained in:
Roberto Ierusalimschy
2020-12-29 13:15:54 -03:00
parent 59e565d955
commit 4bd10b6fe8
6 changed files with 52 additions and 11 deletions

View File

@@ -459,7 +459,22 @@ do -- errors due to non-closable values
getmetatable(xyz).__close = nil -- remove metamethod
end
local stat, msg = pcall(foo)
assert(not stat and string.find(msg, "attempt to call a nil value"))
assert(not stat and string.find(msg, "metamethod 'close'"))
local function foo ()
local a1 <close> = func2close(function (_, msg)
assert(string.find(msg, "number value"))
error(12)
end)
local a2 <close> = setmetatable({}, {__close = print})
local a3 <close> = func2close(function (_, msg)
assert(msg == nil)
error(123)
end)
getmetatable(a2).__close = 4 -- invalidate metamethod
end
local stat, msg = pcall(foo)
assert(not stat and msg == 12)
end