2010-06-11 01:30:26 +04:00
|
|
|
/*
|
2014-11-02 22:19:04 +03:00
|
|
|
** $Id: lcorolib.c,v 1.8 2014/10/25 11:50:46 roberto Exp roberto $
|
2010-06-11 01:30:26 +04:00
|
|
|
** Coroutine Library
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2014-11-02 22:19:04 +03:00
|
|
|
#define lcorolib_c
|
|
|
|
#define LUA_LIB
|
2010-06-11 01:30:26 +04:00
|
|
|
|
2014-11-02 22:19:04 +03:00
|
|
|
#include "lprefix.h"
|
2010-06-11 01:30:26 +04:00
|
|
|
|
|
|
|
|
2014-11-02 22:19:04 +03:00
|
|
|
#include <stdlib.h>
|
2010-06-11 01:30:26 +04:00
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
|
|
|
|
|
2014-05-08 17:52:20 +04:00
|
|
|
static lua_State *getco (lua_State *L) {
|
|
|
|
lua_State *co = lua_tothread(L, 1);
|
2014-09-01 22:00:04 +04:00
|
|
|
luaL_argcheck(L, co, 1, "thread expected");
|
2014-05-08 17:52:20 +04:00
|
|
|
return co;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-11 01:30:26 +04:00
|
|
|
static int auxresume (lua_State *L, lua_State *co, int narg) {
|
|
|
|
int status;
|
|
|
|
if (!lua_checkstack(co, narg)) {
|
|
|
|
lua_pushliteral(L, "too many arguments to resume");
|
|
|
|
return -1; /* error flag */
|
|
|
|
}
|
|
|
|
if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) {
|
|
|
|
lua_pushliteral(L, "cannot resume dead coroutine");
|
|
|
|
return -1; /* error flag */
|
|
|
|
}
|
|
|
|
lua_xmove(L, co, narg);
|
2011-08-23 21:24:34 +04:00
|
|
|
status = lua_resume(co, L, narg);
|
2010-06-11 01:30:26 +04:00
|
|
|
if (status == LUA_OK || status == LUA_YIELD) {
|
|
|
|
int nres = lua_gettop(co);
|
|
|
|
if (!lua_checkstack(L, nres + 1)) {
|
|
|
|
lua_pop(co, nres); /* remove results anyway */
|
|
|
|
lua_pushliteral(L, "too many results to resume");
|
|
|
|
return -1; /* error flag */
|
|
|
|
}
|
|
|
|
lua_xmove(co, L, nres); /* move yielded values */
|
|
|
|
return nres;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lua_xmove(co, L, 1); /* move error message */
|
|
|
|
return -1; /* error flag */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int luaB_coresume (lua_State *L) {
|
2014-05-08 17:52:20 +04:00
|
|
|
lua_State *co = getco(L);
|
2010-06-11 01:30:26 +04:00
|
|
|
int r;
|
|
|
|
r = auxresume(L, co, lua_gettop(L) - 1);
|
|
|
|
if (r < 0) {
|
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
lua_insert(L, -2);
|
|
|
|
return 2; /* return false + error message */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
lua_insert(L, -(r + 1));
|
2014-10-25 15:50:46 +04:00
|
|
|
return r + 1; /* return true + 'resume' returns */
|
2010-06-11 01:30:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int luaB_auxwrap (lua_State *L) {
|
|
|
|
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
|
|
|
|
int r = auxresume(L, co, lua_gettop(L));
|
|
|
|
if (r < 0) {
|
|
|
|
if (lua_isstring(L, -1)) { /* error object is a string? */
|
|
|
|
luaL_where(L, 1); /* add extra info */
|
|
|
|
lua_insert(L, -2);
|
|
|
|
lua_concat(L, 2);
|
|
|
|
}
|
2013-02-21 17:44:53 +04:00
|
|
|
return lua_error(L); /* propagate error */
|
2010-06-11 01:30:26 +04:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int luaB_cocreate (lua_State *L) {
|
2012-04-27 22:59:04 +04:00
|
|
|
lua_State *NL;
|
2010-06-11 01:30:26 +04:00
|
|
|
luaL_checktype(L, 1, LUA_TFUNCTION);
|
2012-04-27 22:59:04 +04:00
|
|
|
NL = lua_newthread(L);
|
2010-06-11 01:30:26 +04:00
|
|
|
lua_pushvalue(L, 1); /* move function to top */
|
|
|
|
lua_xmove(L, NL, 1); /* move function from L to NL */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int luaB_cowrap (lua_State *L) {
|
|
|
|
luaB_cocreate(L);
|
|
|
|
lua_pushcclosure(L, luaB_auxwrap, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int luaB_yield (lua_State *L) {
|
|
|
|
return lua_yield(L, lua_gettop(L));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int luaB_costatus (lua_State *L) {
|
2014-05-08 17:52:20 +04:00
|
|
|
lua_State *co = getco(L);
|
2010-06-11 01:30:26 +04:00
|
|
|
if (L == co) lua_pushliteral(L, "running");
|
|
|
|
else {
|
|
|
|
switch (lua_status(co)) {
|
|
|
|
case LUA_YIELD:
|
|
|
|
lua_pushliteral(L, "suspended");
|
|
|
|
break;
|
|
|
|
case LUA_OK: {
|
|
|
|
lua_Debug ar;
|
|
|
|
if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
|
|
|
|
lua_pushliteral(L, "normal"); /* it is running */
|
|
|
|
else if (lua_gettop(co) == 0)
|
|
|
|
lua_pushliteral(L, "dead");
|
|
|
|
else
|
|
|
|
lua_pushliteral(L, "suspended"); /* initial state */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: /* some error occurred */
|
|
|
|
lua_pushliteral(L, "dead");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-08 17:52:20 +04:00
|
|
|
static int luaB_yieldable (lua_State *L) {
|
|
|
|
lua_pushboolean(L, lua_isyieldable(L));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-11 01:30:26 +04:00
|
|
|
static int luaB_corunning (lua_State *L) {
|
|
|
|
int ismain = lua_pushthread(L);
|
|
|
|
lua_pushboolean(L, ismain);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const luaL_Reg co_funcs[] = {
|
|
|
|
{"create", luaB_cocreate},
|
|
|
|
{"resume", luaB_coresume},
|
|
|
|
{"running", luaB_corunning},
|
|
|
|
{"status", luaB_costatus},
|
|
|
|
{"wrap", luaB_cowrap},
|
|
|
|
{"yield", luaB_yield},
|
2014-05-08 17:52:20 +04:00
|
|
|
{"isyieldable", luaB_yieldable},
|
2010-06-11 01:30:26 +04:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LUAMOD_API int luaopen_coroutine (lua_State *L) {
|
2010-07-02 15:38:13 +04:00
|
|
|
luaL_newlib(L, co_funcs);
|
2010-06-11 01:30:26 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|