diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-05-08 10:52:20 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-05-08 10:52:20 -0300 |
commit | 1bd70a8e40a8600658c706d5f171138e9b902aba (patch) | |
tree | 9b4408457bbd03a3fda83b21bbf54dba56234d08 /lcorolib.c | |
parent | ef83457427cdb2d9f29d94177131db8e6e2eb606 (diff) | |
download | lua-1bd70a8e40a8600658c706d5f171138e9b902aba.tar.gz lua-1bd70a8e40a8600658c706d5f171138e9b902aba.tar.bz2 lua-1bd70a8e40a8600658c706d5f171138e9b902aba.zip |
new function 'lua_isyieldable' (and 'coroutine.isyieldable')
Diffstat (limited to 'lcorolib.c')
-rw-r--r-- | lcorolib.c | 22 |
1 files changed, 17 insertions, 5 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lcorolib.c,v 1.4 2012/04/27 18:59:04 roberto Exp roberto $ | 2 | ** $Id: lcorolib.c,v 1.5 2013/02/21 13:44:53 roberto Exp roberto $ |
3 | ** Coroutine Library | 3 | ** Coroutine Library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -17,6 +17,13 @@ | |||
17 | #include "lualib.h" | 17 | #include "lualib.h" |
18 | 18 | ||
19 | 19 | ||
20 | static lua_State *getco (lua_State *L) { | ||
21 | lua_State *co = lua_tothread(L, 1); | ||
22 | luaL_argcheck(L, co, 1, "coroutine expected"); | ||
23 | return co; | ||
24 | } | ||
25 | |||
26 | |||
20 | static int auxresume (lua_State *L, lua_State *co, int narg) { | 27 | static int auxresume (lua_State *L, lua_State *co, int narg) { |
21 | int status; | 28 | int status; |
22 | if (!lua_checkstack(co, narg)) { | 29 | if (!lua_checkstack(co, narg)) { |
@@ -47,9 +54,8 @@ static int auxresume (lua_State *L, lua_State *co, int narg) { | |||
47 | 54 | ||
48 | 55 | ||
49 | static int luaB_coresume (lua_State *L) { | 56 | static int luaB_coresume (lua_State *L) { |
50 | lua_State *co = lua_tothread(L, 1); | 57 | lua_State *co = getco(L); |
51 | int r; | 58 | int r; |
52 | luaL_argcheck(L, co, 1, "coroutine expected"); | ||
53 | r = auxresume(L, co, lua_gettop(L) - 1); | 59 | r = auxresume(L, co, lua_gettop(L) - 1); |
54 | if (r < 0) { | 60 | if (r < 0) { |
55 | lua_pushboolean(L, 0); | 61 | lua_pushboolean(L, 0); |
@@ -102,8 +108,7 @@ static int luaB_yield (lua_State *L) { | |||
102 | 108 | ||
103 | 109 | ||
104 | static int luaB_costatus (lua_State *L) { | 110 | static int luaB_costatus (lua_State *L) { |
105 | lua_State *co = lua_tothread(L, 1); | 111 | lua_State *co = getco(L); |
106 | luaL_argcheck(L, co, 1, "coroutine expected"); | ||
107 | if (L == co) lua_pushliteral(L, "running"); | 112 | if (L == co) lua_pushliteral(L, "running"); |
108 | else { | 113 | else { |
109 | switch (lua_status(co)) { | 114 | switch (lua_status(co)) { |
@@ -129,6 +134,12 @@ static int luaB_costatus (lua_State *L) { | |||
129 | } | 134 | } |
130 | 135 | ||
131 | 136 | ||
137 | static int luaB_yieldable (lua_State *L) { | ||
138 | lua_pushboolean(L, lua_isyieldable(L)); | ||
139 | return 1; | ||
140 | } | ||
141 | |||
142 | |||
132 | static int luaB_corunning (lua_State *L) { | 143 | static int luaB_corunning (lua_State *L) { |
133 | int ismain = lua_pushthread(L); | 144 | int ismain = lua_pushthread(L); |
134 | lua_pushboolean(L, ismain); | 145 | lua_pushboolean(L, ismain); |
@@ -143,6 +154,7 @@ static const luaL_Reg co_funcs[] = { | |||
143 | {"status", luaB_costatus}, | 154 | {"status", luaB_costatus}, |
144 | {"wrap", luaB_cowrap}, | 155 | {"wrap", luaB_cowrap}, |
145 | {"yield", luaB_yield}, | 156 | {"yield", luaB_yield}, |
157 | {"isyieldable", luaB_yieldable}, | ||
146 | {NULL, NULL} | 158 | {NULL, NULL} |
147 | }; | 159 | }; |
148 | 160 | ||