`yield' passes its arguments to `resume'

This commit is contained in:
Roberto Ierusalimschy 2002-01-11 18:27:41 -02:00
parent 5d14ce612b
commit 6272c843de
2 changed files with 47 additions and 34 deletions

View File

@ -415,6 +415,14 @@ static int luaB_tostring (lua_State *L) {
static int luaB_resume (lua_State *L) { static int luaB_resume (lua_State *L) {
lua_State *co = (lua_State *)lua_touserdata(L, lua_upvalueindex(1)); lua_State *co = (lua_State *)lua_touserdata(L, lua_upvalueindex(1));
lua_resume(L, co); 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; return 0;
} }
@ -422,22 +430,30 @@ static int luaB_resume (lua_State *L) {
static int luaB_coroutine (lua_State *L) { static int luaB_coroutine (lua_State *L) {
lua_State *NL; lua_State *NL;
int ref; 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); NL = lua_newthread(L, 0);
if (NL == NULL) lua_error(L, "unable to create new thread"); if (NL == NULL) lua_error(L, "unable to create new thread");
/* move function from L to NL */ /* move function and arguments from L to NL */
ref = lua_ref(L, 1); for (i=0; i<n; i++) {
lua_getref(NL, ref); ref = lua_ref(L, 1);
lua_unref(L, ref); lua_getref(NL, ref);
lua_cobegin(NL, 0); lua_insert(NL, 1);
lua_unref(L, ref);
}
lua_cobegin(NL, n-1);
lua_newuserdatabox(L, NL); lua_newuserdatabox(L, NL);
lua_getstr(L, LUA_REGISTRYINDEX, "Coroutine");
lua_seteventtable(L, -2);
lua_pushcclosure(L, luaB_resume, 1); lua_pushcclosure(L, luaB_resume, 1);
return 1; return 1;
} }
static int luaB_yield (lua_State *L) { static int luaB_yield (lua_State *L) {
return lua_yield(L, 0); return lua_yield(L, lua_gettop(L));
} }
@ -683,6 +699,11 @@ LUALIB_API int lua_baselibopen (lua_State *L) {
lua_newtable(L); lua_newtable(L);
lua_pushcclosure(L, luaB_require, 1); lua_pushcclosure(L, luaB_require, 1);
lua_setglobal(L, "require"); lua_setglobal(L, "require");
/* create metatable for coroutines */
lua_newtable(L);
lua_pushcfunction(L, gc_coroutine);
lua_setstr(L, -2, "gc");
lua_setstr(L, LUA_REGISTRYINDEX, "Coroutine");
return 0; return 0;
} }

46
ldo.c
View File

@ -29,9 +29,6 @@
#include "lzio.h" #include "lzio.h"
/* space to handle stack overflow errors */
#define EXTRA_STACK (2*LUA_MINSTACK)
static void restore_stack_limit (lua_State *L) { static void restore_stack_limit (lua_State *L) {
StkId limit = L->stack+(L->stacksize-EXTRA_STACK)-1; StkId limit = L->stack+(L->stacksize-EXTRA_STACK)-1;
@ -40,21 +37,6 @@ static void restore_stack_limit (lua_State *L) {
} }
void luaD_init (lua_State *L, int stacksize) {
stacksize += EXTRA_STACK;
L->stack = luaM_newvector(L, stacksize, TObject);
L->stacksize = stacksize;
L->top = L->stack + RESERVED_STACK_PREFIX;
restore_stack_limit(L);
luaM_reallocvector(L, L->base_ci, 0, 20, CallInfo);
L->ci = L->base_ci;
L->ci->base = L->top;
L->ci->savedpc = NULL;
L->size_ci = 20;
L->end_ci = L->base_ci + L->size_ci;
}
void luaD_stackerror (lua_State *L) { void luaD_stackerror (lua_State *L) {
if (L->stack_last == L->stack+L->stacksize-1) { if (L->stack_last == L->stack+L->stacksize-1) {
/* overflow while handling overflow */ /* overflow while handling overflow */
@ -231,15 +213,21 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
LUA_API void lua_cobegin (lua_State *L, int nargs) { LUA_API void lua_cobegin (lua_State *L, int nargs) {
StkId func;
lua_lock(L); lua_lock(L);
func = L->top - (nargs+1); /* coroutine main function */ luaD_precall(L, L->top - (nargs+1));
if (luaD_precall(L, func) != NULL)
luaD_error(L, "coroutine started with a C function");
lua_unlock(L); lua_unlock(L);
} }
static void resume_results (lua_State *L, lua_State *from, int numresults) {
while (numresults) {
setobj(L->top, from->top - numresults);
numresults--;
incr_top;
}
}
LUA_API void lua_resume (lua_State *L, lua_State *co) { LUA_API void lua_resume (lua_State *L, lua_State *co) {
StkId firstResult; StkId firstResult;
lua_lock(L); lua_lock(L);
@ -248,10 +236,13 @@ LUA_API void lua_resume (lua_State *L, lua_State *co) {
lua_assert(co->errorJmp == NULL); lua_assert(co->errorJmp == NULL);
co->errorJmp = L->errorJmp; co->errorJmp = L->errorJmp;
firstResult = luaV_execute(co); firstResult = luaV_execute(co);
if (firstResult != NULL) /* `return'? */ if (firstResult != NULL) { /* `return'? */
luaD_poscall(co, LUA_MULTRET, firstResult); /* ends this coroutine */ resume_results(L, co, co->top - firstResult);
luaD_poscall(co, 0, firstResult); /* ends this coroutine */
}
else { /* `yield' */ else { /* `yield' */
int nresults = GETARG_C(*((co->ci-1)->savedpc - 1)) - 1; int nresults = GETARG_C(*((co->ci-1)->savedpc - 1)) - 1;
resume_results(L, co, co->ci->yield_results);
luaD_poscall(co, nresults, co->top); /* complete it */ luaD_poscall(co, nresults, co->top); /* complete it */
if (nresults >= 0) co->top = co->ci->top; if (nresults >= 0) co->top = co->ci->top;
} }
@ -264,10 +255,11 @@ LUA_API int lua_yield (lua_State *L, int nresults) {
CallInfo *ci; CallInfo *ci;
int ibase; int ibase;
lua_lock(L); lua_lock(L);
ci = L->ci - 1; /* call info of calling function */ ci = L->ci;
if (ci->pc == NULL) if (ci_func(ci-1)->c.isC)
luaD_error(L, "cannot `yield' a C function"); luaD_error(L, "cannot `yield' a C function");
ibase = L->top - ci->base; ci->yield_results = nresults; /* very dirty trick! */
ibase = L->top - (ci-1)->base;
lua_unlock(L); lua_unlock(L);
return ibase; return ibase;
} }