|
local function cowrap(f)
local thread = coroutine.create(f)
local closed = false
local function doclose()
if not closed then
coroutine.close(thread)
closed = true
end
end
return setmetatable({},{
__call = function(_,...)
return coroutine.resume(thread,...)
end,
__close=doclose,
__gc=doclose,
})
end