small improvements

This commit is contained in:
Roberto Ierusalimschy 2005-08-22 15:54:49 -03:00
parent 43ad0637ca
commit 6fcd334ca0
4 changed files with 39 additions and 45 deletions

52
ldo.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.c,v 2.28 2005/07/11 14:00:31 roberto Exp roberto $ ** $Id: ldo.c,v 2.29 2005/08/09 19:49:04 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -134,7 +134,7 @@ static void correctstack (lua_State *L, TValue *oldstack) {
ci->base = (ci->base - oldstack) + L->stack; ci->base = (ci->base - oldstack) + L->stack;
ci->func = (ci->func - oldstack) + L->stack; ci->func = (ci->func - oldstack) + L->stack;
} }
L->base = L->ci->base; L->base = (L->base - oldstack) + L->stack;
} }
@ -314,13 +314,14 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
L->base = ci->base = ci->func + 1; L->base = ci->base = ci->func + 1;
ci->top = L->top + LUA_MINSTACK; ci->top = L->top + LUA_MINSTACK;
lua_assert(ci->top <= L->stack_last); lua_assert(ci->top <= L->stack_last);
ci->nresults = nresults;
if (L->hookmask & LUA_MASKCALL) if (L->hookmask & LUA_MASKCALL)
luaD_callhook(L, LUA_HOOKCALL, -1); luaD_callhook(L, LUA_HOOKCALL, -1);
lua_unlock(L); lua_unlock(L);
n = (*curr_func(L)->c.f)(L); /* do the actual call */ n = (*curr_func(L)->c.f)(L); /* do the actual call */
lua_lock(L); lua_lock(L);
if (n >= 0) { /* no yielding? */ if (n >= 0) { /* no yielding? */
luaD_poscall(L, nresults, L->top - n); luaD_poscall(L, L->top - n);
return PCRC; return PCRC;
} }
else { else {
@ -342,22 +343,24 @@ static StkId callrethooks (lua_State *L, StkId firstResult) {
} }
void luaD_poscall (lua_State *L, int wanted, StkId firstResult) { int luaD_poscall (lua_State *L, StkId firstResult) {
StkId res; StkId res;
int wanted, i;
CallInfo *ci;
if (L->hookmask & LUA_MASKRET) if (L->hookmask & LUA_MASKRET)
firstResult = callrethooks(L, firstResult); firstResult = callrethooks(L, firstResult);
res = L->ci->func; /* res == final position of 1st result */ ci = L->ci--;
L->ci--; res = ci->func; /* res == final position of 1st result */
L->base = L->ci->base; /* restore base */ wanted = ci->nresults;
L->savedpc = L->ci->savedpc; /* restore savedpc */ L->base = (ci - 1)->base; /* restore base */
L->savedpc = (ci - 1)->savedpc; /* restore savedpc */
/* move results to correct place */ /* move results to correct place */
while (wanted != 0 && firstResult < L->top) { for (i = wanted; i != 0 && firstResult < L->top; i--)
setobjs2s(L, res++, firstResult++); setobjs2s(L, res++, firstResult++);
wanted--; while (i-- > 0)
}
while (wanted-- > 0)
setnilvalue(res++); setnilvalue(res++);
L->top = res; L->top = res;
return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */
} }
@ -374,41 +377,34 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
} }
if (luaD_precall(L, func, nResults) == PCRLUA) { /* is a Lua function? */ if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */
StkId firstResult = luaV_execute(L, 1); /* call it */ luaV_execute(L, 1); /* call it */
luaD_poscall(L, nResults, firstResult);
}
L->nCcalls--; L->nCcalls--;
luaC_checkGC(L); luaC_checkGC(L);
} }
static void resume (lua_State *L, void *ud) { static void resume (lua_State *L, void *ud) {
StkId firstResult;
StkId firstArg = cast(StkId, ud); StkId firstArg = cast(StkId, ud);
CallInfo *ci = L->ci; CallInfo *ci = L->ci;
if (L->status != LUA_YIELD) { if (L->status != LUA_YIELD) { /* start coroutine */
lua_assert(ci == L->base_ci && firstArg > L->base); lua_assert(ci == L->base_ci && firstArg > L->base);
luaD_precall(L, firstArg - 1, LUA_MULTRET); /* start coroutine */ if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA)
return;
} }
else { /* resuming from previous yield */ else { /* resuming from previous yield */
if (!f_isLua(ci)) { /* `common' yield? */ if (!f_isLua(ci)) { /* `common' yield? */
/* finish interrupted execution of `OP_CALL' */ /* finish interrupted execution of `OP_CALL' */
int nresults = ci->nresults;
lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL || lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL ||
GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL); GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL);
luaD_poscall(L, nresults, firstArg); /* complete it */ if (luaD_poscall(L, firstArg)) /* complete it... */
if (nresults >= 0) L->top = L->ci->top; L->top = L->ci->top; /* and correct top if not multiple results */
} }
else { /* yielded inside a hook: just continue its execution */ else /* yielded inside a hook: just continue its execution */
L->base = L->ci->base; L->base = L->ci->base;
}
} }
L->status = 0; L->status = 0;
firstResult = luaV_execute(L, cast(int, L->ci - L->base_ci)); luaV_execute(L, cast(int, L->ci - L->base_ci));
if (firstResult != NULL) { /* return? */
luaD_poscall(L, LUA_MULTRET, firstResult); /* finalize this coroutine */
}
} }

4
ldo.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.h,v 2.3 2004/09/08 14:23:09 roberto Exp roberto $ ** $Id: ldo.h,v 2.4 2005/04/25 19:24:10 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -53,7 +53,7 @@ LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults);
LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
ptrdiff_t oldtop, ptrdiff_t ef); ptrdiff_t oldtop, ptrdiff_t ef);
LUAI_FUNC void luaD_poscall (lua_State *L, int wanted, StkId firstResult); LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult);
LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize); LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize);
LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
LUAI_FUNC void luaD_growstack (lua_State *L, int n); LUAI_FUNC void luaD_growstack (lua_State *L, int n);

24
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 2.50 2005/08/09 19:49:04 roberto Exp roberto $ ** $Id: lvm.c,v 2.51 2005/08/10 20:20:13 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -358,7 +358,7 @@ static void Arith (lua_State *L, StkId ra, const TValue *rb,
#define Protect(x) { L->savedpc = pc; {x;}; base = L->base; } #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
StkId luaV_execute (lua_State *L, int nexeccalls) { void luaV_execute (lua_State *L, int nexeccalls) {
LClosure *cl; LClosure *cl;
StkId base; StkId base;
TValue *k; TValue *k;
@ -377,7 +377,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
traceexec(L, pc); traceexec(L, pc);
if (L->status == LUA_YIELD) { /* did hook yield? */ if (L->status == LUA_YIELD) { /* did hook yield? */
L->savedpc = pc - 1; L->savedpc = pc - 1;
return NULL; return;
} }
base = L->base; base = L->base;
} }
@ -617,7 +617,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
continue; continue;
} }
default: { default: {
return NULL; return; /* yield */
} }
} }
} }
@ -644,13 +644,12 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
L->ci--; /* remove new frame */ L->ci--; /* remove new frame */
goto reentry; goto reentry;
} }
case PCRC: { case PCRC: { /* it was a C function (`precall' called it) */
/* it was a C function (`precall' called it) */
base = L->base; base = L->base;
continue; continue;
} }
default: { default: {
return NULL; return; /* yield */
} }
} }
} }
@ -659,14 +658,13 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
if (b != 0) L->top = ra+b-1; if (b != 0) L->top = ra+b-1;
if (L->openupval) luaF_close(L, base); if (L->openupval) luaF_close(L, base);
L->savedpc = pc; L->savedpc = pc;
b = luaD_poscall(L, ra);
if (--nexeccalls == 0) /* was previous function running `here'? */ if (--nexeccalls == 0) /* was previous function running `here'? */
return ra; /* no: return */ return; /* no: return */
else { /* yes: continue its execution */ else { /* yes: continue its execution */
int nresults = L->ci->nresults; if (b) L->top = L->ci->top;
lua_assert(isLua(L->ci - 1)); lua_assert(isLua(L->ci));
lua_assert(GET_OPCODE(*((L->ci - 1)->savedpc - 1)) == OP_CALL); lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
luaD_poscall(L, nresults, ra);
if (nresults >= 0) L->top = L->ci->top;
goto reentry; goto reentry;
} }
} }

4
lvm.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.h,v 2.3 2005/04/04 18:12:51 roberto Exp roberto $ ** $Id: lvm.h,v 2.4 2005/04/25 19:24:10 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -30,7 +30,7 @@ LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
StkId val); StkId val);
LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key,
StkId val); StkId val);
LUAI_FUNC StkId luaV_execute (lua_State *L, int nexeccalls); LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls);
LUAI_FUNC void luaV_concat (lua_State *L, int total, int last); LUAI_FUNC void luaV_concat (lua_State *L, int total, int last);
#endif #endif