From 783eec9b4592ff3fa54ffa1a855dda2a71f2db64 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 13 Mar 2019 20:55:24 -0700 Subject: Made iteration easier to work with by using .__inext and .__next for custom iteration, and a custom ipairs() and pairs() to use that. --- builtin_metatables.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'builtin_metatables.lua') diff --git a/builtin_metatables.lua b/builtin_metatables.lua index 63d865c..3b29e39 100644 --- a/builtin_metatables.lua +++ b/builtin_metatables.lua @@ -186,8 +186,11 @@ co_mt = { return math.huge end, __call = coroutine.resume, - __next = function(self, k) + __inext = function(self, k) local ok, val = coroutine.resume(self) + if coroutine.status(self) == 'dead' then + return + end if ok then return (k or 0) + 1, val end @@ -207,6 +210,7 @@ co_mt = { return co_mt[k] end } +co_mt.__next = co_mt.__inext debug.setmetatable(coroutine.create(function() end), co_mt) local nil_mt = { __type = "Nil", -- cgit v1.2.3