aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2019-03-09 15:51:52 -0800
committerBruce Hill <bruce@bruce-hill.com>2019-03-09 15:52:00 -0800
commit68993a5ffb784a7648f886bb3221703aaaf000ce (patch)
tree5c6f7dddcca103c2a8c234515b21962e1765ca3e
parent58707b18f7d0e1c7526d7c6d39110b52d25a9e4f (diff)
Added support for iteration over coroutines
-rw-r--r--builtin_metatables.lua36
-rw-r--r--builtin_metatables.moon20
2 files changed, 47 insertions, 9 deletions
diff --git a/builtin_metatables.lua b/builtin_metatables.lua
index f659ad3..63d865c 100644
--- a/builtin_metatables.lua
+++ b/builtin_metatables.lua
@@ -1,9 +1,6 @@
require("text")
local number_mt = {
__type = "a Number",
- __len = function(self)
- return self
- end,
as_lua = tostring,
as_nomsu = tostring,
as_text = tostring,
@@ -176,13 +173,40 @@ local fn_mt = {
}
fn_mt.__index = fn_mt
debug.setmetatable((function() end), fn_mt)
-local co_mt = {
+local _last_co_i = setmetatable({ }, {
+ __mode = 'k'
+})
+local co_mt
+co_mt = {
__type = "a Coroutine",
as_text = function(self)
- return (tostring(self):gsub("thread", "Coroutine"))
+ return (tostring(self):gsub("thread", "Coroutine")) .. " (" .. coroutine.status(self) .. ")"
+ end,
+ __len = function(self)
+ return math.huge
+ end,
+ __call = coroutine.resume,
+ __next = function(self, k)
+ local ok, val = coroutine.resume(self)
+ if ok then
+ return (k or 0) + 1, val
+ end
+ end,
+ __index = function(self, k)
+ if k == (_last_co_i[self] or 0) + 1 then
+ local ret = {
+ coroutine.resume(self, k)
+ }
+ _last_co_i[self] = k
+ if ret[1] then
+ return table.unpack(ret, 2)
+ else
+ return nil
+ end
+ end
+ return co_mt[k]
end
}
-co_mt.__index = co_mt
debug.setmetatable(coroutine.create(function() end), co_mt)
local nil_mt = {
__type = "Nil",
diff --git a/builtin_metatables.moon b/builtin_metatables.moon
index ac8beff..7af5bf7 100644
--- a/builtin_metatables.moon
+++ b/builtin_metatables.moon
@@ -3,7 +3,6 @@ require "text"
number_mt =
__type: "a Number"
- __len: => @
as_lua: tostring
as_nomsu: tostring
as_text: tostring
@@ -74,10 +73,25 @@ fn_mt =
fn_mt.__index = fn_mt
debug.setmetatable (->), fn_mt
+_last_co_i = setmetatable({}, {__mode:'k'})
+local co_mt
co_mt =
__type: "a Coroutine"
- as_text: => (tostring(@)\gsub("thread", "Coroutine"))
-co_mt.__index = co_mt
+ as_text: => (tostring(@)\gsub("thread", "Coroutine")).." ("..coroutine.status(@)..")"
+ __len: => math.huge
+ __call: coroutine.resume
+ __next: (k)=>
+ ok, val = coroutine.resume(@)
+ if ok then return (k or 0) + 1, val
+ __index: (k)=>
+ if k == (_last_co_i[@] or 0) + 1
+ ret = {coroutine.resume(@, k)}
+ _last_co_i[@] = k
+ if ret[1]
+ return table.unpack(ret, 2)
+ else
+ return nil
+ return co_mt[k]
debug.setmetatable(coroutine.create(->), co_mt)
nil_mt =