+
+local function id(trace, ...)
+ return trace
+end
+
+function coxpcall(f, err, ...)
+ local current = coroutine.running()
+ if not current then
+ if err == id then
+ return pcall(f, ...)
+ else
+ if select("#", ...) > 0 then
+ local oldf, params = f, { ... }
+ f = function() return oldf(unpack(params)) end
+ end
+ return xpcall(f, err)
+ end
+ else
+ local res, co = pcall(coroutine.create, f)
+ if not res then
+ local newf = function(...) return f(...) end
+ co = coroutine.create(newf)
+ end
+ coromap[co] = current
+ coxpt[co] = coxpt[current] or current or 0
+ return performResume(err, co, ...)
+ end
+end
+
+function copcall(f, ...)
+ return coxpcall(f, id, ...)
+end