From f083812c020186d0d919833100c1a0b6eda8c2c0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 9 Jan 2002 20:02:47 -0200 Subject: first implementation of coroutines --- lbaselib.c | 81 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 46 insertions(+), 35 deletions(-) (limited to 'lbaselib.c') diff --git a/lbaselib.c b/lbaselib.c index cb914f18..6cd97d95 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -148,30 +148,6 @@ static int luaB_eventtable (lua_State *L) { } -static int luaB_weakmode (lua_State *L) { - const char *mode = luaL_check_string(L, 2); - luaL_check_type(L, 1, LUA_TTABLE); - if (*mode == '?') { - char buff[3]; - char *s = buff; - int imode = lua_getweakmode(L, 1); - if (imode & LUA_WEAK_KEY) *s++ = 'k'; - if (imode & LUA_WEAK_VALUE) *s++ = 'v'; - *s = '\0'; - lua_pushstring(L, buff); - return 1; - } - else { - int imode = 0; - if (strchr(mode, 'k')) imode |= LUA_WEAK_KEY; - if (strchr(mode, 'v')) imode |= LUA_WEAK_VALUE; - lua_pushvalue(L, 1); /* push table */ - lua_setweakmode(L, imode); - return 1; /* return the table */ - } -} - - static int luaB_globals (lua_State *L) { lua_getglobals(L); /* value to be returned */ if (!lua_isnone(L, 1)) { @@ -287,6 +263,14 @@ static int luaB_loadfile (lua_State *L) { } +static int luaB_assert (lua_State *L) { + luaL_check_any(L, 1); + if (!lua_istrue(L, 1)) + luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, "")); + lua_settop(L, 1); + return 1; +} + #define LUA_PATH "LUA_PATH" @@ -428,6 +412,40 @@ static int luaB_tostring (lua_State *L) { } +static int luaB_resume (lua_State *L) { + lua_State *co = (lua_State *)lua_touserdata(L, lua_upvalueindex(1)); + lua_resume(L, co); + return 0; +} + + +static int luaB_coroutine (lua_State *L) { + lua_State *NL; + int ref; + luaL_check_type(L, 1, LUA_TFUNCTION); + NL = lua_newthread(L, 0); + if (NL == NULL) lua_error(L, "unable to create new thread"); + /* move function from L to NL */ + ref = lua_ref(L, 1); + lua_getref(NL, ref); + lua_unref(L, ref); + lua_cobegin(NL, 0); + lua_newuserdatabox(L, NL); + lua_pushcclosure(L, luaB_resume, 1); + return 1; +} + + +static int luaB_yield (lua_State *L) { + return lua_yield(L, 0); +} + + +/* +** {====================================================== +** Auxiliar table-related functions +*/ + static int luaB_foreachi (lua_State *L) { int n, i; luaL_check_type(L, 1, LUA_TTABLE); @@ -464,15 +482,6 @@ static int luaB_foreach (lua_State *L) { } -static int luaB_assert (lua_State *L) { - luaL_check_any(L, 1); - if (!lua_istrue(L, 1)) - luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, "")); - lua_settop(L, 1); - return 1; -} - - static int luaB_getn (lua_State *L) { luaL_check_type(L, 1, LUA_TTABLE); lua_pushnumber(L, lua_getn(L, 1)); @@ -521,7 +530,6 @@ static int luaB_tremove (lua_State *L) { - /* ** {====================================================== ** Quicksort @@ -627,6 +635,8 @@ static int luaB_sort (lua_State *L) { /* }====================================================== */ +/* }====================================================== */ + static const luaL_reg base_funcs[] = { @@ -634,6 +644,7 @@ static const luaL_reg base_funcs[] = { {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE}, {"call", luaB_call}, {"collectgarbage", luaB_collectgarbage}, + {"coroutine", luaB_coroutine}, {"dofile", luaB_dofile}, {"dostring", luaB_dostring}, {"error", luaB_error}, @@ -659,7 +670,7 @@ static const luaL_reg base_funcs[] = { {"tinsert", luaB_tinsert}, {"tremove", luaB_tremove}, {"unpack", luaB_unpack}, - {"weakmode", luaB_weakmode} + {"yield", luaB_yield} }; -- cgit v1.2.3-55-g6feb