From 1bd70a8e40a8600658c706d5f171138e9b902aba Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 8 May 2014 10:52:20 -0300 Subject: new function 'lua_isyieldable' (and 'coroutine.isyieldable') --- lcorolib.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'lcorolib.c') diff --git a/lcorolib.c b/lcorolib.c index cd7e4e1e..4bcb7e9a 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -1,5 +1,5 @@ /* -** $Id: lcorolib.c,v 1.4 2012/04/27 18:59:04 roberto Exp roberto $ +** $Id: lcorolib.c,v 1.5 2013/02/21 13:44:53 roberto Exp roberto $ ** Coroutine Library ** See Copyright Notice in lua.h */ @@ -17,6 +17,13 @@ #include "lualib.h" +static lua_State *getco (lua_State *L) { + lua_State *co = lua_tothread(L, 1); + luaL_argcheck(L, co, 1, "coroutine expected"); + return co; +} + + static int auxresume (lua_State *L, lua_State *co, int narg) { int status; if (!lua_checkstack(co, narg)) { @@ -47,9 +54,8 @@ static int auxresume (lua_State *L, lua_State *co, int narg) { static int luaB_coresume (lua_State *L) { - lua_State *co = lua_tothread(L, 1); + lua_State *co = getco(L); int r; - luaL_argcheck(L, co, 1, "coroutine expected"); r = auxresume(L, co, lua_gettop(L) - 1); if (r < 0) { lua_pushboolean(L, 0); @@ -102,8 +108,7 @@ static int luaB_yield (lua_State *L) { static int luaB_costatus (lua_State *L) { - lua_State *co = lua_tothread(L, 1); - luaL_argcheck(L, co, 1, "coroutine expected"); + lua_State *co = getco(L); if (L == co) lua_pushliteral(L, "running"); else { switch (lua_status(co)) { @@ -129,6 +134,12 @@ static int luaB_costatus (lua_State *L) { } +static int luaB_yieldable (lua_State *L) { + lua_pushboolean(L, lua_isyieldable(L)); + return 1; +} + + static int luaB_corunning (lua_State *L) { int ismain = lua_pushthread(L); lua_pushboolean(L, ismain); @@ -143,6 +154,7 @@ static const luaL_Reg co_funcs[] = { {"status", luaB_costatus}, {"wrap", luaB_cowrap}, {"yield", luaB_yield}, + {"isyieldable", luaB_yieldable}, {NULL, NULL} }; -- cgit v1.2.3-55-g6feb