mirror of
https://github.com/lua/lua.git
synced 2025-02-04 06:13:04 +08:00
new function 'lua_isyieldable' (and 'coroutine.isyieldable')
This commit is contained in:
parent
ef83457427
commit
1bd70a8e40
22
lcorolib.c
22
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
|
** Coroutine Library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -17,6 +17,13 @@
|
|||||||
#include "lualib.h"
|
#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) {
|
static int auxresume (lua_State *L, lua_State *co, int narg) {
|
||||||
int status;
|
int status;
|
||||||
if (!lua_checkstack(co, narg)) {
|
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) {
|
static int luaB_coresume (lua_State *L) {
|
||||||
lua_State *co = lua_tothread(L, 1);
|
lua_State *co = getco(L);
|
||||||
int r;
|
int r;
|
||||||
luaL_argcheck(L, co, 1, "coroutine expected");
|
|
||||||
r = auxresume(L, co, lua_gettop(L) - 1);
|
r = auxresume(L, co, lua_gettop(L) - 1);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
lua_pushboolean(L, 0);
|
lua_pushboolean(L, 0);
|
||||||
@ -102,8 +108,7 @@ static int luaB_yield (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int luaB_costatus (lua_State *L) {
|
static int luaB_costatus (lua_State *L) {
|
||||||
lua_State *co = lua_tothread(L, 1);
|
lua_State *co = getco(L);
|
||||||
luaL_argcheck(L, co, 1, "coroutine expected");
|
|
||||||
if (L == co) lua_pushliteral(L, "running");
|
if (L == co) lua_pushliteral(L, "running");
|
||||||
else {
|
else {
|
||||||
switch (lua_status(co)) {
|
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) {
|
static int luaB_corunning (lua_State *L) {
|
||||||
int ismain = lua_pushthread(L);
|
int ismain = lua_pushthread(L);
|
||||||
lua_pushboolean(L, ismain);
|
lua_pushboolean(L, ismain);
|
||||||
@ -143,6 +154,7 @@ static const luaL_Reg co_funcs[] = {
|
|||||||
{"status", luaB_costatus},
|
{"status", luaB_costatus},
|
||||||
{"wrap", luaB_cowrap},
|
{"wrap", luaB_cowrap},
|
||||||
{"yield", luaB_yield},
|
{"yield", luaB_yield},
|
||||||
|
{"isyieldable", luaB_yieldable},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
7
ldo.c
7
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
|
** Stack and Call structure of Lua
|
||||||
** See Copyright Notice in lua.h
|
** 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) {
|
LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) {
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
luai_userstateyield(L, nresults);
|
luai_userstateyield(L, nresults);
|
||||||
|
4
lua.h
4
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 - A Scripting Language
|
||||||
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
|
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
|
||||||
** See Copyright Notice at the end of this file
|
** 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)
|
#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_resume) (lua_State *L, lua_State *from, int narg);
|
||||||
LUA_API int (lua_status) (lua_State *L);
|
LUA_API int (lua_status) (lua_State *L);
|
||||||
|
LUA_API int lua_isyieldable (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** garbage-collection function and options
|
** garbage-collection function and options
|
||||||
|
Loading…
x
Reference in New Issue
Block a user