diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-06-12 11:15:09 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-06-12 11:15:09 -0300 |
commit | fd897027f19288ce2cb0249cb8c1818e2f3f1c4c (patch) | |
tree | 7fd131ca204c4100a24157405eda4239d2155a46 /lcorolib.c | |
parent | d05fe48bfdd89956c0ebd115dca0fb115aa28dd6 (diff) | |
download | lua-fd897027f19288ce2cb0249cb8c1818e2f3f1c4c.tar.gz lua-fd897027f19288ce2cb0249cb8c1818e2f3f1c4c.tar.bz2 lua-fd897027f19288ce2cb0249cb8c1818e2f3f1c4c.zip |
A coroutine can close itself
A call to close itself will close all its to-be-closed variables and
return to the resume that (re)started the coroutine.
Diffstat (limited to 'lcorolib.c')
-rw-r--r-- | lcorolib.c | 13 |
1 files changed, 11 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,10 @@ 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_closethread(co, L); /* close itself */ | ||
194 | lua_assert(0); /* previous call does not return */ | ||
195 | return 0; | ||
187 | default: /* normal or running coroutine */ | 196 | default: /* normal or running coroutine */ |
188 | return luaL_error(L, "cannot close a %s coroutine", statname[status]); | 197 | return luaL_error(L, "cannot close a %s coroutine", statname[status]); |
189 | } | 198 | } |