-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add. try.co module that implement protect function for coroutine.
- Loading branch information
Showing
5 changed files
with
43 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,5 +28,6 @@ build = { | |
try = { | ||
sources = { "src/try.c" }, | ||
}, | ||
["try.co"] = "src/lua/try/co.lua", | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
local try = require "try" | ||
|
||
local function _protect(co, status, ...) | ||
if not status then | ||
local msg = ... | ||
if type(msg) == 'table' then | ||
local k, v = next(msg) | ||
if k == try.NIL then | ||
if v == try.NIL then v = nil end | ||
return nil, v | ||
end | ||
end | ||
return error(msg, 0) | ||
end | ||
if coroutine.status(co) == "suspended" then | ||
return _protect(co, coroutine.resume(co, coroutine.yield(...))) | ||
else | ||
return ... | ||
end | ||
end | ||
|
||
local function protect(f) | ||
return function(...) | ||
local co = coroutine.create(f) | ||
return _protect(co, coroutine.resume(co, ...)) | ||
end | ||
end | ||
|
||
return setmetatable({ | ||
protect = protect | ||
},{__index = try}) |
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
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