From 1f81baffadad9d955b030a1a29b9b06042a66552 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 25 Jan 2021 10:39:18 -0300 Subject: [PATCH] Janitorial work Comments, code details, identation. --- lapi.c | 3 ++- lbaselib.c | 3 ++- ldo.c | 55 ++++++++++++++++++++++++++++++------------------------ lstate.h | 10 ++++++++++ 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/lapi.c b/lapi.c index 3583e9c0..163533a2 100644 --- a/lapi.c +++ b/lapi.c @@ -74,7 +74,8 @@ static TValue *index2value (lua_State *L, int idx) { return &G(L)->nilvalue; /* it has no upvalues */ else { 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; } } } diff --git a/lbaselib.c b/lbaselib.c index 747fd45a..60786b3d 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -182,7 +182,8 @@ static int luaB_rawset (lua_State *L) { 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; } diff --git a/ldo.c b/ldo.c index 57d4f7d5..9076c0ed 100644 --- a/ldo.c +++ b/ldo.c @@ -295,8 +295,8 @@ void luaD_hook (lua_State *L, int event, int line, if (hook && L->allowhook) { /* make sure there is a hook */ int mask = CIST_HOOKED; CallInfo *ci = L->ci; - ptrdiff_t top = savestack(L, L->top); - ptrdiff_t ci_top = savestack(L, ci->top); + ptrdiff_t top = savestack(L, L->top); /* preserve original 'top' */ + ptrdiff_t ci_top = savestack(L, ci->top); /* idem for 'ci->top' */ lua_Debug ar; ar.event = event; ar.currentline = line; @@ -307,7 +307,7 @@ void luaD_hook (lua_State *L, int event, int line, ci->u2.transferinfo.ntransfer = ntransfer; } 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; L->allowhook = 0; /* cannot call hooks inside a hook */ ci->callstatus |= mask; @@ -329,39 +329,44 @@ void luaD_hook (lua_State *L, int event, int line, ** active. */ void luaD_hookcall (lua_State *L, CallInfo *ci) { - int hook = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL; - Proto *p; - if (!(L->hookmask & LUA_MASKCALL)) /* some other hook? */ - return; /* don't call hook */ - p = clLvalue(s2v(ci->func))->p; - L->top = ci->top; /* prepare top */ - ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ - luaD_hook(L, hook, -1, 1, p->numparams); - ci->u.l.savedpc--; /* correct 'pc' */ + if (L->hookmask & LUA_MASKCALL) { /* is call hook on? */ + int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL + : LUA_HOOKCALL; + Proto *p = ci_func(ci)->p; + L->top = ci->top; /* prepare top */ + ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ + luaD_hook(L, event, -1, 1, p->numparams); + 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) { - StkId firstres = L->top - nres; /* index of first result */ - ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ - int delta = 0; - if (isLuacode(ci)) { - Proto *p = ci_func(ci)->p; - if (p->is_vararg) - delta = ci->u.l.nextraargs + p->numparams + 1; + if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ + StkId firstres = L->top - nres; /* index of first result */ + ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ + int delta = 0; /* correction for vararg functions */ + int ftransfer; + if (isLuacode(ci)) { + Proto *p = ci_func(ci)->p; + if (p->is_vararg) + delta = ci->u.l.nextraargs + p->numparams + 1; if (L->top < ci->top) 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' */ ftransfer = cast(unsigned short, firstres - ci->func); luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ ci->func -= delta; + L->top = restorestack(L, oldtop); } if (isLua(ci = ci->previous)) 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 */ /* 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); for (; i < wanted; i++) /* complete wanted number of results */ setnilvalue(s2v(res + i)); diff --git a/lstate.h b/lstate.h index 65d45264..f3d791ab 100644 --- a/lstate.h +++ b/lstate.h @@ -156,6 +156,16 @@ typedef struct stringtable { /* ** 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 { StkId func; /* function index in the stack */