From c36614159d4118deccf99c99d63737864fa1ce41 Mon Sep 17 00:00:00 2001 From: XinyuGuan Date: Tue, 13 Oct 2026 20:30:37 +0000 Subject: [PATCH] Issue #551 : Accept callable tables as the first argument of xpcall --- _glua-tests/issues.lua | 11 +++++++++++ baselib.go | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/_glua-tests/issues.lua b/_glua-tests/issues.lua index 6d6343ab..b83e6bcb 100644 --- a/_glua-tests/issues.lua +++ b/_glua-tests/issues.lua @@ -490,3 +490,14 @@ function test() assert(b == nil) end test() + +-- xpcall with a callable table as the first argument +function test() + local f = setmetatable({}, {__call = function() return 42 end}) + local ok, v = xpcall(f, function(e) return e end) + assert(ok == true and v == 42) + local g = setmetatable({}, {__call = function() error("boom", 0) end}) + local ok, v = xpcall(g, function(e) return "handled: " .. e end) + assert(ok == false and v == "handled: boom") +end +test() diff --git a/baselib.go b/baselib.go index aa2c08a9..3074dd23 100644 --- a/baselib.go +++ b/baselib.go @@ -458,7 +458,7 @@ func baseUnpack(L *LState) int { } func baseXPCall(L *LState) int { - fn := L.CheckFunction(1) + fn := L.CheckAny(1) errfunc := L.CheckFunction(2) top := L.GetTop()