diff options
Diffstat (limited to '')
-rw-r--r-- | lcorolib.c | 16 |
1 files changed, 14 insertions, 2 deletions
@@ -154,8 +154,13 @@ static int luaB_costatus (lua_State *L) { | |||
154 | } | 154 | } |
155 | 155 | ||
156 | 156 | ||
157 | static lua_State *getoptco (lua_State *L) { | ||
158 | return (lua_isnone(L, 1) ? L : getco(L)); | ||
159 | } | ||
160 | |||
161 | |||
157 | static int luaB_yieldable (lua_State *L) { | 162 | static int luaB_yieldable (lua_State *L) { |
158 | lua_State *co = lua_isnone(L, 1) ? L : getco(L); | 163 | lua_State *co = getoptco(L); |
159 | lua_pushboolean(L, lua_isyieldable(co)); | 164 | lua_pushboolean(L, lua_isyieldable(co)); |
160 | return 1; | 165 | return 1; |
161 | } | 166 | } |
@@ -169,7 +174,7 @@ static int luaB_corunning (lua_State *L) { | |||
169 | 174 | ||
170 | 175 | ||
171 | static int luaB_close (lua_State *L) { | 176 | static int luaB_close (lua_State *L) { |
172 | lua_State *co = getco(L); | 177 | lua_State *co = getoptco(L); |
173 | int status = auxstatus(L, co); | 178 | int status = auxstatus(L, co); |
174 | switch (status) { | 179 | switch (status) { |
175 | case COS_DEAD: case COS_YIELD: { | 180 | case COS_DEAD: case COS_YIELD: { |
@@ -184,6 +189,13 @@ static int luaB_close (lua_State *L) { | |||
184 | return 2; | 189 | return 2; |
185 | } | 190 | } |
186 | } | 191 | } |
192 | case COS_RUN: /* running coroutine? */ | ||
193 | lua_geti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); /* get main */ | ||
194 | if (lua_tothread(L, -1) == co) | ||
195 | return luaL_error(L, "cannot close main thread"); | ||
196 | lua_closethread(co, L); /* close itself */ | ||
197 | lua_assert(0); /* previous call does not return */ | ||
198 | return 0; | ||
187 | default: /* normal or running coroutine */ | 199 | default: /* normal or running coroutine */ |
188 | return luaL_error(L, "cannot close a %s coroutine", statname[status]); | 200 | return luaL_error(L, "cannot close a %s coroutine", statname[status]); |
189 | } | 201 | } |