From 729b5dbf31b26e5b5705b43620e9ec0beb95da9e Mon Sep 17 00:00:00 2001 From: mzki <28139969+mzki@users.noreply.github.com> Date: Sat, 12 Aug 2023 08:14:21 +0000 Subject: [PATCH 1/2] Issue #452 : fix xpcall with error in error handler returns (nil, nil) --- _glua-tests/issues.lua | 11 +++++++++++ _state.go | 3 +++ state.go | 3 +++ state_test.go | 22 ++++++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/_glua-tests/issues.lua b/_glua-tests/issues.lua index 514d0b7a..7abcd936 100644 --- a/_glua-tests/issues.lua +++ b/_glua-tests/issues.lua @@ -457,3 +457,14 @@ function test() assert(c == 1) assert(type(c) == "number") end + +-- issue #452 +function test() + local ok, msg = pcall(function() + local ok, msg = xpcall(function() error("fn") end, function(err) error("handler") end) + assert(not ok and msg) + error("expected to reach this.") + end) + assert(not ok) +end +test() \ No newline at end of file diff --git a/_state.go b/_state.go index 645589fe..df169259 100644 --- a/_state.go +++ b/_state.go @@ -1855,6 +1855,9 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) { err = rcv.(*ApiError) err.(*ApiError).StackTrace = ls.stackTrace(0) } + ls.stack.SetSp(sp) + ls.currentFrame = ls.stack.Last() + ls.reg.SetTop(base) } }() ls.Call(1, 1) diff --git a/state.go b/state.go index 50e70ed6..8e0acbb3 100644 --- a/state.go +++ b/state.go @@ -2014,6 +2014,9 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) { err = rcv.(*ApiError) err.(*ApiError).StackTrace = ls.stackTrace(0) } + ls.stack.SetSp(sp) + ls.currentFrame = ls.stack.Last() + ls.reg.SetTop(base) } }() ls.Call(1, 1) diff --git a/state_test.go b/state_test.go index bfe93b93..7b7aa53d 100644 --- a/state_test.go +++ b/state_test.go @@ -291,17 +291,39 @@ func TestPCall(t *testing.T) { })) errorIfFalse(t, strings.Contains(err.Error(), "by handler"), "") + L.Push(L.GetGlobal("f1")) err = L.PCall(0, 0, L.NewFunction(func(L *LState) int { L.RaiseError("error!") return 1 })) errorIfFalse(t, strings.Contains(err.Error(), "error!"), "") + L.Push(L.GetGlobal("f1")) err = L.PCall(0, 0, L.NewFunction(func(L *LState) int { panic("panicc!") return 1 })) errorIfFalse(t, strings.Contains(err.Error(), "panicc!"), "") + + // Issue #452, expected to be revert back to previous call stack after any error. + currentFrame, currentTop, currentSp := L.currentFrame, L.GetTop(), L.stack.Sp() + L.Push(L.GetGlobal("f1")) + err = L.PCall(0, 0, nil) + errorIfFalse(t, err != nil, "") + errorIfFalse(t, L.currentFrame == currentFrame, "") + errorIfFalse(t, L.GetTop() == currentTop, "") + errorIfFalse(t, L.stack.Sp() == currentSp, "") + + currentFrame, currentTop, currentSp = L.currentFrame, L.GetTop(), L.stack.Sp() + L.Push(L.GetGlobal("f1")) + err = L.PCall(0, 0, L.NewFunction(func(L *LState) int { + L.RaiseError("error!") + return 1 + })) + errorIfFalse(t, err != nil, "") + errorIfFalse(t, L.currentFrame == currentFrame, "") + errorIfFalse(t, L.GetTop() == currentTop, "") + errorIfFalse(t, L.stack.Sp() == currentSp, "") } func TestCoroutineApi1(t *testing.T) { From 6543bc9e02b9557b5638b11b548a2d32b60dcc66 Mon Sep 17 00:00:00 2001 From: mzki <28139969+mzki@users.noreply.github.com> Date: Sat, 12 Aug 2023 08:17:23 +0000 Subject: [PATCH 2/2] add missing call of test() for #423 --- _glua-tests/issues.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/_glua-tests/issues.lua b/_glua-tests/issues.lua index 7abcd936..0bb77bcd 100644 --- a/_glua-tests/issues.lua +++ b/_glua-tests/issues.lua @@ -457,6 +457,7 @@ function test() assert(c == 1) assert(type(c) == "number") end +test() -- issue #452 function test()