Skip to content

Commit 9d7d921

Browse files
authored
Merge pull request yuin#453 from mzki/fix-xpcall-error-in-error-handler
Fix xpcall error in error handler
2 parents 018eaa0 + 293e22e commit 9d7d921

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

_glua-tests/issues.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,18 @@ function test()
457457
assert(c == 1)
458458
assert(type(c) == "number")
459459
end
460+
test()
461+
462+
-- issue #452
463+
function test()
464+
local ok, msg = pcall(function()
465+
local ok, msg = xpcall(function() error("fn") end, function(err) error("handler") end)
466+
assert(not ok and msg)
467+
error("expected to reach this.")
468+
end)
469+
assert(not ok)
470+
end
471+
test()
460472

461473
-- issue #459
462474
function test()

_state.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,9 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
18551855
err = rcv.(*ApiError)
18561856
err.(*ApiError).StackTrace = ls.stackTrace(0)
18571857
}
1858+
ls.stack.SetSp(sp)
1859+
ls.currentFrame = ls.stack.Last()
1860+
ls.reg.SetTop(base)
18581861
}
18591862
}()
18601863
ls.Call(1, 1)

state.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,9 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
20142014
err = rcv.(*ApiError)
20152015
err.(*ApiError).StackTrace = ls.stackTrace(0)
20162016
}
2017+
ls.stack.SetSp(sp)
2018+
ls.currentFrame = ls.stack.Last()
2019+
ls.reg.SetTop(base)
20172020
}
20182021
}()
20192022
ls.Call(1, 1)

state_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,17 +291,39 @@ func TestPCall(t *testing.T) {
291291
}))
292292
errorIfFalse(t, strings.Contains(err.Error(), "by handler"), "")
293293

294+
L.Push(L.GetGlobal("f1"))
294295
err = L.PCall(0, 0, L.NewFunction(func(L *LState) int {
295296
L.RaiseError("error!")
296297
return 1
297298
}))
298299
errorIfFalse(t, strings.Contains(err.Error(), "error!"), "")
299300

301+
L.Push(L.GetGlobal("f1"))
300302
err = L.PCall(0, 0, L.NewFunction(func(L *LState) int {
301303
panic("panicc!")
302304
return 1
303305
}))
304306
errorIfFalse(t, strings.Contains(err.Error(), "panicc!"), "")
307+
308+
// Issue #452, expected to be revert back to previous call stack after any error.
309+
currentFrame, currentTop, currentSp := L.currentFrame, L.GetTop(), L.stack.Sp()
310+
L.Push(L.GetGlobal("f1"))
311+
err = L.PCall(0, 0, nil)
312+
errorIfFalse(t, err != nil, "")
313+
errorIfFalse(t, L.currentFrame == currentFrame, "")
314+
errorIfFalse(t, L.GetTop() == currentTop, "")
315+
errorIfFalse(t, L.stack.Sp() == currentSp, "")
316+
317+
currentFrame, currentTop, currentSp = L.currentFrame, L.GetTop(), L.stack.Sp()
318+
L.Push(L.GetGlobal("f1"))
319+
err = L.PCall(0, 0, L.NewFunction(func(L *LState) int {
320+
L.RaiseError("error!")
321+
return 1
322+
}))
323+
errorIfFalse(t, err != nil, "")
324+
errorIfFalse(t, L.currentFrame == currentFrame, "")
325+
errorIfFalse(t, L.GetTop() == currentTop, "")
326+
errorIfFalse(t, L.stack.Sp() == currentSp, "")
305327
}
306328

307329
func TestCoroutineApi1(t *testing.T) {

0 commit comments

Comments
 (0)