|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function isCoroutineSafe(func) |
|
|
local co = coroutine.create(function() |
|
|
return func(coroutine.yield, function() end) |
|
|
end) |
|
|
|
|
|
coroutine.resume(co) |
|
|
return coroutine.resume(co) |
|
|
end |
|
|
|
|
|
|
|
|
if isCoroutineSafe(pcall) and isCoroutineSafe(xpcall) then |
|
|
_G.copcall = pcall |
|
|
_G.coxpcall = xpcall |
|
|
return { pcall = pcall, xpcall = xpcall, running = coroutine.running } |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local performResume |
|
|
local oldpcall, oldxpcall = pcall, xpcall |
|
|
local pack = table.pack or function(...) return {n = select("#", ...), ...} end |
|
|
local unpack = table.unpack or unpack |
|
|
local running = coroutine.running |
|
|
|
|
|
local coromap = setmetatable({}, { __mode = "k" }) |
|
|
|
|
|
local 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 |
|
|
|
|
|
|
|
|
local function id(trace, ...) |
|
|
return trace |
|
|
end |
|
|
|
|
|
function _G.coxpcall(f, err, ...) |
|
|
local current = running() |
|
|
if not current then |
|
|
if err == id then |
|
|
return oldpcall(f, ...) |
|
|
else |
|
|
if select("#", ...) > 0 then |
|
|
local oldf, params = f, pack(...) |
|
|
f = function() return oldf(unpack(params, 1, params.n)) end |
|
|
end |
|
|
return oldxpcall(f, err) |
|
|
end |
|
|
else |
|
|
local res, co = oldpcall(coroutine.create, f) |
|
|
if not res then |
|
|
local newf = function(...) return f(...) end |
|
|
co = coroutine.create(newf) |
|
|
end |
|
|
coromap[co] = current |
|
|
return performResume(err, co, ...) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
local function corunning(coro) |
|
|
if coro ~= nil then |
|
|
assert(type(coro)=="thread", "Bad argument; expected thread, got: "..type(coro)) |
|
|
else |
|
|
coro = running() |
|
|
end |
|
|
while coromap[coro] do |
|
|
coro = coromap[coro] |
|
|
end |
|
|
if coro == "mainthread" then return nil end |
|
|
return coro |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _G.copcall(f, ...) |
|
|
return coxpcall(f, id, ...) |
|
|
end |
|
|
|
|
|
return { pcall = copcall, xpcall = coxpcall, running = corunning } |
|
|
|