2

I'm modeling Go's defer/panic/recover in luajit 2.1.0-beta3, and it would be nice to be able to recursively use xpcall, since Go's deferred functions can themselves panic, and functions with defers regularly call other functions with defers. I'm trying this:

-- recursive xpcalls, do they work? hmm... looks like we get "error in error handling"

ouch=function() error("ouch") end

ouch2=function() error("ouch2") end

ok=function() return "ok" end

h2 = function(err)
   print("panicHandler2 running with err =", err) -- hmm, can't get this to call?
end

h = function(err)
   print("panicHandler running with err =", err)
   g = {xpcall(ouch2, h2)}
   for k,v in pairs(g) do print("g result of recursive xpcall is k=",k," val=",v) end
   -- g result of recursive xpcall is k=    1    val=   false
   -- g result of recursive xpcall is k=    2    val=   error in error handling
end

r={xpcall(ouch, h)}

for k,v in pairs(r) do print("r result of top xpcall is k=",k," val=",v) end


-- output:

$ luajit
LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
JIT: ON SSE2 SSE3 SSE4.1 BMI2 fold cse dce fwd dse narrow loop abc sink fuse
> dofile 'rexp.lua'
panicHandler running with err = rexp.lua:3: ouch
g result of recursive xpcall is k=  1    val=   false
g result of recursive xpcall is k=  2    val=   error in error handling
r result of top xpcall is k=    1    val=   false
> 

So instead of h2 being called, I see "error in error handling".

Is there a way to make recursive xpcalls work?

The above was built using clang on OSX, using the default Makefile with XCFLAGS+= -DLUAJIT_ENABLE_LUA52COMPAT uncommented. So its a C built, not a C++ build, on macOS.

Update: works on PUC Lua5.1.5 and 5.3.4, h2 gets called.

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.