diff --git a/ldebug.c b/ldebug.c index f3b92af9..1aff2064 100644 --- a/ldebug.c +++ b/ldebug.c @@ -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 ** 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 refi; - if (lineinfo == NULL) return -1; /* no line info */ - else if (pc == -1) return refline; /* function preamble */ + if (lineinfo == NULL || pc == -1) + return -1; /* no line info or function is not active */ refi = prefi ? *prefi : 0; if (lineinfo[refi] < 0) refline += -lineinfo[refi++]; @@ -124,7 +124,10 @@ int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) { static int lua_currentpc (StkId f) { CallInfo *ci = infovalue(f); 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 */ } diff --git a/ldo.c b/ldo.c index 11d6c5a6..a3752042 100644 --- a/ldo.c +++ b/ldo.c @@ -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 ** 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) { - 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)) { /* overflow while handling overflow: do what?? */ 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) { if (L->allowhooks) { lua_Debug ar; ar._func = func; ar.event = event; + infovalue(func)->pc = NULL; /* function is not active */ dohook(L, &ar, callhook); } } static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { - lua_Hook callhook = L->callhook; int nup = cl->nupvalues; /* number of upvalues */ StkId old_Cbase = L->Cbase; 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 */ for (n=0; ntop++) = cl->upvalue[n]; - if (callhook) - luaD_callHook(L, base-1, callhook, "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 */ 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. */ void luaD_call (lua_State *L, StkId func, int nResults) { + lua_Hook callhook; StkId firstResult; CallInfo ci; Closure *cl; @@ -175,22 +172,29 @@ void luaD_call (lua_State *L, StkId func, int nResults) { ci.func = cl; infovalue(func) = &ci; ttype(func) = LUA_TMARK; - if (cl->isC) - firstResult = callCclosure(L, cl, func+1); - else - firstResult = luaV_execute(L, cl, func+1); + callhook = L->callhook; + if (callhook) + luaD_callHook(L, func, callhook, "call"); + 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"); - /* 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) */ - while (nResults) { - *func++ = *(L->top - nResults); - nResults--; + if (nResults == LUA_MULTRET) { + while (firstResult < L->top) /* copy all results */ + *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); } diff --git a/ldo.h b/ldo.h index 1058d34e..7cf5af38 100644 --- a/ldo.h +++ b/ldo.h @@ -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 ** See Copyright Notice in lua.h */ @@ -22,8 +22,6 @@ void luaD_init (lua_State *L, int stacksize); 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_callHook (lua_State *L, StkId func, lua_Hook callhook, - const char *event); void luaD_call (lua_State *L, StkId func, int nResults); void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults); void luaD_checkstack (lua_State *L, int n); diff --git a/lvm.c b/lvm.c index 48f78d01..356dc4a1 100644 --- a/lvm.c +++ b/lvm.c @@ -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 ** 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) { CallInfo *ci = infovalue(base-1); 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; if (pc == 0) { /* may be first time? */ 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). */ 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 */ const Instruction *pc = tf->code; - TString **kstr = tf->kstr; - lua_Hook callhook = L->callhook; - lua_Hook linehook; /* set it only after calling eventual call hook */ + TString **const kstr = tf->kstr; + const lua_Hook linehook = L->linehook; infovalue(base-1)->pc = &pc; luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK); if (tf->is_vararg) /* varargs? */ adjust_varargs(L, base, tf->numparams); else luaD_adjusttop(L, base, tf->numparams); - if (callhook) - luaD_callHook(L, base-1, callhook, "call"); top = L->top; - linehook = L->linehook; /* main loop of interpreter */ for (;;) { const Instruction i = *pc++; @@ -373,12 +369,11 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { switch (GET_OPCODE(i)) { case OP_END: { L->top = top; - goto endloop; + return top; } case OP_RETURN: { L->top = top; - top = base+GETARG_U(i); - goto endloop; + return base+GETARG_U(i); } case OP_CALL: { int nres = GETARG_B(i); @@ -391,8 +386,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { case OP_TAILCALL: { L->top = top; luaD_call(L, base+GETARG_A(i), LUA_MULTRET); - top = base+GETARG_B(i); - goto endloop; + return base+GETARG_B(i); } case OP_PUSHNIL: { int n = GETARG_U(i); @@ -712,8 +706,5 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { break; } } - } endloop: - if (callhook) /* same hook that was active at entry */ - luaD_callHook(L, base-1, callhook, "return"); - return top; + } }