From 6272c843dee7544bf319afbac85e8064fa1f3a4b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 11 Jan 2002 18:27:41 -0200 Subject: `yield' passes its arguments to `resume' --- lbaselib.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'lbaselib.c') diff --git a/lbaselib.c b/lbaselib.c index 6cd97d95..b06cc2b3 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -415,6 +415,14 @@ 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 lua_gettop(L); +} + + + +static int gc_coroutine (lua_State *L) { + lua_State *co = (lua_State *)lua_touserdata(L, 1); + lua_closethread(L, co); return 0; } @@ -422,22 +430,30 @@ static int luaB_resume (lua_State *L) { static int luaB_coroutine (lua_State *L) { lua_State *NL; int ref; - luaL_check_type(L, 1, LUA_TFUNCTION); + 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, 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); + /* move function and arguments from L to NL */ + for (i=0; i