diff options
-rw-r--r-- | lcorolib.c | 3 | ||||
-rw-r--r-- | manual/manual.of | 7 | ||||
-rw-r--r-- | testes/coroutine.lua | 7 |
3 files changed, 11 insertions, 6 deletions
@@ -146,7 +146,8 @@ static int luaB_costatus (lua_State *L) { | |||
146 | 146 | ||
147 | 147 | ||
148 | static int luaB_yieldable (lua_State *L) { | 148 | static 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 | ||
6312 | Returns true when the running coroutine can yield. | 6312 | Returns true when the coroutine @id{co} can yield. |
6313 | The default for @id{co} is the running coroutine. | ||
6313 | 6314 | ||
6314 | A running coroutine is yieldable if it is not the main thread and | 6315 | A coroutine is yieldable if it is not the main thread and |
6315 | it is not inside a non-yieldable @N{C function}. | 6316 | it 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 | |||
10 | local main, ismain = coroutine.running() | 10 | local main, ismain = coroutine.running() |
11 | assert(type(main) == "thread" and ismain) | 11 | assert(type(main) == "thread" and ismain) |
12 | assert(not coroutine.resume(main)) | 12 | assert(not coroutine.resume(main)) |
13 | assert(not coroutine.isyieldable()) | 13 | assert(not coroutine.isyieldable(main) and not coroutine.isyieldable()) |
14 | assert(not pcall(coroutine.yield)) | 14 | assert(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, ...) | |||
46 | end | 46 | end |
47 | 47 | ||
48 | f = coroutine.create(foo) | 48 | f = coroutine.create(foo) |
49 | assert(coroutine.isyieldable(f)) | ||
49 | assert(type(f) == "thread" and coroutine.status(f) == "suspended") | 50 | assert(type(f) == "thread" and coroutine.status(f) == "suspended") |
50 | assert(string.find(tostring(f), "thread")) | 51 | assert(string.find(tostring(f), "thread")) |
51 | local s,a,b,c,d | 52 | local s,a,b,c,d |
52 | s,a,b,c,d = coroutine.resume(f, {1,2,3}, {}, {1}, {'a', 'b', 'c'}) | 53 | s,a,b,c,d = coroutine.resume(f, {1,2,3}, {}, {1}, {'a', 'b', 'c'}) |
54 | assert(coroutine.isyieldable(f)) | ||
53 | assert(s and a == nil and coroutine.status(f) == "suspended") | 55 | assert(s and a == nil and coroutine.status(f) == "suspended") |
54 | s,a,b,c,d = coroutine.resume(f) | 56 | s,a,b,c,d = coroutine.resume(f) |
55 | eqtab(_G.x, {}) | 57 | eqtab(_G.x, {}) |
56 | assert(s and a == 1 and b == nil) | 58 | assert(s and a == 1 and b == nil) |
59 | assert(coroutine.isyieldable(f)) | ||
57 | s,a,b,c,d = coroutine.resume(f, 1, 2, 3) | 60 | s,a,b,c,d = coroutine.resume(f, 1, 2, 3) |
58 | eqtab(_G.x, {1, 2, 3}) | 61 | eqtab(_G.x, {1, 2, 3}) |
59 | assert(s and a == 'a' and b == 'b' and c == 'c' and d == nil) | 62 | assert(s and a == 'a' and b == 'b' and c == 'c' and d == nil) |