-
-
Save daurnimator/a0288074b747a4c26d16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local coros = setmetatable({}, {__mode="kv"}) | |
local function new_thread(func, onerror) | |
local co = coroutine.create(func) | |
local function resumer(...) | |
local ok, err = coroutine.resume(co, ...) | |
if not ok then | |
onerror(co, err) | |
end | |
end | |
coros[co] = resumer | |
return resumer | |
end | |
local function resumer() | |
local co = coroutine.running() | |
return coros[co] | |
end | |
function sleep(ms) | |
setTimeout(ms, resumer()) | |
return coroutine.yield() | |
end | |
local t = new_thread(function(...) | |
print("A", ...) | |
sleep(1000) | |
print("B") | |
error("oops!") | |
end, function(co, err) | |
print("an error occured!", debug.traceback(co, err)) | |
end)("myargs") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment