1
0
mirror of https://github.com/lua/lua.git synced 2025-01-28 06:03:00 +08:00

new function 'lua_isyieldable' (and 'coroutine.isyieldable')

This commit is contained in:
Roberto Ierusalimschy 2014-05-08 10:52:20 -03:00
parent ef83457427
commit 1bd70a8e40
3 changed files with 26 additions and 7 deletions

View File

@ -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}
};

7
ldo.c
View File

@ -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);

4
lua.h
View File

@ -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