From 44b19075c0fa4310d312147ee1d74dc11ff36eba Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 9 Aug 2005 16:49:04 -0300 Subject: [PATCH] small changes (to easy integration with Coco) --- ldo.c | 31 ++++++++++++++++--------------- lvm.c | 16 +++++----------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/ldo.c b/ldo.c index 87c9a1f7..ecce53d3 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.27 2005/06/13 21:17:59 roberto Exp roberto $ +** $Id: ldo.c,v 2.28 2005/07/11 14:00:31 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -298,6 +298,11 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { for (st = L->top; st < ci->top; st++) setnilvalue(st); L->top = ci->top; + if (L->hookmask & LUA_MASKCALL) { + L->savedpc++; /* hooks assume 'pc' is already incremented */ + luaD_callhook(L, LUA_HOOKCALL, -1); + L->savedpc--; /* correct 'pc' */ + } return PCRLUA; } else { /* if is a C function, call it */ @@ -380,11 +385,11 @@ void luaD_call (lua_State *L, StkId func, int nResults) { static void resume (lua_State *L, void *ud) { StkId firstResult; - int nargs = *cast(int *, ud); + StkId firstArg = cast(StkId, ud); CallInfo *ci = L->ci; if (L->status != LUA_YIELD) { - lua_assert(ci == L->base_ci && nargs < L->top - L->base); - luaD_precall(L, L->top - (nargs + 1), LUA_MULTRET); /* start coroutine */ + lua_assert(ci == L->base_ci && firstArg > L->base); + luaD_precall(L, firstArg - 1, LUA_MULTRET); /* start coroutine */ } else { /* resuming from previous yield */ if (!f_isLua(ci)) { /* `common' yield? */ @@ -392,9 +397,12 @@ static void resume (lua_State *L, void *ud) { int nresults = ci->nresults; lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL || GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL); - luaD_poscall(L, nresults, L->top - nargs); /* complete it */ + luaD_poscall(L, nresults, firstArg); /* complete it */ if (nresults >= 0) L->top = L->ci->top; - } /* else yielded inside a hook: just continue its execution */ + } + else { /* yielded inside a hook: just continue its execution */ + L->base = L->ci->base; + } } L->status = 0; firstResult = luaV_execute(L, cast(int, L->ci - L->base_ci)); @@ -423,7 +431,7 @@ LUA_API int lua_resume (lua_State *L, int nargs) { else if (L->ci != L->base_ci) return resume_error(L, "cannot resume non-suspended coroutine"); } - status = luaD_rawrunprotected(L, resume, &nargs); + status = luaD_rawrunprotected(L, resume, L->top - nargs); if (status != 0) { /* error? */ L->status = cast(lu_byte, status); /* mark thread as `dead' */ seterrorobj(L, status, L->top); @@ -441,14 +449,7 @@ LUA_API int lua_yield (lua_State *L, int nresults) { ci = L->ci; if (L->nCcalls > 0) luaG_runerror(L, "attempt to yield across metamethod/C-call boundary"); - if (!f_isLua(ci)) { /* usual yield */ - if (L->top - nresults > L->base) { /* is there garbage in the stack? */ - int i; - for (i=0; ibase + i, L->top - nresults + i); - L->top = L->base + nresults; - } - } /* else it's an yield inside a hook: nothing to do */ + L->base = L->top - nresults; /* protect stack slots below */ L->status = LUA_YIELD; lua_unlock(L); return -1; diff --git a/lvm.c b/lvm.c index 037d696e..c177e9fc 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.48 2005/07/05 14:31:20 roberto Exp roberto $ +** $Id: lvm.c,v 2.49 2005/08/09 17:42:02 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -362,13 +362,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { StkId base; TValue *k; const Instruction *pc; - callentry: /* entry point when calling new functions */ - if (L->hookmask & LUA_MASKCALL) { - L->savedpc++; /* hooks assume 'pc' is already incremented */ - luaD_callhook(L, LUA_HOOKCALL, -1); - L->savedpc--; /* correct 'pc' */ - } - retentry: /* entry point when returning to old functions */ + reentry: /* entry point */ pc = L->savedpc; cl = &clvalue(L->ci->func)->l; base = L->base; @@ -618,7 +612,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { switch (luaD_precall(L, ra, nresults)) { case PCRLUA: { nexeccalls++; - goto callentry; /* restart luaV_execute over new Lua function */ + goto reentry; /* restart luaV_execute over new Lua function */ } case PCRC: { /* it was a C function (`precall' called it); adjust results */ @@ -652,7 +646,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { ci->savedpc = L->savedpc; ci->tailcalls++; /* one more call lost */ L->ci--; /* remove new frame */ - goto callentry; + goto reentry; } case PCRC: { /* it was a C function (`precall' called it) */ @@ -677,7 +671,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { lua_assert(GET_OPCODE(*((L->ci - 1)->savedpc - 1)) == OP_CALL); luaD_poscall(L, nresults, ra); if (nresults >= 0) L->top = L->ci->top; - goto retentry; + goto reentry; } } case OP_FORLOOP: {