aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-01-25 10:39:18 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-01-25 10:39:18 -0300
commit1f81baffadad9d955b030a1a29b9b06042a66552 (patch)
tree7a7b06eed86f59b68fb8bc4250726e45c1323202
parent0e9254dfa03d95c3aa2888cf78e9a30bc88d41bc (diff)
downloadlua-1f81baffadad9d955b030a1a29b9b06042a66552.tar.gz
lua-1f81baffadad9d955b030a1a29b9b06042a66552.tar.bz2
lua-1f81baffadad9d955b030a1a29b9b06042a66552.zip
Janitorial work
Comments, code details, identation.
-rw-r--r--lapi.c3
-rw-r--r--lbaselib.c3
-rw-r--r--ldo.c55
-rw-r--r--lstate.h10
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) {
74 return &G(L)->nilvalue; /* it has no upvalues */ 74 return &G(L)->nilvalue; /* it has no upvalues */
75 else { 75 else {
76 CClosure *func = clCvalue(s2v(ci->func)); 76 CClosure *func = clCvalue(s2v(ci->func));
77 return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue; 77 return (idx <= func->nupvalues) ? &func->upvalue[idx-1]
78 : &G(L)->nilvalue;
78 } 79 }
79 } 80 }
80} 81}
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) {
182 182
183 183
184static int pushmode (lua_State *L, int oldmode) { 184static int pushmode (lua_State *L, int oldmode) {
185 lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational"); 185 lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental"
186 : "generational");
186 return 1; 187 return 1;
187} 188}
188 189
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,
295 if (hook && L->allowhook) { /* make sure there is a hook */ 295 if (hook && L->allowhook) { /* make sure there is a hook */
296 int mask = CIST_HOOKED; 296 int mask = CIST_HOOKED;
297 CallInfo *ci = L->ci; 297 CallInfo *ci = L->ci;
298 ptrdiff_t top = savestack(L, L->top); 298 ptrdiff_t top = savestack(L, L->top); /* preserve original 'top' */
299 ptrdiff_t ci_top = savestack(L, ci->top); 299 ptrdiff_t ci_top = savestack(L, ci->top); /* idem for 'ci->top' */
300 lua_Debug ar; 300 lua_Debug ar;
301 ar.event = event; 301 ar.event = event;
302 ar.currentline = line; 302 ar.currentline = line;
@@ -307,7 +307,7 @@ void luaD_hook (lua_State *L, int event, int line,
307 ci->u2.transferinfo.ntransfer = ntransfer; 307 ci->u2.transferinfo.ntransfer = ntransfer;
308 } 308 }
309 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ 309 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
310 if (L->top + LUA_MINSTACK > ci->top) 310 if (ci->top < L->top + LUA_MINSTACK)
311 ci->top = L->top + LUA_MINSTACK; 311 ci->top = L->top + LUA_MINSTACK;
312 L->allowhook = 0; /* cannot call hooks inside a hook */ 312 L->allowhook = 0; /* cannot call hooks inside a hook */
313 ci->callstatus |= mask; 313 ci->callstatus |= mask;
@@ -329,39 +329,44 @@ void luaD_hook (lua_State *L, int event, int line,
329** active. 329** active.
330*/ 330*/
331void luaD_hookcall (lua_State *L, CallInfo *ci) { 331void luaD_hookcall (lua_State *L, CallInfo *ci) {
332 int hook = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL; 332 if (L->hookmask & LUA_MASKCALL) { /* is call hook on? */
333 Proto *p; 333 int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL
334 if (!(L->hookmask & LUA_MASKCALL)) /* some other hook? */ 334 : LUA_HOOKCALL;
335 return; /* don't call hook */ 335 Proto *p = ci_func(ci)->p;
336 p = clLvalue(s2v(ci->func))->p; 336 L->top = ci->top; /* prepare top */
337 L->top = ci->top; /* prepare top */ 337 ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */
338 ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ 338 luaD_hook(L, event, -1, 1, p->numparams);
339 luaD_hook(L, hook, -1, 1, p->numparams); 339 ci->u.l.savedpc--; /* correct 'pc' */
340 ci->u.l.savedpc--; /* correct 'pc' */ 340 }
341} 341}
342 342
343 343
344/*
345** Executes a call hook for Lua and C functions. This function is called
346** whenever 'hookmask' is not zero, so it checks whether return hooks are
347** active.
348*/
344static void rethook (lua_State *L, CallInfo *ci, int nres) { 349static void rethook (lua_State *L, CallInfo *ci, int nres) {
345 StkId firstres = L->top - nres; /* index of first result */
346 ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */
347 int delta = 0;
348 if (isLuacode(ci)) {
349 Proto *p = ci_func(ci)->p;
350 if (p->is_vararg)
351 delta = ci->u.l.nextraargs + p->numparams + 1;
352 if (L->top < ci->top)
353 L->top = ci->top; /* correct top to run hook */
354 }
355 if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ 350 if (L->hookmask & LUA_MASKRET) { /* is return hook on? */
351 StkId firstres = L->top - nres; /* index of first result */
352 ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */
353 int delta = 0; /* correction for vararg functions */
356 int ftransfer; 354 int ftransfer;
355 if (isLuacode(ci)) {
356 Proto *p = ci_func(ci)->p;
357 if (p->is_vararg)
358 delta = ci->u.l.nextraargs + p->numparams + 1;
359 if (L->top < ci->top)
360 L->top = ci->top; /* correct top to run hook */
361 }
357 ci->func += delta; /* if vararg, back to virtual 'func' */ 362 ci->func += delta; /* if vararg, back to virtual 'func' */
358 ftransfer = cast(unsigned short, firstres - ci->func); 363 ftransfer = cast(unsigned short, firstres - ci->func);
359 luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ 364 luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */
360 ci->func -= delta; 365 ci->func -= delta;
366 L->top = restorestack(L, oldtop);
361 } 367 }
362 if (isLua(ci = ci->previous)) 368 if (isLua(ci = ci->previous))
363 L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */ 369 L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */
364 L->top = restorestack(L, oldtop);
365} 370}
366 371
367 372
@@ -420,7 +425,9 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) {
420 } 425 }
421 firstresult = L->top - nres; /* index of first result */ 426 firstresult = L->top - nres; /* index of first result */
422 /* move all results to correct place */ 427 /* move all results to correct place */
423 for (i = 0; i < nres && i < wanted; i++) 428 if (nres > wanted)
429 nres = wanted; /* don't need more than that */
430 for (i = 0; i < nres; i++)
424 setobjs2s(L, res + i, firstresult + i); 431 setobjs2s(L, res + i, firstresult + i);
425 for (; i < wanted; i++) /* complete wanted number of results */ 432 for (; i < wanted; i++) /* complete wanted number of results */
426 setnilvalue(s2v(res + i)); 433 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 {
156 156
157/* 157/*
158** Information about a call. 158** Information about a call.
159** About union 'u':
160** - field 'l' is used only for Lua functions;
161** - field 'c' is used only for C functions.
162** About union 'u2':
163** - field 'funcidx' is used only by C functions while doing a
164** protected call;
165** - field 'nyield' is used only while a function is "doing" an
166** yield (from the yield until the next resume);
167** - field 'transferinfo' is used only during call/returnhooks,
168** before the function starts or after it ends.
159*/ 169*/
160typedef struct CallInfo { 170typedef struct CallInfo {
161 StkId func; /* function index in the stack */ 171 StkId func; /* function index in the stack */