Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix xpcall error in error handler #453

Merged
merged 3 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions _glua-tests/issues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,18 @@ function test()
assert(c == 1)
assert(type(c) == "number")
end
test()

-- 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()

-- issue #459
function test()
Expand Down
3 changes: 3 additions & 0 deletions _state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down