Janitorial work

Comments, code details, identation.
This commit is contained in:
Roberto Ierusalimschy 2021-01-25 10:39:18 -03:00
parent 0e9254dfa0
commit 1f81baffad
4 changed files with 45 additions and 26 deletions

3
lapi.c
View File

@ -74,7 +74,8 @@ static TValue *index2value (lua_State *L, int idx) {
return &G(L)->nilvalue; /* it has no upvalues */ return &G(L)->nilvalue; /* it has no upvalues */
else { else {
CClosure *func = clCvalue(s2v(ci->func)); CClosure *func = clCvalue(s2v(ci->func));
return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue; return (idx <= func->nupvalues) ? &func->upvalue[idx-1]
: &G(L)->nilvalue;
} }
} }
} }

View File

@ -182,7 +182,8 @@ static int luaB_rawset (lua_State *L) {
static int pushmode (lua_State *L, int oldmode) { static int pushmode (lua_State *L, int oldmode) {
lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational"); lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental"
: "generational");
return 1; return 1;
} }

55
ldo.c
View File

@ -295,8 +295,8 @@ void luaD_hook (lua_State *L, int event, int line,
if (hook && L->allowhook) { /* make sure there is a hook */ if (hook && L->allowhook) { /* make sure there is a hook */
int mask = CIST_HOOKED; int mask = CIST_HOOKED;
CallInfo *ci = L->ci; CallInfo *ci = L->ci;
ptrdiff_t top = savestack(L, L->top); ptrdiff_t top = savestack(L, L->top); /* preserve original 'top' */
ptrdiff_t ci_top = savestack(L, ci->top); ptrdiff_t ci_top = savestack(L, ci->top); /* idem for 'ci->top' */
lua_Debug ar; lua_Debug ar;
ar.event = event; ar.event = event;
ar.currentline = line; ar.currentline = line;
@ -307,7 +307,7 @@ void luaD_hook (lua_State *L, int event, int line,
ci->u2.transferinfo.ntransfer = ntransfer; ci->u2.transferinfo.ntransfer = ntransfer;
} }
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
if (L->top + LUA_MINSTACK > ci->top) if (ci->top < L->top + LUA_MINSTACK)
ci->top = L->top + LUA_MINSTACK; ci->top = L->top + LUA_MINSTACK;
L->allowhook = 0; /* cannot call hooks inside a hook */ L->allowhook = 0; /* cannot call hooks inside a hook */
ci->callstatus |= mask; ci->callstatus |= mask;
@ -329,39 +329,44 @@ void luaD_hook (lua_State *L, int event, int line,
** active. ** active.
*/ */
void luaD_hookcall (lua_State *L, CallInfo *ci) { void luaD_hookcall (lua_State *L, CallInfo *ci) {
int hook = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL; if (L->hookmask & LUA_MASKCALL) { /* is call hook on? */
Proto *p; int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL
if (!(L->hookmask & LUA_MASKCALL)) /* some other hook? */ : LUA_HOOKCALL;
return; /* don't call hook */ Proto *p = ci_func(ci)->p;
p = clLvalue(s2v(ci->func))->p; L->top = ci->top; /* prepare top */
L->top = ci->top; /* prepare top */ ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */
ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ luaD_hook(L, event, -1, 1, p->numparams);
luaD_hook(L, hook, -1, 1, p->numparams); ci->u.l.savedpc--; /* correct 'pc' */
ci->u.l.savedpc--; /* correct 'pc' */ }
} }
/*
** Executes a call hook for Lua and C functions. This function is called
** whenever 'hookmask' is not zero, so it checks whether return hooks are
** active.
*/
static void rethook (lua_State *L, CallInfo *ci, int nres) { static void rethook (lua_State *L, CallInfo *ci, int nres) {
StkId firstres = L->top - nres; /* index of first result */ if (L->hookmask & LUA_MASKRET) { /* is return hook on? */
ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ StkId firstres = L->top - nres; /* index of first result */
int delta = 0; ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */
if (isLuacode(ci)) { int delta = 0; /* correction for vararg functions */
Proto *p = ci_func(ci)->p; int ftransfer;
if (p->is_vararg) if (isLuacode(ci)) {
delta = ci->u.l.nextraargs + p->numparams + 1; Proto *p = ci_func(ci)->p;
if (p->is_vararg)
delta = ci->u.l.nextraargs + p->numparams + 1;
if (L->top < ci->top) if (L->top < ci->top)
L->top = ci->top; /* correct top to run hook */ L->top = ci->top; /* correct top to run hook */
} }
if (L->hookmask & LUA_MASKRET) { /* is return hook on? */
int ftransfer;
ci->func += delta; /* if vararg, back to virtual 'func' */ ci->func += delta; /* if vararg, back to virtual 'func' */
ftransfer = cast(unsigned short, firstres - ci->func); ftransfer = cast(unsigned short, firstres - ci->func);
luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */
ci->func -= delta; ci->func -= delta;
L->top = restorestack(L, oldtop);
} }
if (isLua(ci = ci->previous)) if (isLua(ci = ci->previous))
L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */ L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */
L->top = restorestack(L, oldtop);
} }
@ -420,7 +425,9 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) {
} }
firstresult = L->top - nres; /* index of first result */ firstresult = L->top - nres; /* index of first result */
/* move all results to correct place */ /* move all results to correct place */
for (i = 0; i < nres && i < wanted; i++) if (nres > wanted)
nres = wanted; /* don't need more than that */
for (i = 0; i < nres; i++)
setobjs2s(L, res + i, firstresult + i); setobjs2s(L, res + i, firstresult + i);
for (; i < wanted; i++) /* complete wanted number of results */ for (; i < wanted; i++) /* complete wanted number of results */
setnilvalue(s2v(res + i)); setnilvalue(s2v(res + i));

View File

@ -156,6 +156,16 @@ typedef struct stringtable {
/* /*
** Information about a call. ** Information about a call.
** About union 'u':
** - field 'l' is used only for Lua functions;
** - field 'c' is used only for C functions.
** About union 'u2':
** - field 'funcidx' is used only by C functions while doing a
** protected call;
** - field 'nyield' is used only while a function is "doing" an
** yield (from the yield until the next resume);
** - field 'transferinfo' is used only during call/returnhooks,
** before the function starts or after it ends.
*/ */
typedef struct CallInfo { typedef struct CallInfo {
StkId func; /* function index in the stack */ StkId func; /* function index in the stack */