local oldpcall = pcall
function handleReturnValue(err, co, status, ...)
if not status then
return false, err(debug.traceback(co, (...)), ...)
end
if coroutine.status(co) == 'suspended' then
return performResume(err, co, coroutine.yield(...))
else
return true, ...
end
end
function performResume(err, co, ...)
return handleReturnValue(err, co, coroutine.resume(co, ...))
end
function coxpcall(f, err, ...)
local res, co = oldpcall(coroutine.create, f)
if not res then
local params = { ... }
local newf = function() return f(table.unpack(params)) end
co = coroutine.create(newf)
end
return performResume(err, co, ...)
end
local function id(_, ...)
return ...
end
function copcall(f, ...)
return coxpcall(f, id, ...)
end