Added an optional parameter to 'coroutine.isyieldable'

This commit is contained in:
Roberto Ierusalimschy 2019-04-10 13:23:14 -03:00
parent 8ba4523ccc
commit a93e014447
3 changed files with 11 additions and 6 deletions

View File

@ -146,7 +146,8 @@ static int luaB_costatus (lua_State *L) {
static int luaB_yieldable (lua_State *L) { static int luaB_yieldable (lua_State *L) {
lua_pushboolean(L, lua_isyieldable(L)); lua_State *co = lua_isnone(L, 1) ? L : getco(L);
lua_pushboolean(L, lua_isyieldable(co));
return 1; return 1;
} }

View File

@ -6307,11 +6307,12 @@ an object with type @T{"thread"}.
} }
@LibEntry{coroutine.isyieldable ()| @LibEntry{coroutine.isyieldable ([co])|
Returns true when the running coroutine can yield. Returns true when the coroutine @id{co} can yield.
The default for @id{co} is the running coroutine.
A running coroutine is yieldable if it is not the main thread and A coroutine is yieldable if it is not the main thread and
it is not inside a non-yieldable @N{C function}. it is not inside a non-yieldable @N{C function}.
} }

View File

@ -10,7 +10,7 @@ local f
local main, ismain = coroutine.running() local main, ismain = coroutine.running()
assert(type(main) == "thread" and ismain) assert(type(main) == "thread" and ismain)
assert(not coroutine.resume(main)) assert(not coroutine.resume(main))
assert(not coroutine.isyieldable()) assert(not coroutine.isyieldable(main) and not coroutine.isyieldable())
assert(not pcall(coroutine.yield)) assert(not pcall(coroutine.yield))
@ -38,7 +38,7 @@ function foo (a, ...)
assert(coroutine.resume(f) == false) assert(coroutine.resume(f) == false)
assert(coroutine.status(f) == "running") assert(coroutine.status(f) == "running")
local arg = {...} local arg = {...}
assert(coroutine.isyieldable()) assert(coroutine.isyieldable(x))
for i=1,#arg do for i=1,#arg do
_G.x = {coroutine.yield(table.unpack(arg[i]))} _G.x = {coroutine.yield(table.unpack(arg[i]))}
end end
@ -46,14 +46,17 @@ function foo (a, ...)
end end
f = coroutine.create(foo) f = coroutine.create(foo)
assert(coroutine.isyieldable(f))
assert(type(f) == "thread" and coroutine.status(f) == "suspended") assert(type(f) == "thread" and coroutine.status(f) == "suspended")
assert(string.find(tostring(f), "thread")) assert(string.find(tostring(f), "thread"))
local s,a,b,c,d local s,a,b,c,d
s,a,b,c,d = coroutine.resume(f, {1,2,3}, {}, {1}, {'a', 'b', 'c'}) s,a,b,c,d = coroutine.resume(f, {1,2,3}, {}, {1}, {'a', 'b', 'c'})
assert(coroutine.isyieldable(f))
assert(s and a == nil and coroutine.status(f) == "suspended") assert(s and a == nil and coroutine.status(f) == "suspended")
s,a,b,c,d = coroutine.resume(f) s,a,b,c,d = coroutine.resume(f)
eqtab(_G.x, {}) eqtab(_G.x, {})
assert(s and a == 1 and b == nil) assert(s and a == 1 and b == nil)
assert(coroutine.isyieldable(f))
s,a,b,c,d = coroutine.resume(f, 1, 2, 3) s,a,b,c,d = coroutine.resume(f, 1, 2, 3)
eqtab(_G.x, {1, 2, 3}) eqtab(_G.x, {1, 2, 3})
assert(s and a == 'a' and b == 'b' and c == 'c' and d == nil) assert(s and a == 'a' and b == 'b' and c == 'c' and d == nil)