From 1bd70a8e40a8600658c706d5f171138e9b902aba Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 8 May 2014 10:52:20 -0300 Subject: [PATCH] new function 'lua_isyieldable' (and 'coroutine.isyieldable') --- lcorolib.c | 22 +++++++++++++++++----- ldo.c | 7 ++++++- lua.h | 4 +++- 3 files changed, 26 insertions(+), 7 deletions(-) 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} }; diff --git a/ldo.c b/ldo.c index 267c8237..ae58cda7 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.114 2014/02/26 15:27:56 roberto Exp roberto $ +** $Id: ldo.c,v 2.115 2014/03/21 13:52:33 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -571,6 +571,11 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { } +LUA_API int lua_isyieldable (lua_State *L) { + return (L->nny == 0); +} + + LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { CallInfo *ci = L->ci; luai_userstateyield(L, nresults); diff --git a/lua.h b/lua.h index bef5ff29..464d3717 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.303 2014/05/01 18:18:06 roberto Exp roberto $ +** $Id: lua.h,v 1.304 2014/05/01 18:21:32 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** 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, #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); LUA_API int (lua_status) (lua_State *L); +LUA_API int lua_isyieldable (lua_State *L); + /* ** garbage-collection function and options