From ae5283dc368b7b2d5f0811f7b74860b46774bd8d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 12 Apr 2002 16:57:29 -0300 Subject: `co' library goes with basic library (and not with `tab') --- lbaselib.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) (limited to 'lbaselib.c') diff --git a/lbaselib.c b/lbaselib.c index b6d4ce8e..2aaafe43 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.65 2002/04/09 19:46:56 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.66 2002/04/09 20:19:06 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -421,6 +421,82 @@ static const luaL_reg base_funcs[] = { }; +/* +** {====================================================== +** Coroutine library +** ======================================================= +*/ + + +static int luaB_resume (lua_State *L) { + lua_State *co = (lua_State *)lua_getfrombox(L, lua_upvalueindex(1)); + lua_settop(L, 0); + if (lua_resume(L, co) != 0) + lua_error(L, "error running co-routine"); + return lua_gettop(L); +} + + + +static int gc_coroutine (lua_State *L) { + lua_State *co = (lua_State *)lua_getfrombox(L, 1); + lua_closethread(L, co); + return 0; +} + + +static int luaB_coroutine (lua_State *L) { + lua_State *NL; + int ref; + int i; + int n = lua_gettop(L); + luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, + "Lua function expected"); + NL = lua_newthread(L); + if (NL == NULL) lua_error(L, "unable to create new thread"); + /* move function and arguments from L to NL */ + for (i=0; i