luaD_call is more uniform

This commit is contained in:
Roberto Ierusalimschy 2000-10-06 09:45:25 -03:00
parent 046a3d6173
commit ad3816d0d1
4 changed files with 42 additions and 46 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.c,v 1.44 2000/10/05 12:14:08 roberto Exp roberto $ ** $Id: ldebug.c,v 1.45 2000/10/05 13:00:17 roberto Exp roberto $
** Debug Interface ** Debug Interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -92,8 +92,8 @@ static int lua_nups (StkId f) {
int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) { int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
int refi; int refi;
if (lineinfo == NULL) return -1; /* no line info */ if (lineinfo == NULL || pc == -1)
else if (pc == -1) return refline; /* function preamble */ return -1; /* no line info or function is not active */
refi = prefi ? *prefi : 0; refi = prefi ? *prefi : 0;
if (lineinfo[refi] < 0) if (lineinfo[refi] < 0)
refline += -lineinfo[refi++]; refline += -lineinfo[refi++];
@ -124,7 +124,10 @@ int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
static int lua_currentpc (StkId f) { static int lua_currentpc (StkId f) {
CallInfo *ci = infovalue(f); CallInfo *ci = infovalue(f);
LUA_ASSERT(isLmark(f), "function has no pc"); LUA_ASSERT(isLmark(f), "function has no pc");
return (*ci->pc - ci->func->f.l->code) - 1; if (ci->pc)
return (*ci->pc - ci->func->f.l->code) - 1;
else
return -1; /* function is not active */
} }

46
ldo.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.c,v 1.102 2000/10/05 12:14:08 roberto Exp roberto $ ** $Id: ldo.c,v 1.103 2000/10/05 13:00:17 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
*/ */
@ -41,7 +41,7 @@ void luaD_init (lua_State *L, int stacksize) {
void luaD_checkstack (lua_State *L, int n) { void luaD_checkstack (lua_State *L, int n) {
if (L->stack_last-L->top <= n) { /* stack overflow? */ if (L->stack_last - L->top <= n) { /* stack overflow? */
if (L->stack_last-L->stack > (L->stacksize-1)) { if (L->stack_last-L->stack > (L->stacksize-1)) {
/* overflow while handling overflow: do what?? */ /* overflow while handling overflow: do what?? */
L->top -= EXTRA_STACK; L->top -= EXTRA_STACK;
@ -112,19 +112,19 @@ void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook) {
} }
void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook, static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook,
const char *event) { const char *event) {
if (L->allowhooks) { if (L->allowhooks) {
lua_Debug ar; lua_Debug ar;
ar._func = func; ar._func = func;
ar.event = event; ar.event = event;
infovalue(func)->pc = NULL; /* function is not active */
dohook(L, &ar, callhook); dohook(L, &ar, callhook);
} }
} }
static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
lua_Hook callhook = L->callhook;
int nup = cl->nupvalues; /* number of upvalues */ int nup = cl->nupvalues; /* number of upvalues */
StkId old_Cbase = L->Cbase; StkId old_Cbase = L->Cbase;
int n; int n;
@ -132,11 +132,7 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */ luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */
for (n=0; n<nup; n++) /* copy upvalues as extra arguments */ for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
*(L->top++) = cl->upvalue[n]; *(L->top++) = cl->upvalue[n];
if (callhook)
luaD_callHook(L, base-1, callhook, "call");
n = (*cl->f.c)(L); /* do the actual call */ n = (*cl->f.c)(L); /* do the actual call */
if (callhook) /* same hook that was active at entry */
luaD_callHook(L, base-1, callhook, "return");
L->Cbase = old_Cbase; /* restore old C base */ L->Cbase = old_Cbase; /* restore old C base */
return L->top - n; /* return index of first result */ return L->top - n; /* return index of first result */
} }
@ -159,6 +155,7 @@ void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults) {
** The number of results is nResults, unless nResults=LUA_MULTRET. ** The number of results is nResults, unless nResults=LUA_MULTRET.
*/ */
void luaD_call (lua_State *L, StkId func, int nResults) { void luaD_call (lua_State *L, StkId func, int nResults) {
lua_Hook callhook;
StkId firstResult; StkId firstResult;
CallInfo ci; CallInfo ci;
Closure *cl; Closure *cl;
@ -175,22 +172,29 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
ci.func = cl; ci.func = cl;
infovalue(func) = &ci; infovalue(func) = &ci;
ttype(func) = LUA_TMARK; ttype(func) = LUA_TMARK;
if (cl->isC) callhook = L->callhook;
firstResult = callCclosure(L, cl, func+1); if (callhook)
else luaD_callHook(L, func, callhook, "call");
firstResult = luaV_execute(L, cl, func+1); firstResult = (cl->isC ? callCclosure(L, cl, func+1) :
luaV_execute(L, cl, func+1));
if (callhook) /* same hook that was active at entry */
luaD_callHook(L, func, callhook, "return");
LUA_ASSERT(ttype(func) == LUA_TMARK, "invalid tag"); LUA_ASSERT(ttype(func) == LUA_TMARK, "invalid tag");
/* adjust the number of results */
if (nResults == LUA_MULTRET)
nResults = L->top - firstResult;
else
luaD_adjusttop(L, firstResult, nResults);
/* move results to `func' (to erase parameters and function) */ /* move results to `func' (to erase parameters and function) */
while (nResults) { if (nResults == LUA_MULTRET) {
*func++ = *(L->top - nResults); while (firstResult < L->top) /* copy all results */
nResults--; *func++ = *firstResult++;
L->top = func;
}
else { /* copy at most `nResults' */
for (; nResults > 0 && firstResult < L->top; nResults--)
*func++ = *firstResult++;
L->top = func;
for (; nResults > 0; nResults--) { /* if there are not enough results */
ttype(L->top) = LUA_TNIL; /* adjust the stack */
incr_top; /* must check stack space */
}
} }
L->top = func;
luaC_checkGC(L); luaC_checkGC(L);
} }

4
ldo.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.h,v 1.26 2000/10/04 12:16:08 roberto Exp roberto $ ** $Id: ldo.h,v 1.27 2000/10/05 13:00:17 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
*/ */
@ -22,8 +22,6 @@
void luaD_init (lua_State *L, int stacksize); void luaD_init (lua_State *L, int stacksize);
void luaD_adjusttop (lua_State *L, StkId base, int extra); void luaD_adjusttop (lua_State *L, StkId base, int extra);
void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook); void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook);
void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook,
const char *event);
void luaD_call (lua_State *L, StkId func, int nResults); void luaD_call (lua_State *L, StkId func, int nResults);
void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults); void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults);
void luaD_checkstack (lua_State *L, int n); void luaD_checkstack (lua_State *L, int n);

27
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.143 2000/10/05 12:14:08 roberto Exp roberto $ ** $Id: lvm.c,v 1.144 2000/10/05 13:00:17 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -67,7 +67,7 @@ int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
static void traceexec (lua_State *L, StkId base, StkId top, lua_Hook linehook) { static void traceexec (lua_State *L, StkId base, StkId top, lua_Hook linehook) {
CallInfo *ci = infovalue(base-1); CallInfo *ci = infovalue(base-1);
int *lineinfo = ci->func->f.l->lineinfo; int *lineinfo = ci->func->f.l->lineinfo;
int pc = (*ci->pc - 1) - ci->func->f.l->code; int pc = (*ci->pc - ci->func->f.l->code) - 1;
int newline; int newline;
if (pc == 0) { /* may be first time? */ if (pc == 0) { /* may be first time? */
ci->line = 1; ci->line = 1;
@ -349,22 +349,18 @@ static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
** Returns n such that the the results are between [n,top). ** Returns n such that the the results are between [n,top).
*/ */
StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
const Proto *tf = cl->f.l; const Proto *const tf = cl->f.l;
StkId top; /* keep top local, for performance */ StkId top; /* keep top local, for performance */
const Instruction *pc = tf->code; const Instruction *pc = tf->code;
TString **kstr = tf->kstr; TString **const kstr = tf->kstr;
lua_Hook callhook = L->callhook; const lua_Hook linehook = L->linehook;
lua_Hook linehook; /* set it only after calling eventual call hook */
infovalue(base-1)->pc = &pc; infovalue(base-1)->pc = &pc;
luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK); luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK);
if (tf->is_vararg) /* varargs? */ if (tf->is_vararg) /* varargs? */
adjust_varargs(L, base, tf->numparams); adjust_varargs(L, base, tf->numparams);
else else
luaD_adjusttop(L, base, tf->numparams); luaD_adjusttop(L, base, tf->numparams);
if (callhook)
luaD_callHook(L, base-1, callhook, "call");
top = L->top; top = L->top;
linehook = L->linehook;
/* main loop of interpreter */ /* main loop of interpreter */
for (;;) { for (;;) {
const Instruction i = *pc++; const Instruction i = *pc++;
@ -373,12 +369,11 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
switch (GET_OPCODE(i)) { switch (GET_OPCODE(i)) {
case OP_END: { case OP_END: {
L->top = top; L->top = top;
goto endloop; return top;
} }
case OP_RETURN: { case OP_RETURN: {
L->top = top; L->top = top;
top = base+GETARG_U(i); return base+GETARG_U(i);
goto endloop;
} }
case OP_CALL: { case OP_CALL: {
int nres = GETARG_B(i); int nres = GETARG_B(i);
@ -391,8 +386,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
case OP_TAILCALL: { case OP_TAILCALL: {
L->top = top; L->top = top;
luaD_call(L, base+GETARG_A(i), LUA_MULTRET); luaD_call(L, base+GETARG_A(i), LUA_MULTRET);
top = base+GETARG_B(i); return base+GETARG_B(i);
goto endloop;
} }
case OP_PUSHNIL: { case OP_PUSHNIL: {
int n = GETARG_U(i); int n = GETARG_U(i);
@ -712,8 +706,5 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
break; break;
} }
} }
} endloop: }
if (callhook) /* same hook that was active at entry */
luaD_callHook(L, base-1, callhook, "return");
return top;
} }