LCOV - code coverage report
Current view: top level - AP_Scripting/lua/src - ldo.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 224 374 59.9 %
Date: 2026-09-09 11:02:08 Functions: 21 32 65.6 %

          Line data    Source code
       1             : /*
       2             : ** $Id: ldo.c,v 2.157.1.1 2017/04/19 17:20:42 roberto Exp $
       3             : ** Stack and Call structure of Lua
       4             : ** See Copyright Notice in lua.h
       5             : */
       6             : 
       7             : #define ldo_c
       8             : #define LUA_CORE
       9             : 
      10             : #include "lprefix.h"
      11             : 
      12             : 
      13             : #include <AP_HAL/ap_setjmp.h>
      14             : #include <stdlib.h>
      15             : #include <string.h>
      16             : 
      17             : #include "lua.h"
      18             : 
      19             : #include "lapi.h"
      20             : #include "ldebug.h"
      21             : #include "ldo.h"
      22             : #include "lfunc.h"
      23             : #include "lgc.h"
      24             : #include "lmem.h"
      25             : #include "lobject.h"
      26             : #include "lopcodes.h"
      27             : #include "lparser.h"
      28             : #include "lstate.h"
      29             : #include "lstring.h"
      30             : #include "ltable.h"
      31             : #include "ltm.h"
      32             : #include "lundump.h"
      33             : #include "lvm.h"
      34             : #include "lzio.h"
      35             : 
      36             : // lua code does lots of casting, these warnings are not helpful
      37             : #pragma GCC diagnostic ignored "-Wcast-align"
      38             : 
      39             : #define errorstatus(s)  ((s) > LUA_YIELD)
      40             : 
      41             : 
      42             : /*
      43             : ** {======================================================
      44             : ** Error-recovery functions
      45             : ** =======================================================
      46             : */
      47             : 
      48             : /*
      49             : ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By
      50             : ** default, Lua handles errors with exceptions when compiling as
      51             : ** C++ code, with _longjmp/_setjmp when asked to use them, and with
      52             : ** longjmp/setjmp otherwise.
      53             : */
      54             : #if !defined(LUAI_THROW)                                /* { */
      55             : 
      56             : #if defined(__cplusplus) && !defined(LUA_USE_LONGJMP)   /* { */
      57             : 
      58             : /* C++ exceptions */
      59             : #define LUAI_THROW(L,c)         throw(c)
      60             : #define LUAI_TRY(L,c,a) \
      61             :         try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; }
      62             : #define luai_jmpbuf             int  /* dummy variable */
      63             : 
      64             : #elif defined(LUA_USE_POSIX)                            /* }{ */
      65             : 
      66             : /* in POSIX, try _longjmp/_setjmp (more efficient) */
      67             : #define LUAI_THROW(L,c)         _longjmp((c)->b, 1)
      68             : #define LUAI_TRY(L,c,a)         if (_setjmp((c)->b) == 0) { a }
      69             : #define luai_jmpbuf             jmp_buf
      70             : 
      71             : #else                                                   /* }{ */
      72             : 
      73             : /* ISO C handling with long jumps */
      74             : #define LUAI_THROW(L,c)         ap_longjmp((c)->b, 1)
      75             : #define LUAI_TRY(L,c,a)         if (ap_setjmp((c)->b) == 0) { a }
      76             : #define luai_jmpbuf             ap_jmp_buf
      77             : 
      78             : #endif                                                  /* } */
      79             : 
      80             : #endif                                                  /* } */
      81             : 
      82             : 
      83             : 
      84             : /* chain list of long jump buffers */
      85             : struct lua_longjmp {
      86             :   struct lua_longjmp *previous;
      87             :   luai_jmpbuf b;
      88             :   volatile int status;  /* error code */
      89             : };
      90             : 
      91             : 
      92          58 : static void seterrorobj (lua_State *L, int errcode, StkId oldtop) {
      93          58 :   switch (errcode) {
      94           0 :     case LUA_ERRMEM: {  /* memory error? */
      95           0 :       setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
      96           0 :       break;
      97             :     }
      98           0 :     case LUA_ERRERR: {
      99           0 :       setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
     100           0 :       break;
     101             :     }
     102          58 :     default: {
     103          58 :       setobjs2s(L, oldtop, L->top - 1);  /* error message on current top */
     104          58 :       break;
     105             :     }
     106             :   }
     107          58 :   L->top = oldtop + 1;
     108          58 : }
     109             : 
     110             : 
     111          58 : l_noret luaD_throw (lua_State *L, int errcode) {
     112          58 :   if (L->errorJmp) {  /* thread has an error handler? */
     113          58 :     L->errorJmp->status = errcode;  /* set status */
     114          58 :     LUAI_THROW(L, L->errorJmp);  /* jump to it */
     115             :   }
     116             :   else {  /* thread has no error handler */
     117           0 :     global_State *g = G(L);
     118           0 :     L->status = cast_byte(errcode);  /* mark it as dead */
     119           0 :     if (g->mainthread->errorJmp) {  /* main thread has a handler? */
     120           0 :       setobjs2s(L, g->mainthread->top++, L->top - 1);  /* copy error obj. */
     121           0 :       luaD_throw(g->mainthread, errcode);  /* re-throw in main thread */
     122             :     }
     123             :     else {  /* no handler at all; abort */
     124           0 :       if (g->panic) {  /* panic function? */
     125           0 :         seterrorobj(L, errcode, L->top);  /* assume EXTRA_STACK */
     126           0 :         if (L->ci->top < L->top)
     127           0 :           L->ci->top = L->top;  /* pushing msg. can break this invariant */
     128             :         lua_unlock(L);
     129           0 :         g->panic(L);  /* call panic function (last chance to jump out) */
     130             :       }
     131           0 :       lua_abort();
     132             :     }
     133             :   }
     134             : }
     135             : 
     136             : 
     137      263742 : int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
     138      263742 :   unsigned short oldnCcalls = L->nCcalls;
     139             :   struct lua_longjmp lj;
     140      263742 :   lj.status = LUA_OK;
     141      263742 :   lj.previous = L->errorJmp;  /* chain new error handler */
     142      263742 :   L->errorJmp = &lj;
     143      263742 :   LUAI_TRY(L, &lj,
     144             :     (*f)(L, ud);
     145             :   );
     146      263695 :   L->errorJmp = lj.previous;  /* restore old error handler */
     147      263695 :   L->nCcalls = oldnCcalls;
     148      263695 :   return lj.status;
     149             : }
     150             : 
     151             : /* }====================================================== */
     152             : 
     153             : 
     154             : /*
     155             : ** {==================================================================
     156             : ** Stack reallocation
     157             : ** ===================================================================
     158             : */
     159      184821 : static void correctstack (lua_State *L, TValue *oldstack) {
     160             :   CallInfo *ci;
     161             :   UpVal *up;
     162      184821 :   L->top = (L->top - oldstack) + L->stack;
     163      185204 :   for (up = L->openupval; up != NULL; up = up->u.open.next)
     164         383 :     up->v = (up->v - oldstack) + L->stack;
     165      719625 :   for (ci = L->ci; ci != NULL; ci = ci->previous) {
     166      534804 :     ci->top = (ci->top - oldstack) + L->stack;
     167      534804 :     ci->func = (ci->func - oldstack) + L->stack;
     168      534804 :     if (isLua(ci))
     169      137751 :       ci->u.l.base = (ci->u.l.base - oldstack) + L->stack;
     170             :   }
     171      184821 : }
     172             : 
     173             : 
     174             : /* some space for error handling */
     175             : #define ERRORSTACKSIZE  (LUAI_MAXSTACK + 200)
     176             : 
     177             : 
     178      184821 : void luaD_reallocstack (lua_State *L, int newsize) {
     179      184821 :   TValue *oldstack = L->stack;
     180      184821 :   int lim = L->stacksize;
     181             :   lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
     182             :   lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK);
     183      184821 :   luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue);
     184     3688304 :   for (; lim < newsize; lim++)
     185     3503483 :     setnilvalue(L->stack + lim); /* erase new segment */
     186      184821 :   L->stacksize = newsize;
     187      184821 :   L->stack_last = L->stack + newsize - EXTRA_STACK;
     188      184821 :   correctstack(L, oldstack);
     189      184821 : }
     190             : 
     191             : 
     192       94907 : void luaD_growstack (lua_State *L, int n) {
     193       94907 :   int size = L->stacksize;
     194       94907 :   if (size > LUAI_MAXSTACK)  /* error after extra size? */
     195           0 :     luaD_throw(L, LUA_ERRERR);
     196             :   else {
     197       94907 :     int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK;
     198       94907 :     int newsize = 2 * size;
     199       94907 :     if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK;
     200       94907 :     if (newsize < needed) newsize = needed;
     201       94907 :     if (newsize > LUAI_MAXSTACK) {  /* stack overflow? */
     202           0 :       luaD_reallocstack(L, ERRORSTACKSIZE);
     203           0 :       luaG_runerror(L, "stack overflow");
     204             :     }
     205             :     else
     206       94907 :       luaD_reallocstack(L, newsize);
     207             :   }
     208       94907 : }
     209             : 
     210             : 
     211       90721 : static int stackinuse (lua_State *L) {
     212             :   CallInfo *ci;
     213       90721 :   StkId lim = L->top;
     214      272549 :   for (ci = L->ci; ci != NULL; ci = ci->previous) {
     215      181828 :     if (lim < ci->top) lim = ci->top;
     216             :   }
     217             :   lua_assert(lim <= L->stack_last);
     218       90721 :   return cast_int(lim - L->stack) + 1;  /* part of stack in use */
     219             : }
     220             : 
     221             : 
     222       90721 : void luaD_shrinkstack (lua_State *L) {
     223       90721 :   int inuse = stackinuse(L);
     224       90721 :   int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK;
     225       90721 :   if (goodsize > LUAI_MAXSTACK)
     226           0 :     goodsize = LUAI_MAXSTACK;  /* respect stack limit */
     227       90721 :   if (L->stacksize > LUAI_MAXSTACK)  /* had been handling stack overflow? */
     228           0 :     luaE_freeCI(L);  /* free all CIs (list grew because of an error) */
     229             :   else
     230       90721 :     luaE_shrinkCI(L);  /* shrink list */
     231             :   /* if thread is currently not handling a stack overflow and its
     232             :      good size is smaller than current size, shrink its stack */
     233       90721 :   if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) &&
     234       90721 :       goodsize < L->stacksize)
     235       89914 :     luaD_reallocstack(L, goodsize);
     236             :   else  /* don't change stack */
     237             :     condmovestack(L,{},{});  /* (change only for debugging) */
     238       90721 : }
     239             : 
     240             : 
     241        2454 : void luaD_inctop (lua_State *L) {
     242        2454 :   luaD_checkstack(L, 1);
     243        2454 :   L->top++;
     244        2454 : }
     245             : 
     246             : /* }================================================================== */
     247             : 
     248             : 
     249             : /*
     250             : ** Call a hook for the given event. Make sure there is a hook to be
     251             : ** called. (Both 'L->hook' and 'L->hookmask', which triggers this
     252             : ** function, can be changed asynchronously by signals.)
     253             : */
     254           1 : void luaD_hook (lua_State *L, int event, int line) {
     255           1 :   lua_Hook hook = L->hook;
     256           1 :   if (hook && L->allowhook) {  /* make sure there is a hook */
     257           1 :     CallInfo *ci = L->ci;
     258           1 :     ptrdiff_t top = savestack(L, L->top);
     259           1 :     ptrdiff_t ci_top = savestack(L, ci->top);
     260             :     lua_Debug ar;
     261           1 :     ar.event = event;
     262           1 :     ar.currentline = line;
     263           1 :     ar.i_ci = ci;
     264           1 :     luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
     265           1 :     ci->top = L->top + LUA_MINSTACK;
     266             :     lua_assert(ci->top <= L->stack_last);
     267           1 :     L->allowhook = 0;  /* cannot call hooks inside a hook */
     268           1 :     ci->callstatus |= CIST_HOOKED;
     269             :     lua_unlock(L);
     270           1 :     (*hook)(L, &ar);
     271             :     lua_lock(L);
     272             :     lua_assert(!L->allowhook);
     273           0 :     L->allowhook = 1;
     274           0 :     ci->top = restorestack(L, ci_top);
     275           0 :     L->top = restorestack(L, top);
     276           0 :     ci->callstatus &= ~CIST_HOOKED;
     277             :   }
     278           0 : }
     279             : 
     280             : 
     281           0 : static void callhook (lua_State *L, CallInfo *ci) {
     282           0 :   int hook = LUA_HOOKCALL;
     283           0 :   ci->u.l.savedpc++;  /* hooks assume 'pc' is already incremented */
     284           0 :   if (isLua(ci->previous) &&
     285           0 :       GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) {
     286           0 :     ci->callstatus |= CIST_TAIL;
     287           0 :     hook = LUA_HOOKTAILCALL;
     288             :   }
     289           0 :   luaD_hook(L, hook, -1);
     290           0 :   ci->u.l.savedpc--;  /* correct 'pc' */
     291           0 : }
     292             : 
     293             : 
     294         155 : static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
     295             :   int i;
     296         155 :   int nfixargs = p->numparams;
     297             :   StkId base, fixed;
     298             :   /* move fixed parameters to final position */
     299         155 :   fixed = L->top - actual;  /* first fixed argument */
     300         155 :   base = L->top;  /* final position of first argument */
     301         225 :   for (i = 0; i < nfixargs && i < actual; i++) {
     302          70 :     setobjs2s(L, L->top++, fixed + i);
     303          70 :     setnilvalue(fixed + i);  /* erase original copy (for GC) */
     304             :   }
     305         155 :   for (; i < nfixargs; i++)
     306           0 :     setnilvalue(L->top++);  /* complete missing arguments */
     307         155 :   return base;
     308             : }
     309             : 
     310             : 
     311             : /*
     312             : ** Check whether __call metafield of 'func' is a function. If so, put
     313             : ** it in stack below original 'func' so that 'luaD_precall' can call
     314             : ** it. Raise an error if __call metafield is not a function.
     315             : */
     316         114 : static void tryfuncTM (lua_State *L, StkId func) {
     317         114 :   const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
     318             :   StkId p;
     319         114 :   if (!ttisfunction(tm))
     320           2 :     luaG_typeerror(L, func, "call");
     321             :   /* Open a hole inside the stack at 'func' */
     322        1120 :   for (p = L->top; p > func; p--)
     323        1008 :     setobjs2s(L, p, p-1);
     324         112 :   L->top++;  /* slot ensured by caller */
     325         112 :   setobj2s(L, func, tm);  /* tag method is the new function to be called */
     326         112 : }
     327             : 
     328             : 
     329             : /*
     330             : ** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'.
     331             : ** Handle most typical cases (zero results for commands, one result for
     332             : ** expressions, multiple results for tail calls/single parameters)
     333             : ** separated.
     334             : */
     335    10796855 : static int moveresults (lua_State *L, const TValue *firstResult, StkId res,
     336             :                                       int nres, int wanted) {
     337    10796855 :   switch (wanted) {  /* handle typical cases separately */
     338     1117547 :     case 0: break;  /* nothing to move */
     339     7988874 :     case 1: {  /* one result needed */
     340     7988874 :       if (nres == 0)   /* no results? */
     341       12030 :         firstResult = luaO_nilobject;  /* adjust with nil */
     342     7988874 :       setobjs2s(L, res, firstResult);  /* move it to proper place */
     343     7988874 :       break;
     344             :     }
     345      744898 :     case LUA_MULTRET: {
     346             :       int i;
     347     1553148 :       for (i = 0; i < nres; i++)  /* move all results to correct place */
     348      808250 :         setobjs2s(L, res + i, firstResult + i);
     349      744898 :       L->top = res + nres;
     350      744898 :       return 0;  /* wanted == LUA_MULTRET */
     351             :     }
     352      945536 :     default: {
     353             :       int i;
     354      945536 :       if (wanted <= nres) {  /* enough results? */
     355     2676454 :         for (i = 0; i < wanted; i++)  /* move wanted results to correct place */
     356     1806988 :           setobjs2s(L, res + i, firstResult + i);
     357             :       }
     358             :       else {  /* not enough results; use all of them plus nils */
     359      147289 :         for (i = 0; i < nres; i++)  /* move all results to correct place */
     360       71219 :           setobjs2s(L, res + i, firstResult + i);
     361      161446 :         for (; i < wanted; i++)  /* complete wanted number of results */
     362       85376 :           setnilvalue(res + i);
     363             :       }
     364      945536 :       break;
     365             :     }
     366             :   }
     367    10051957 :   L->top = res + wanted;  /* top points after the last result */
     368    10051957 :   return 1;
     369             : }
     370             : 
     371             : 
     372             : /*
     373             : ** Finishes a function call: calls hook if necessary, removes CallInfo,
     374             : ** moves current number of results to proper place; returns 0 iff call
     375             : ** wanted multiple (variable number of) results.
     376             : */
     377    10796855 : int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) {
     378             :   StkId res;
     379    10796855 :   int wanted = ci->nresults;
     380    10796855 :   if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
     381           0 :     if (L->hookmask & LUA_MASKRET) {
     382           0 :       ptrdiff_t fr = savestack(L, firstResult);  /* hook may change stack */
     383           0 :       luaD_hook(L, LUA_HOOKRET, -1);
     384           0 :       firstResult = restorestack(L, fr);
     385             :     }
     386           0 :     L->oldpc = ci->previous->u.l.savedpc;  /* 'oldpc' for caller function */
     387             :   }
     388    10796855 :   res = ci->func;  /* res == final position of 1st result */
     389    10796855 :   L->ci = ci->previous;  /* back to caller */
     390             :   /* move results to proper place */
     391    10796855 :   return moveresults(L, firstResult, res, nres, wanted);
     392             : }
     393             : 
     394             : 
     395             : 
     396             : #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
     397             : 
     398             : 
     399             : /* macro to check stack size, preserving 'p' */
     400             : #define checkstackp(L,n,p)  \
     401             :   luaD_checkstackaux(L, n, \
     402             :     ptrdiff_t t__ = savestack(L, p);  /* save 'p' */ \
     403             :     luaC_checkGC(L),  /* stack grow uses memory */ \
     404             :     p = restorestack(L, t__))  /* 'pos' part: restore 'p' */
     405             : 
     406             : 
     407             : /*
     408             : ** Prepares a function call: checks the stack, creates a new CallInfo
     409             : ** entry, fills in the relevant information, calls hook if needed.
     410             : ** If function is a C function, does the call, too. (Otherwise, leave
     411             : ** the execution ('luaV_execute') to the caller, to allow stackless
     412             : ** calls.) Returns true iff function has been executed (C function).
     413             : */
     414    10932449 : int luaD_precall (lua_State *L, StkId func, int nresults) {
     415             :   lua_CFunction f;
     416             :   CallInfo *ci;
     417    10932449 :   switch (ttype(func)) {
     418          73 :     case LUA_TCCL:  /* C closure */
     419          73 :       f = clCvalue(func)->f;
     420          73 :       goto Cfunc;
     421     7759790 :     case LUA_TLCF:  /* light C function */
     422     7759790 :       f = fvalue(func);
     423     7759863 :      Cfunc: {
     424             :       int n;  /* number of returns */
     425     7759863 :       checkstackp(L, LUA_MINSTACK, func);  /* ensure minimum stack size */
     426     7759863 :       ci = next_ci(L);  /* now 'enter' new function */
     427     7759863 :       ci->nresults = nresults;
     428     7759863 :       ci->func = func;
     429     7759863 :       ci->top = L->top + LUA_MINSTACK;
     430             :       lua_assert(ci->top <= L->stack_last);
     431     7759863 :       ci->callstatus = 0;
     432     7759863 :       if (L->hookmask & LUA_MASKCALL)
     433           0 :         luaD_hook(L, LUA_HOOKCALL, -1);
     434             :       lua_unlock(L);
     435     7759863 :       n = (*f)(L);  /* do the actual call */
     436             :       lua_lock(L);
     437             :       api_checknelems(L, n);
     438     7759769 :       luaD_poscall(L, ci, L->top - n, n);
     439     7759769 :       return 1;
     440             :     }
     441     3172472 :     case LUA_TLCL: {  /* Lua function: prepare its call */
     442             :       StkId base;
     443     3172472 :       Proto *p = clLvalue(func)->p;
     444     3172472 :       int n = cast_int(L->top - func) - 1;  /* number of real arguments */
     445     3172472 :       int fsize = p->maxstacksize;  /* frame size */
     446     3172472 :       checkstackp(L, fsize, func);
     447     3172472 :       if (p->is_vararg)
     448         155 :         base = adjust_varargs(L, p, n);
     449             :       else {  /* non vararg function */
     450     3192455 :         for (; n < p->numparams; n++)
     451       20138 :           setnilvalue(L->top++);  /* complete missing arguments */
     452     3172317 :         base = func + 1;
     453             :       }
     454     3172472 :       ci = next_ci(L);  /* now 'enter' new function */
     455     3172472 :       ci->nresults = nresults;
     456     3172472 :       ci->func = func;
     457     3172472 :       ci->u.l.base = base;
     458     3172472 :       L->top = ci->top = base + fsize;
     459             :       lua_assert(ci->top <= L->stack_last);
     460     3172472 :       ci->u.l.savedpc = p->code;  /* starting point */
     461     3172472 :       ci->callstatus = CIST_LUA;
     462     3172472 :       if (L->hookmask & LUA_MASKCALL)
     463           0 :         callhook(L, ci);
     464     3172472 :       return 0;
     465             :     }
     466         114 :     default: {  /* not a function */
     467         114 :       checkstackp(L, 1, func);  /* ensure space for metamethod */
     468         114 :       tryfuncTM(L, func);  /* try to get '__call' metamethod */
     469         112 :       return luaD_precall(L, func, nresults);  /* now it must be a function */
     470             :     }
     471             :   }
     472             : }
     473             : 
     474             : 
     475             : /*
     476             : ** Check appropriate error for stack overflow ("regular" overflow or
     477             : ** overflow while handling stack overflow). If 'nCalls' is larger than
     478             : ** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but
     479             : ** smaller than 9/8 of LUAI_MAXCCALLS, does not report an error (to
     480             : ** allow overflow handling to work)
     481             : */
     482           0 : static void stackerror (lua_State *L) {
     483           0 :   if (L->nCcalls == LUAI_MAXCCALLS)
     484           0 :     luaG_runerror(L, "C stack overflow");
     485           0 :   else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
     486           0 :     luaD_throw(L, LUA_ERRERR);  /* error while handing stack error */
     487           0 : }
     488             : 
     489             : 
     490             : /*
     491             : ** Call a function (C or Lua). The function to be called is at *func.
     492             : ** The arguments are on the stack, right after the function.
     493             : ** When returns, all the results are on the stack, starting at the original
     494             : ** function position.
     495             : */
     496     3775720 : void luaD_call (lua_State *L, StkId func, int nResults) {
     497     3775720 :   if (++L->nCcalls >= LUAI_MAXCCALLS)
     498           0 :     stackerror(L);
     499     3775720 :   if (!luaD_precall(L, func, nResults))  /* is a Lua function? */
     500      117719 :     luaV_execute(L);  /* call it */
     501     3775615 :   L->nCcalls--;
     502     3775615 : }
     503             : 
     504             : 
     505             : /*
     506             : ** Similar to 'luaD_call', but does not allow yields during the call
     507             : */
     508      118082 : void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
     509      118082 :   L->nny++;
     510      118082 :   luaD_call(L, func, nResults);
     511      117977 :   L->nny--;
     512      117977 : }
     513             : 
     514             : 
     515             : /*
     516             : ** Completes the execution of an interrupted C function, calling its
     517             : ** continuation function.
     518             : */
     519           0 : static void finishCcall (lua_State *L, int status) {
     520           0 :   CallInfo *ci = L->ci;
     521             :   int n;
     522             :   /* must have a continuation and must be able to call it */
     523             :   lua_assert(ci->u.c.k != NULL && L->nny == 0);
     524             :   /* error status can only happen in a protected call */
     525             :   lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD);
     526           0 :   if (ci->callstatus & CIST_YPCALL) {  /* was inside a pcall? */
     527           0 :     ci->callstatus &= ~CIST_YPCALL;  /* continuation is also inside it */
     528           0 :     L->errfunc = ci->u.c.old_errfunc;  /* with the same error function */
     529             :   }
     530             :   /* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already
     531             :      handled */
     532           0 :   adjustresults(L, ci->nresults);
     533             :   lua_unlock(L);
     534           0 :   n = (*ci->u.c.k)(L, status, ci->u.c.ctx);  /* call continuation function */
     535             :   lua_lock(L);
     536             :   api_checknelems(L, n);
     537           0 :   luaD_poscall(L, ci, L->top - n, n);  /* finish 'luaD_precall' */
     538           0 : }
     539             : 
     540             : 
     541             : /*
     542             : ** Executes "full continuation" (everything in the stack) of a
     543             : ** previously interrupted coroutine until the stack is empty (or another
     544             : ** interruption long-jumps out of the loop). If the coroutine is
     545             : ** recovering from an error, 'ud' points to the error status, which must
     546             : ** be passed to the first continuation function (otherwise the default
     547             : ** status is LUA_YIELD).
     548             : */
     549           0 : static void unroll (lua_State *L, void *ud) {
     550           0 :   if (ud != NULL)  /* error status? */
     551           0 :     finishCcall(L, *(int *)ud);  /* finish 'lua_pcallk' callee */
     552           0 :   while (L->ci != &L->base_ci) {  /* something in the stack */
     553           0 :     if (!isLua(L->ci))  /* C function? */
     554           0 :       finishCcall(L, LUA_YIELD);  /* complete its execution */
     555             :     else {  /* Lua function */
     556           0 :       luaV_finishOp(L);  /* finish interrupted instruction */
     557           0 :       luaV_execute(L);  /* execute down to higher C 'boundary' */
     558             :     }
     559             :   }
     560           0 : }
     561             : 
     562             : 
     563             : /*
     564             : ** Try to find a suspended protected call (a "recover point") for the
     565             : ** given thread.
     566             : */
     567           0 : static CallInfo *findpcall (lua_State *L) {
     568             :   CallInfo *ci;
     569           0 :   for (ci = L->ci; ci != NULL; ci = ci->previous) {  /* search for a pcall */
     570           0 :     if (ci->callstatus & CIST_YPCALL)
     571           0 :       return ci;
     572             :   }
     573           0 :   return NULL;  /* no pending pcall */
     574             : }
     575             : 
     576             : 
     577             : /*
     578             : ** Recovers from an error in a coroutine. Finds a recover point (if
     579             : ** there is one) and completes the execution of the interrupted
     580             : ** 'luaD_pcall'. If there is no recover point, returns zero.
     581             : */
     582           0 : static int recover (lua_State *L, int status) {
     583             :   StkId oldtop;
     584           0 :   CallInfo *ci = findpcall(L);
     585           0 :   if (ci == NULL) return 0;  /* no recovery point */
     586             :   /* "finish" luaD_pcall */
     587           0 :   oldtop = restorestack(L, ci->extra);
     588           0 :   luaF_close(L, oldtop);
     589           0 :   seterrorobj(L, status, oldtop);
     590           0 :   L->ci = ci;
     591           0 :   L->allowhook = getoah(ci->callstatus);  /* restore original 'allowhook' */
     592           0 :   L->nny = 0;  /* should be zero to be yieldable */
     593           0 :   luaD_shrinkstack(L);
     594           0 :   L->errfunc = ci->u.c.old_errfunc;
     595           0 :   return 1;  /* continue running the coroutine */
     596             : }
     597             : 
     598             : 
     599             : /*
     600             : ** Signal an error in the call to 'lua_resume', not in the execution
     601             : ** of the coroutine itself. (Such errors should not be handled by any
     602             : ** coroutine error handler and should not kill the coroutine.)
     603             : */
     604           0 : static int resume_error (lua_State *L, const char *msg, int narg) {
     605           0 :   L->top -= narg;  /* remove args from the stack */
     606           0 :   setsvalue2s(L, L->top, luaS_new(L, msg));  /* push error message */
     607           0 :   api_incr_top(L);
     608             :   lua_unlock(L);
     609           0 :   return LUA_ERRRUN;
     610             : }
     611             : 
     612             : 
     613             : /*
     614             : ** Do the work for 'lua_resume' in protected mode. Most of the work
     615             : ** depends on the status of the coroutine: initial state, suspended
     616             : ** inside a hook, or regularly suspended (optionally with a continuation
     617             : ** function), plus erroneous cases: non-suspended coroutine or dead
     618             : ** coroutine.
     619             : */
     620           0 : static void resume (lua_State *L, void *ud) {
     621           0 :   int n = *(cast(int*, ud));  /* number of arguments */
     622           0 :   StkId firstArg = L->top - n;  /* first argument */
     623           0 :   CallInfo *ci = L->ci;
     624           0 :   if (L->status == LUA_OK) {  /* starting a coroutine? */
     625           0 :     if (!luaD_precall(L, firstArg - 1, LUA_MULTRET))  /* Lua function? */
     626           0 :       luaV_execute(L);  /* call it */
     627             :   }
     628             :   else {  /* resuming from previous yield */
     629             :     lua_assert(L->status == LUA_YIELD);
     630           0 :     L->status = LUA_OK;  /* mark that it is running (again) */
     631           0 :     ci->func = restorestack(L, ci->extra);
     632           0 :     if (isLua(ci))  /* yielded inside a hook? */
     633           0 :       luaV_execute(L);  /* just continue running Lua code */
     634             :     else {  /* 'common' yield */
     635           0 :       if (ci->u.c.k != NULL) {  /* does it have a continuation function? */
     636             :         lua_unlock(L);
     637           0 :         n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */
     638             :         lua_lock(L);
     639             :         api_checknelems(L, n);
     640           0 :         firstArg = L->top - n;  /* yield results come from continuation */
     641             :       }
     642           0 :       luaD_poscall(L, ci, firstArg, n);  /* finish 'luaD_precall' */
     643             :     }
     644           0 :     unroll(L, NULL);  /* run continuation */
     645             :   }
     646           0 : }
     647             : 
     648             : 
     649           0 : LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
     650             :   int status;
     651           0 :   unsigned short oldnny = L->nny;  /* save "number of non-yieldable" calls */
     652             :   lua_lock(L);
     653           0 :   if (L->status == LUA_OK) {  /* may be starting a coroutine */
     654           0 :     if (L->ci != &L->base_ci)  /* not in base level? */
     655           0 :       return resume_error(L, "cannot resume non-suspended coroutine", nargs);
     656             :   }
     657           0 :   else if (L->status != LUA_YIELD)
     658           0 :     return resume_error(L, "cannot resume dead coroutine", nargs);
     659           0 :   L->nCcalls = (from) ? from->nCcalls + 1 : 1;
     660           0 :   if (L->nCcalls >= LUAI_MAXCCALLS)
     661           0 :     return resume_error(L, "C stack overflow", nargs);
     662             :   luai_userstateresume(L, nargs);
     663           0 :   L->nny = 0;  /* allow yields */
     664             :   api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
     665           0 :   status = luaD_rawrunprotected(L, resume, &nargs);
     666           0 :   if (status == -1)  /* error calling 'lua_resume'? */
     667           0 :     status = LUA_ERRRUN;
     668             :   else {  /* continue running after recoverable errors */
     669           0 :     while (errorstatus(status) && recover(L, status)) {
     670             :       /* unroll continuation */
     671           0 :       status = luaD_rawrunprotected(L, unroll, &status);
     672             :     }
     673           0 :     if (errorstatus(status)) {  /* unrecoverable error? */
     674           0 :       L->status = cast_byte(status);  /* mark thread as 'dead' */
     675           0 :       seterrorobj(L, status, L->top);  /* push error message */
     676           0 :       L->ci->top = L->top;
     677             :     }
     678             :     else lua_assert(status == L->status);  /* normal end or yield */
     679             :   }
     680           0 :   L->nny = oldnny;  /* restore 'nny' */
     681           0 :   L->nCcalls--;
     682             :   lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0));
     683             :   lua_unlock(L);
     684           0 :   return status;
     685             : }
     686             : 
     687             : 
     688           0 : LUA_API int lua_isyieldable (lua_State *L) {
     689           0 :   return (L->nny == 0);
     690             : }
     691             : 
     692             : 
     693           0 : LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
     694             :                         lua_KFunction k) {
     695           0 :   CallInfo *ci = L->ci;
     696             :   luai_userstateyield(L, nresults);
     697             :   lua_lock(L);
     698             :   api_checknelems(L, nresults);
     699           0 :   if (L->nny > 0) {
     700           0 :     if (L != G(L)->mainthread)
     701           0 :       luaG_runerror(L, "attempt to yield across a C-call boundary");
     702             :     else
     703           0 :       luaG_runerror(L, "attempt to yield from outside a coroutine");
     704             :   }
     705           0 :   L->status = LUA_YIELD;
     706           0 :   ci->extra = savestack(L, ci->func);  /* save current 'func' */
     707           0 :   if (isLua(ci)) {  /* inside a hook? */
     708             :     api_check(L, k == NULL, "hooks cannot continue after yielding");
     709             :   }
     710             :   else {
     711           0 :     if ((ci->u.c.k = k) != NULL)  /* is there a continuation? */
     712           0 :       ci->u.c.ctx = ctx;  /* save context */
     713           0 :     ci->func = L->top - nresults - 1;  /* protect stack below results */
     714           0 :     luaD_throw(L, LUA_YIELD);
     715             :   }
     716             :   lua_assert(ci->callstatus & CIST_HOOKED);  /* must be inside a hook */
     717             :   lua_unlock(L);
     718           0 :   return 0;  /* return to 'luaD_hook' */
     719             : }
     720             : 
     721             : 
     722      118148 : int luaD_pcall (lua_State *L, Pfunc func, void *u,
     723             :                 ptrdiff_t old_top, ptrdiff_t ef) {
     724             :   int status;
     725      118148 :   CallInfo *old_ci = L->ci;
     726      118148 :   lu_byte old_allowhooks = L->allowhook;
     727      118148 :   unsigned short old_nny = L->nny;
     728      118148 :   ptrdiff_t old_errfunc = L->errfunc;
     729      118148 :   L->errfunc = ef;
     730      118148 :   status = luaD_rawrunprotected(L, func, u);
     731      118101 :   if (status != LUA_OK) {  /* an error occurred? */
     732          58 :     StkId oldtop = restorestack(L, old_top);
     733          58 :     luaF_close(L, oldtop);  /* close possible pending closures */
     734          58 :     seterrorobj(L, status, oldtop);
     735          58 :     L->ci = old_ci;
     736          58 :     L->allowhook = old_allowhooks;
     737          58 :     L->nny = old_nny;
     738          58 :     luaD_shrinkstack(L);
     739             :   }
     740      118101 :   L->errfunc = old_errfunc;
     741      118101 :   return status;
     742             : }
     743             : 
     744             : 
     745             : 
     746             : /*
     747             : ** Execute a protected parser.
     748             : */
     749             : struct SParser {  /* data to 'f_parser' */
     750             :   ZIO *z;
     751             :   Mbuffer buff;  /* dynamic structure used by the scanner */
     752             :   Dyndata dyd;  /* dynamic structures used by the parser */
     753             :   const char *mode;
     754             :   const char *name;
     755             : };
     756             : 
     757             : 
     758         120 : static void checkmode (lua_State *L, const char *mode, const char *x) {
     759         120 :   if (mode && strchr(mode, x[0]) == NULL) {
     760           0 :     luaO_pushfstring(L,
     761             :        "attempt to load a %s chunk (mode is '%s')", x, mode);
     762           0 :     luaD_throw(L, LUA_ERRSYNTAX);
     763             :   }
     764         120 : }
     765             : 
     766             : 
     767         120 : static void f_parser (lua_State *L, void *ud) {
     768             :   LClosure *cl;
     769         120 :   struct SParser *p = cast(struct SParser *, ud);
     770         120 :   int c = zgetc(p->z);  /* read first character */
     771             : #if LUA_SUPPORT_LOAD_BINARY
     772             :   // support loading pre-compiled luac
     773             :   if (c == LUA_SIGNATURE[0]) {
     774             :     checkmode(L, p->mode, "binary");
     775             :     cl = luaU_undump(L, p->z, p->name);
     776             :   }
     777             :   else
     778             : #endif
     779             :   {
     780         120 :     checkmode(L, p->mode, "text");
     781         120 :     cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
     782             :   }
     783             :   lua_assert(cl->nupvalues == cl->p->sizeupvalues);
     784         120 :   luaF_initupvals(L, cl);
     785         120 : }
     786             : 
     787             : 
     788         120 : int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
     789             :                                         const char *mode) {
     790             :   struct SParser p;
     791             :   int status;
     792         120 :   L->nny++;  /* cannot yield during parsing */
     793         120 :   p.z = z; p.name = name; p.mode = mode;
     794         120 :   p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
     795         120 :   p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
     796         120 :   p.dyd.label.arr = NULL; p.dyd.label.size = 0;
     797         120 :   luaZ_initbuffer(L, &p.buff);
     798         120 :   status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
     799         120 :   luaZ_freebuffer(L, &p.buff);
     800         120 :   luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
     801         120 :   luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
     802         120 :   luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);
     803         120 :   L->nny--;
     804         120 :   return status;
     805             : }
     806             : 
     807             : 

Generated by: LCOV version 1.14