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 | |
| parent | ef83457427cdb2d9f29d94177131db8e6e2eb606 (diff) | |
| download | lua-1bd70a8e40a8600658c706d5f171138e9b902aba.tar.gz lua-1bd70a8e40a8600658c706d5f171138e9b902aba.tar.bz2 lua-1bd70a8e40a8600658c706d5f171138e9b902aba.zip | |
new function 'lua_isyieldable' (and 'coroutine.isyieldable')
| -rw-r--r-- | lcorolib.c | 22 | ||||
| -rw-r--r-- | ldo.c | 7 | ||||
| -rw-r--r-- | lua.h | 4 |
3 files changed, 26 insertions, 7 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 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldo.c,v 2.114 2014/02/26 15:27:56 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 2.115 2014/03/21 13:52:33 roberto Exp roberto $ |
| 3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -571,6 +571,11 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { | |||
| 571 | } | 571 | } |
| 572 | 572 | ||
| 573 | 573 | ||
| 574 | LUA_API int lua_isyieldable (lua_State *L) { | ||
| 575 | return (L->nny == 0); | ||
| 576 | } | ||
| 577 | |||
| 578 | |||
| 574 | LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { | 579 | LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { |
| 575 | CallInfo *ci = L->ci; | 580 | CallInfo *ci = L->ci; |
| 576 | luai_userstateyield(L, nresults); | 581 | luai_userstateyield(L, nresults); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.h,v 1.303 2014/05/01 18:18:06 roberto Exp roberto $ | 2 | ** $Id: lua.h,v 1.304 2014/05/01 18:21:32 roberto Exp roberto $ |
| 3 | ** Lua - A Scripting Language | 3 | ** Lua - A Scripting Language |
| 4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) | 4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) |
| 5 | ** See Copyright Notice at the end of this file | 5 | ** See Copyright Notice at the end of this file |
| @@ -282,6 +282,8 @@ LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx, | |||
| 282 | #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) | 282 | #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) |
| 283 | LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); | 283 | LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); |
| 284 | LUA_API int (lua_status) (lua_State *L); | 284 | LUA_API int (lua_status) (lua_State *L); |
| 285 | LUA_API int lua_isyieldable (lua_State *L); | ||
| 286 | |||
| 285 | 287 | ||
| 286 | /* | 288 | /* |
| 287 | ** garbage-collection function and options | 289 | ** garbage-collection function and options |
