aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lcorolib.c3
-rw-r--r--manual/manual.of7
-rw-r--r--testes/coroutine.lua7
3 files changed, 11 insertions, 6 deletions
diff --git a/lcorolib.c b/lcorolib.c
index cdb5fedc..f7c9e165 100644
--- a/lcorolib.c
+++ b/lcorolib.c
@@ -146,7 +146,8 @@ static int luaB_costatus (lua_State *L) {
146 146
147 147
148static int luaB_yieldable (lua_State *L) { 148static int luaB_yieldable (lua_State *L) {
149 lua_pushboolean(L, lua_isyieldable(L)); 149 lua_State *co = lua_isnone(L, 1) ? L : getco(L);
150 lua_pushboolean(L, lua_isyieldable(co));
150 return 1; 151 return 1;
151} 152}
152 153
diff --git a/manual/manual.of b/manual/manual.of
index 6da2e494..fea6922e 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -6307,11 +6307,12 @@ an object with type @T{"thread"}.
6307 6307
6308} 6308}
6309 6309
6310@LibEntry{coroutine.isyieldable ()| 6310@LibEntry{coroutine.isyieldable ([co])|
6311 6311
6312Returns true when the running coroutine can yield. 6312Returns true when the coroutine @id{co} can yield.
6313The default for @id{co} is the running coroutine.
6313 6314
6314A running coroutine is yieldable if it is not the main thread and 6315A coroutine is yieldable if it is not the main thread and
6315it is not inside a non-yieldable @N{C function}. 6316it is not inside a non-yieldable @N{C function}.
6316 6317
6317} 6318}
diff --git a/testes/coroutine.lua b/testes/coroutine.lua
index a4321bed..35ff27fb 100644
--- a/testes/coroutine.lua
+++ b/testes/coroutine.lua
@@ -10,7 +10,7 @@ local f
10local main, ismain = coroutine.running() 10local main, ismain = coroutine.running()
11assert(type(main) == "thread" and ismain) 11assert(type(main) == "thread" and ismain)
12assert(not coroutine.resume(main)) 12assert(not coroutine.resume(main))
13assert(not coroutine.isyieldable()) 13assert(not coroutine.isyieldable(main) and not coroutine.isyieldable())
14assert(not pcall(coroutine.yield)) 14assert(not pcall(coroutine.yield))
15 15
16 16
@@ -38,7 +38,7 @@ function foo (a, ...)
38 assert(coroutine.resume(f) == false) 38 assert(coroutine.resume(f) == false)
39 assert(coroutine.status(f) == "running") 39 assert(coroutine.status(f) == "running")
40 local arg = {...} 40 local arg = {...}
41 assert(coroutine.isyieldable()) 41 assert(coroutine.isyieldable(x))
42 for i=1,#arg do 42 for i=1,#arg do
43 _G.x = {coroutine.yield(table.unpack(arg[i]))} 43 _G.x = {coroutine.yield(table.unpack(arg[i]))}
44 end 44 end
@@ -46,14 +46,17 @@ function foo (a, ...)
46end 46end
47 47
48f = coroutine.create(foo) 48f = coroutine.create(foo)
49assert(coroutine.isyieldable(f))
49assert(type(f) == "thread" and coroutine.status(f) == "suspended") 50assert(type(f) == "thread" and coroutine.status(f) == "suspended")
50assert(string.find(tostring(f), "thread")) 51assert(string.find(tostring(f), "thread"))
51local s,a,b,c,d 52local s,a,b,c,d
52s,a,b,c,d = coroutine.resume(f, {1,2,3}, {}, {1}, {'a', 'b', 'c'}) 53s,a,b,c,d = coroutine.resume(f, {1,2,3}, {}, {1}, {'a', 'b', 'c'})
54assert(coroutine.isyieldable(f))
53assert(s and a == nil and coroutine.status(f) == "suspended") 55assert(s and a == nil and coroutine.status(f) == "suspended")
54s,a,b,c,d = coroutine.resume(f) 56s,a,b,c,d = coroutine.resume(f)
55eqtab(_G.x, {}) 57eqtab(_G.x, {})
56assert(s and a == 1 and b == nil) 58assert(s and a == 1 and b == nil)
59assert(coroutine.isyieldable(f))
57s,a,b,c,d = coroutine.resume(f, 1, 2, 3) 60s,a,b,c,d = coroutine.resume(f, 1, 2, 3)
58eqtab(_G.x, {1, 2, 3}) 61eqtab(_G.x, {1, 2, 3})
59assert(s and a == 'a' and b == 'b' and c == 'c' and d == nil) 62assert(s and a == 'a' and b == 'b' and c == 'c' and d == nil)