LCOV - code coverage report
Current view: top level - external/lua/src - lapi.c (source / functions) Hit Total Coverage
Test: rapport Lines: 377 741 50.9 %
Date: 2021-12-10 16:22:55 Functions: 56 92 60.9 %
Branches: 123 388 31.7 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            : ** $Id: lapi.c $
       3                 :            : ** Lua API
       4                 :            : ** See Copyright Notice in lua.h
       5                 :            : */
       6                 :            : 
       7                 :            : #define lapi_c
       8                 :            : #define LUA_CORE
       9                 :            : 
      10                 :            : #include "lprefix.h"
      11                 :            : 
      12                 :            : 
      13                 :            : #include <limits.h>
      14                 :            : #include <stdarg.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 "lstate.h"
      27                 :            : #include "lstring.h"
      28                 :            : #include "ltable.h"
      29                 :            : #include "ltm.h"
      30                 :            : #include "lundump.h"
      31                 :            : #include "lvm.h"
      32                 :            : 
      33                 :            : 
      34                 :            : 
      35                 :            : const char lua_ident[] =
      36                 :            :   "$LuaVersion: " LUA_COPYRIGHT " $"
      37                 :            :   "$LuaAuthors: " LUA_AUTHORS " $";
      38                 :            : 
      39                 :            : 
      40                 :            : 
      41                 :            : /*
      42                 :            : ** Test for a valid index.
      43                 :            : ** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed.
      44                 :            : ** However, it covers the most common cases in a faster way.
      45                 :            : */
      46                 :            : #define isvalid(L, o)   (!ttisnil(o) || o != &G(L)->nilvalue)
      47                 :            : 
      48                 :            : 
      49                 :            : /* test for pseudo index */
      50                 :            : #define ispseudo(i)             ((i) <= LUA_REGISTRYINDEX)
      51                 :            : 
      52                 :            : /* test for upvalue */
      53                 :            : #define isupvalue(i)            ((i) < LUA_REGISTRYINDEX)
      54                 :            : 
      55                 :            : 
      56                 :     236546 : static TValue *index2value (lua_State *L, int idx) {
      57                 :     236546 :   CallInfo *ci = L->ci;
      58         [ +  + ]:     236546 :   if (idx > 0) {
      59                 :       2143 :     StkId o = ci->func + idx;
      60                 :       2143 :     api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index");
      61         [ +  + ]:       2143 :     if (o >= L->top) return &G(L)->nilvalue;
      62                 :       2137 :     else return s2v(o);
      63                 :            :   }
      64         [ +  + ]:     234403 :   else if (!ispseudo(idx)) {  /* negative index */
      65                 :     213714 :     api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
      66                 :     213714 :     return s2v(L->top + idx);
      67                 :            :   }
      68         [ +  - ]:      20689 :   else if (idx == LUA_REGISTRYINDEX)
      69                 :      20689 :     return &G(L)->l_registry;
      70                 :            :   else {  /* upvalues */
      71                 :          0 :     idx = LUA_REGISTRYINDEX - idx;
      72                 :          0 :     api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
      73         [ #  # ]:          0 :     if (ttislcf(s2v(ci->func)))  /* light C function? */
      74                 :          0 :       return &G(L)->nilvalue;  /* it has no upvalues */
      75                 :            :     else {
      76                 :          0 :       CClosure *func = clCvalue(s2v(ci->func));
      77         [ #  # ]:          0 :       return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue;
      78                 :            :     }
      79                 :            :   }
      80                 :     236546 : }
      81                 :            : 
      82                 :            : 
      83                 :       8106 : static StkId index2stack (lua_State *L, int idx) {
      84                 :       8106 :   CallInfo *ci = L->ci;
      85         [ +  + ]:       8106 :   if (idx > 0) {
      86                 :          6 :     StkId o = ci->func + idx;
      87                 :          6 :     api_check(L, o < L->top, "unacceptable index");
      88                 :          6 :     return o;
      89                 :            :   }
      90                 :            :   else {    /* non-positive index */
      91                 :       8100 :     api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
      92                 :       8100 :     api_check(L, !ispseudo(idx), "invalid index");
      93                 :       8100 :     return L->top + idx;
      94                 :            :   }
      95                 :       8106 : }
      96                 :            : 
      97                 :            : 
      98                 :      12881 : LUA_API int lua_checkstack (lua_State *L, int n) {
      99                 :            :   int res;
     100                 :            :   CallInfo *ci;
     101                 :            :   lua_lock(L);
     102                 :      12881 :   ci = L->ci;
     103                 :      12881 :   api_check(L, n >= 0, "negative 'n'");
     104         [ +  - ]:      12881 :   if (L->stack_last - L->top > n)  /* stack large enough? */
     105                 :      12881 :     res = 1;  /* yes; check is OK */
     106                 :            :   else {  /* no; need to grow stack */
     107                 :          0 :     int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
     108         [ #  # ]:          0 :     if (inuse > LUAI_MAXSTACK - n)  /* can grow without overflow? */
     109                 :          0 :       res = 0;  /* no */
     110                 :            :     else  /* try to grow stack */
     111                 :          0 :       res = luaD_growstack(L, n, 0);
     112                 :            :   }
     113   [ +  -  +  - ]:      12881 :   if (res && ci->top < L->top + n)
     114                 :          0 :     ci->top = L->top + n;  /* adjust frame top */
     115                 :            :   lua_unlock(L);
     116                 :      12881 :   return res;
     117                 :            : }
     118                 :            : 
     119                 :            : 
     120                 :          0 : LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
     121                 :            :   int i;
     122         [ #  # ]:          0 :   if (from == to) return;
     123                 :            :   lua_lock(to);
     124                 :          0 :   api_checknelems(from, n);
     125                 :          0 :   api_check(from, G(from) == G(to), "moving among independent states");
     126                 :          0 :   api_check(from, to->ci->top - to->top >= n, "stack overflow");
     127                 :          0 :   from->top -= n;
     128         [ #  # ]:          0 :   for (i = 0; i < n; i++) {
     129                 :          0 :     setobjs2s(to, to->top, from->top + i);
     130                 :          0 :     to->top++;  /* stack already checked by previous 'api_check' */
     131                 :          0 :   }
     132                 :            :   lua_unlock(to);
     133                 :          0 : }
     134                 :            : 
     135                 :            : 
     136                 :        810 : LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
     137                 :            :   lua_CFunction old;
     138                 :            :   lua_lock(L);
     139                 :        810 :   old = G(L)->panic;
     140                 :        810 :   G(L)->panic = panicf;
     141                 :            :   lua_unlock(L);
     142                 :        810 :   return old;
     143                 :            : }
     144                 :            : 
     145                 :            : 
     146                 :       8021 : LUA_API lua_Number lua_version (lua_State *L) {
     147                 :       8021 :   UNUSED(L);
     148                 :       8021 :   return LUA_VERSION_NUM;
     149                 :            : }
     150                 :            : 
     151                 :            : 
     152                 :            : 
     153                 :            : /*
     154                 :            : ** basic stack manipulation
     155                 :            : */
     156                 :            : 
     157                 :            : 
     158                 :            : /*
     159                 :            : ** convert an acceptable stack index into an absolute index
     160                 :            : */
     161                 :       2556 : LUA_API int lua_absindex (lua_State *L, int idx) {
     162   [ +  +  +  - ]:       2556 :   return (idx > 0 || ispseudo(idx))
     163                 :       2556 :          ? idx
     164                 :          0 :          : cast_int(L->top - L->ci->func) + idx;
     165                 :            : }
     166                 :            : 
     167                 :            : 
     168                 :        718 : LUA_API int lua_gettop (lua_State *L) {
     169                 :        718 :   return cast_int(L->top - (L->ci->func + 1));
     170                 :            : }
     171                 :            : 
     172                 :            : 
     173                 :      46771 : LUA_API void lua_settop (lua_State *L, int idx) {
     174                 :            :   CallInfo *ci;
     175                 :            :   StkId func;
     176                 :            :   ptrdiff_t diff;  /* difference for new top */
     177                 :            :   lua_lock(L);
     178                 :      46771 :   ci = L->ci;
     179                 :      46771 :   func = ci->func;
     180         [ +  + ]:      46771 :   if (idx >= 0) {
     181                 :         43 :     api_check(L, idx <= ci->top - (func + 1), "new top too large");
     182                 :         43 :     diff = ((func + 1) + idx) - L->top;
     183         [ -  + ]:         43 :     for (; diff > 0; diff--)
     184                 :          0 :       setnilvalue(s2v(L->top++));  /* clear new slots */
     185                 :         43 :   }
     186                 :            :   else {
     187                 :      46728 :     api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
     188                 :      46728 :     diff = idx + 1;  /* will "subtract" index (as it is negative) */
     189                 :            :   }
     190   [ +  +  +  - ]:      46771 :   if (diff < 0 && hastocloseCfunc(ci->nresults))
     191                 :          0 :     luaF_close(L, L->top + diff, LUA_OK);
     192                 :      46771 :   L->top += diff;  /* correct top only after closing any upvalue */
     193                 :            :   lua_unlock(L);
     194                 :      46771 : }
     195                 :            : 
     196                 :            : 
     197                 :            : /*
     198                 :            : ** Reverse the stack segment from 'from' to 'to'
     199                 :            : ** (auxiliary to 'lua_rotate')
     200                 :            : ** Note that we move(copy) only the value inside the stack.
     201                 :            : ** (We do not move additional fields that may exist.)
     202                 :            : */
     203                 :      24318 : static void reverse (lua_State *L, StkId from, StkId to) {
     204         [ +  + ]:      32430 :   for (; from < to; from++, to--) {
     205                 :            :     TValue temp;
     206                 :       8112 :     setobj(L, &temp, s2v(from));
     207                 :       8112 :     setobjs2s(L, from, to);
     208                 :       8112 :     setobj2s(L, to, &temp);
     209                 :       8112 :   }
     210                 :      24318 : }
     211                 :            : 
     212                 :            : 
     213                 :            : /*
     214                 :            : ** Let x = AB, where A is a prefix of length 'n'. Then,
     215                 :            : ** rotate x n == BA. But BA == (A^r . B^r)^r.
     216                 :            : */
     217                 :       8106 : LUA_API void lua_rotate (lua_State *L, int idx, int n) {
     218                 :            :   StkId p, t, m;
     219                 :            :   lua_lock(L);
     220                 :       8106 :   t = L->top - 1;  /* end of stack segment being rotated */
     221                 :       8106 :   p = index2stack(L, idx);  /* start of segment */
     222                 :       8106 :   api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
     223         [ -  + ]:       8106 :   m = (n >= 0 ? t - n : p - n - 1);  /* end of prefix */
     224                 :       8106 :   reverse(L, p, m);  /* reverse the prefix with length 'n' */
     225                 :       8106 :   reverse(L, m + 1, t);  /* reverse the suffix */
     226                 :       8106 :   reverse(L, p, t);  /* reverse the entire segment */
     227                 :            :   lua_unlock(L);
     228                 :       8106 : }
     229                 :            : 
     230                 :            : 
     231                 :          0 : LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
     232                 :            :   TValue *fr, *to;
     233                 :            :   lua_lock(L);
     234                 :          0 :   fr = index2value(L, fromidx);
     235                 :          0 :   to = index2value(L, toidx);
     236                 :          0 :   api_check(L, isvalid(L, to), "invalid index");
     237                 :          0 :   setobj(L, to, fr);
     238         [ #  # ]:          0 :   if (isupvalue(toidx))  /* function upvalue? */
     239   [ #  #  #  #  :          0 :     luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr);
                   #  # ]
     240                 :            :   /* LUA_REGISTRYINDEX does not need gc barrier
     241                 :            :      (collector revisits it before finishing collection) */
     242                 :            :   lua_unlock(L);
     243                 :          0 : }
     244                 :            : 
     245                 :            : 
     246                 :      30051 : LUA_API void lua_pushvalue (lua_State *L, int idx) {
     247                 :            :   lua_lock(L);
     248                 :      30051 :   setobj2s(L, L->top, index2value(L, idx));
     249                 :      30051 :   api_incr_top(L);
     250                 :            :   lua_unlock(L);
     251                 :      30051 : }
     252                 :            : 
     253                 :            : 
     254                 :            : 
     255                 :            : /*
     256                 :            : ** access functions (stack -> C)
     257                 :            : */
     258                 :            : 
     259                 :            : 
     260                 :        269 : LUA_API int lua_type (lua_State *L, int idx) {
     261                 :        269 :   const TValue *o = index2value(L, idx);
     262   [ +  +  +  + ]:        269 :   return (isvalid(L, o) ? ttype(o) : LUA_TNONE);
     263                 :            : }
     264                 :            : 
     265                 :            : 
     266                 :         35 : LUA_API const char *lua_typename (lua_State *L, int t) {
     267                 :         35 :   UNUSED(L);
     268                 :         35 :   api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type");
     269                 :         35 :   return ttypename(t);
     270                 :            : }
     271                 :            : 
     272                 :            : 
     273                 :          0 : LUA_API int lua_iscfunction (lua_State *L, int idx) {
     274                 :          0 :   const TValue *o = index2value(L, idx);
     275         [ #  # ]:          0 :   return (ttislcf(o) || (ttisCclosure(o)));
     276                 :            : }
     277                 :            : 
     278                 :            : 
     279                 :         44 : LUA_API int lua_isinteger (lua_State *L, int idx) {
     280                 :         44 :   const TValue *o = index2value(L, idx);
     281                 :         44 :   return ttisinteger(o);
     282                 :            : }
     283                 :            : 
     284                 :            : 
     285                 :          0 : LUA_API int lua_isnumber (lua_State *L, int idx) {
     286                 :            :   lua_Number n;
     287                 :          0 :   const TValue *o = index2value(L, idx);
     288         [ #  # ]:          0 :   return tonumber(o, &n);
     289                 :            : }
     290                 :            : 
     291                 :            : 
     292                 :          0 : LUA_API int lua_isstring (lua_State *L, int idx) {
     293                 :          0 :   const TValue *o = index2value(L, idx);
     294         [ #  # ]:          0 :   return (ttisstring(o) || cvt2str(o));
     295                 :            : }
     296                 :            : 
     297                 :            : 
     298                 :          0 : LUA_API int lua_isuserdata (lua_State *L, int idx) {
     299                 :          0 :   const TValue *o = index2value(L, idx);
     300         [ #  # ]:          0 :   return (ttisfulluserdata(o) || ttislightuserdata(o));
     301                 :            : }
     302                 :            : 
     303                 :            : 
     304                 :        420 : LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
     305                 :        420 :   const TValue *o1 = index2value(L, index1);
     306                 :        420 :   const TValue *o2 = index2value(L, index2);
     307   [ -  +  #  #  :        420 :   return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0;
                   -  + ]
     308                 :            : }
     309                 :            : 
     310                 :            : 
     311                 :          0 : LUA_API void lua_arith (lua_State *L, int op) {
     312                 :            :   lua_lock(L);
     313   [ #  #  #  # ]:          0 :   if (op != LUA_OPUNM && op != LUA_OPBNOT)
     314                 :          0 :     api_checknelems(L, 2);  /* all other operations expect two operands */
     315                 :            :   else {  /* for unary operations, add fake 2nd operand */
     316                 :          0 :     api_checknelems(L, 1);
     317                 :          0 :     setobjs2s(L, L->top, L->top - 1);
     318                 :          0 :     api_incr_top(L);
     319                 :            :   }
     320                 :            :   /* first operand at top - 2, second at top - 1; result go to top - 2 */
     321                 :          0 :   luaO_arith(L, op, s2v(L->top - 2), s2v(L->top - 1), L->top - 2);
     322                 :          0 :   L->top--;  /* remove second operand */
     323                 :            :   lua_unlock(L);
     324                 :          0 : }
     325                 :            : 
     326                 :            : 
     327                 :          0 : LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
     328                 :            :   const TValue *o1;
     329                 :            :   const TValue *o2;
     330                 :          0 :   int i = 0;
     331                 :            :   lua_lock(L);  /* may call tag method */
     332                 :          0 :   o1 = index2value(L, index1);
     333                 :          0 :   o2 = index2value(L, index2);
     334   [ #  #  #  #  :          0 :   if (isvalid(L, o1) && isvalid(L, o2)) {
                   #  # ]
     335   [ #  #  #  # ]:          0 :     switch (op) {
     336                 :          0 :       case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
     337                 :          0 :       case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
     338                 :          0 :       case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
     339                 :          0 :       default: api_check(L, 0, "invalid option");
     340                 :          0 :     }
     341                 :          0 :   }
     342                 :            :   lua_unlock(L);
     343                 :          0 :   return i;
     344                 :            : }
     345                 :            : 
     346                 :            : 
     347                 :          0 : LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
     348                 :          0 :   size_t sz = luaO_str2num(s, s2v(L->top));
     349         [ #  # ]:          0 :   if (sz != 0)
     350                 :          0 :     api_incr_top(L);
     351                 :          0 :   return sz;
     352                 :            : }
     353                 :            : 
     354                 :            : 
     355                 :        661 : LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
     356                 :        661 :   lua_Number n = 0;
     357                 :        661 :   const TValue *o = index2value(L, idx);
     358         [ -  + ]:        661 :   int isnum = tonumber(o, &n);
     359         [ +  - ]:        661 :   if (pisnum)
     360                 :          0 :     *pisnum = isnum;
     361                 :        661 :   return n;
     362                 :            : }
     363                 :            : 
     364                 :            : 
     365                 :        291 : LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
     366                 :        291 :   lua_Integer res = 0;
     367                 :        291 :   const TValue *o = index2value(L, idx);
     368         [ +  + ]:        291 :   int isnum = tointeger(o, &res);
     369         [ +  + ]:        291 :   if (pisnum)
     370                 :         40 :     *pisnum = isnum;
     371                 :        291 :   return res;
     372                 :            : }
     373                 :            : 
     374                 :            : 
     375                 :       8106 : LUA_API int lua_toboolean (lua_State *L, int idx) {
     376                 :       8106 :   const TValue *o = index2value(L, idx);
     377         [ -  + ]:       8106 :   return !l_isfalse(o);
     378                 :            : }
     379                 :            : 
     380                 :            : 
     381                 :        983 : LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
     382                 :            :   TValue *o;
     383                 :            :   lua_lock(L);
     384                 :        983 :   o = index2value(L, idx);
     385         [ +  + ]:        983 :   if (!ttisstring(o)) {
     386         [ +  + ]:         97 :     if (!cvt2str(o)) {  /* not convertible? */
     387         [ -  + ]:          3 :       if (len != NULL) *len = 0;
     388                 :            :       lua_unlock(L);
     389                 :          3 :       return NULL;
     390                 :            :     }
     391                 :         94 :     luaO_tostring(L, o);
     392         [ +  + ]:         94 :     luaC_checkGC(L);
     393                 :         94 :     o = index2value(L, idx);  /* previous call may reallocate the stack */
     394                 :         94 :   }
     395         [ +  + ]:        980 :   if (len != NULL)
     396         [ +  + ]:        128 :     *len = vslen(o);
     397                 :            :   lua_unlock(L);
     398                 :        980 :   return svalue(o);
     399                 :        983 : }
     400                 :            : 
     401                 :            : 
     402                 :          9 : LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
     403                 :          9 :   const TValue *o = index2value(L, idx);
     404   [ -  -  -  -  :          9 :   switch (ttypetag(o)) {
                      + ]
     405                 :          0 :     case LUA_VSHRSTR: return tsvalue(o)->shrlen;
     406                 :          0 :     case LUA_VLNGSTR: return tsvalue(o)->u.lnglen;
     407                 :          0 :     case LUA_VUSERDATA: return uvalue(o)->len;
     408                 :          9 :     case LUA_VTABLE: return luaH_getn(hvalue(o));
     409                 :          0 :     default: return 0;
     410                 :            :   }
     411                 :          9 : }
     412                 :            : 
     413                 :            : 
     414                 :          0 : LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
     415                 :          0 :   const TValue *o = index2value(L, idx);
     416         [ #  # ]:          0 :   if (ttislcf(o)) return fvalue(o);
     417         [ #  # ]:          0 :   else if (ttisCclosure(o))
     418                 :          0 :     return clCvalue(o)->f;
     419                 :          0 :   else return NULL;  /* not a C function */
     420                 :          0 : }
     421                 :            : 
     422                 :            : 
     423                 :        492 : static void *touserdata (const TValue *o) {
     424      [ +  +  + ]:        492 :   switch (ttype(o)) {
     425         [ +  + ]:        420 :     case LUA_TUSERDATA: return getudatamem(uvalue(o));
     426                 :         69 :     case LUA_TLIGHTUSERDATA: return pvalue(o);
     427                 :          3 :     default: return NULL;
     428                 :            :   }
     429                 :        492 : }
     430                 :            : 
     431                 :            : 
     432                 :        492 : LUA_API void *lua_touserdata (lua_State *L, int idx) {
     433                 :        492 :   const TValue *o = index2value(L, idx);
     434                 :        492 :   return touserdata(o);
     435                 :            : }
     436                 :            : 
     437                 :            : 
     438                 :          0 : LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
     439                 :          0 :   const TValue *o = index2value(L, idx);
     440         [ #  # ]:          0 :   return (!ttisthread(o)) ? NULL : thvalue(o);
     441                 :            : }
     442                 :            : 
     443                 :            : 
     444                 :            : /*
     445                 :            : ** Returns a pointer to the internal representation of an object.
     446                 :            : ** Note that ANSI C does not allow the conversion of a pointer to
     447                 :            : ** function to a 'void*', so the conversion here goes through
     448                 :            : ** a 'size_t'. (As the returned pointer is only informative, this
     449                 :            : ** conversion should not be a problem.)
     450                 :            : */
     451                 :          0 : LUA_API const void *lua_topointer (lua_State *L, int idx) {
     452                 :          0 :   const TValue *o = index2value(L, idx);
     453      [ #  #  # ]:          0 :   switch (ttypetag(o)) {
     454                 :          0 :     case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
     455                 :            :     case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA:
     456                 :          0 :       return touserdata(o);
     457                 :            :     default: {
     458         [ #  # ]:          0 :       if (iscollectable(o))
     459                 :          0 :         return gcvalue(o);
     460                 :            :       else
     461                 :          0 :         return NULL;
     462                 :            :     }
     463                 :            :   }
     464                 :          0 : }
     465                 :            : 
     466                 :            : 
     467                 :            : 
     468                 :            : /*
     469                 :            : ** push functions (C -> stack)
     470                 :            : */
     471                 :            : 
     472                 :            : 
     473                 :        241 : LUA_API void lua_pushnil (lua_State *L) {
     474                 :            :   lua_lock(L);
     475                 :        241 :   setnilvalue(s2v(L->top));
     476                 :        241 :   api_incr_top(L);
     477                 :            :   lua_unlock(L);
     478                 :        241 : }
     479                 :            : 
     480                 :            : 
     481                 :       1620 : LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
     482                 :            :   lua_lock(L);
     483                 :       1620 :   setfltvalue(s2v(L->top), n);
     484                 :       1620 :   api_incr_top(L);
     485                 :            :   lua_unlock(L);
     486                 :       1620 : }
     487                 :            : 
     488                 :            : 
     489                 :       4345 : LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
     490                 :            :   lua_lock(L);
     491                 :       4345 :   setivalue(s2v(L->top), n);
     492                 :       4345 :   api_incr_top(L);
     493                 :            :   lua_unlock(L);
     494                 :       4345 : }
     495                 :            : 
     496                 :            : 
     497                 :            : /*
     498                 :            : ** Pushes on the stack a string with given length. Avoid using 's' when
     499                 :            : ** 'len' == 0 (as 's' can be NULL in that case), due to later use of
     500                 :            : ** 'memcmp' and 'memcpy'.
     501                 :            : */
     502                 :        810 : LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
     503                 :            :   TString *ts;
     504                 :            :   lua_lock(L);
     505         [ -  + ]:        810 :   ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
     506                 :        810 :   setsvalue2s(L, L->top, ts);
     507                 :        810 :   api_incr_top(L);
     508         [ +  - ]:        810 :   luaC_checkGC(L);
     509                 :            :   lua_unlock(L);
     510                 :        810 :   return getstr(ts);
     511                 :            : }
     512                 :            : 
     513                 :            : 
     514                 :      13443 : LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
     515                 :            :   lua_lock(L);
     516         [ -  + ]:      13443 :   if (s == NULL)
     517                 :          0 :     setnilvalue(s2v(L->top));
     518                 :            :   else {
     519                 :            :     TString *ts;
     520                 :      13443 :     ts = luaS_new(L, s);
     521                 :      13443 :     setsvalue2s(L, L->top, ts);
     522                 :      13443 :     s = getstr(ts);  /* internal copy's address */
     523                 :            :   }
     524                 :      13443 :   api_incr_top(L);
     525         [ +  + ]:      13443 :   luaC_checkGC(L);
     526                 :            :   lua_unlock(L);
     527                 :      13443 :   return s;
     528                 :            : }
     529                 :            : 
     530                 :            : 
     531                 :        276 : LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
     532                 :            :                                       va_list argp) {
     533                 :            :   const char *ret;
     534                 :            :   lua_lock(L);
     535                 :        276 :   ret = luaO_pushvfstring(L, fmt, argp);
     536         [ +  + ]:        276 :   luaC_checkGC(L);
     537                 :            :   lua_unlock(L);
     538                 :        276 :   return ret;
     539                 :            : }
     540                 :            : 
     541                 :            : 
     542                 :       2075 : LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
     543                 :            :   const char *ret;
     544                 :            :   va_list argp;
     545                 :            :   lua_lock(L);
     546                 :       2075 :   va_start(argp, fmt);
     547                 :       2075 :   ret = luaO_pushvfstring(L, fmt, argp);
     548                 :       2075 :   va_end(argp);
     549         [ +  + ]:       2075 :   luaC_checkGC(L);
     550                 :            :   lua_unlock(L);
     551                 :       2075 :   return ret;
     552                 :            : }
     553                 :            : 
     554                 :            : 
     555                 :     132506 : LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
     556                 :            :   lua_lock(L);
     557         [ +  + ]:     132506 :   if (n == 0) {
     558                 :     126836 :     setfvalue(s2v(L->top), fn);
     559                 :     126836 :     api_incr_top(L);
     560                 :     126836 :   }
     561                 :            :   else {
     562                 :            :     CClosure *cl;
     563                 :       5670 :     api_checknelems(L, n);
     564                 :       5670 :     api_check(L, n <= MAXUPVAL, "upvalue index too large");
     565                 :       5670 :     cl = luaF_newCclosure(L, n);
     566                 :       5670 :     cl->f = fn;
     567                 :       5670 :     L->top -= n;
     568         [ +  + ]:      11340 :     while (n--) {
     569                 :       5670 :       setobj2n(L, &cl->upvalue[n], s2v(L->top + n));
     570                 :            :       /* does not need barrier because closure is white */
     571                 :            :       lua_assert(iswhite(cl));
     572                 :            :     }
     573                 :       5670 :     setclCvalue(L, s2v(L->top), cl);
     574                 :       5670 :     api_incr_top(L);
     575         [ +  - ]:       5670 :     luaC_checkGC(L);
     576                 :            :   }
     577                 :            :   lua_unlock(L);
     578                 :     132506 : }
     579                 :            : 
     580                 :            : 
     581                 :      13013 : LUA_API void lua_pushboolean (lua_State *L, int b) {
     582                 :            :   lua_lock(L);
     583         [ +  + ]:      13013 :   if (b)
     584                 :         45 :     setbtvalue(s2v(L->top));
     585                 :            :   else
     586                 :      12968 :     setbfvalue(s2v(L->top));
     587                 :      13013 :   api_incr_top(L);
     588                 :            :   lua_unlock(L);
     589                 :      13013 : }
     590                 :            : 
     591                 :            : 
     592                 :        104 : LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
     593                 :            :   lua_lock(L);
     594                 :        104 :   setpvalue(s2v(L->top), p);
     595                 :        104 :   api_incr_top(L);
     596                 :            :   lua_unlock(L);
     597                 :        104 : }
     598                 :            : 
     599                 :            : 
     600                 :          0 : LUA_API int lua_pushthread (lua_State *L) {
     601                 :            :   lua_lock(L);
     602                 :          0 :   setthvalue(L, s2v(L->top), L);
     603                 :          0 :   api_incr_top(L);
     604                 :            :   lua_unlock(L);
     605                 :          0 :   return (G(L)->mainthread == L);
     606                 :            : }
     607                 :            : 
     608                 :            : 
     609                 :            : 
     610                 :            : /*
     611                 :            : ** get functions (Lua -> stack)
     612                 :            : */
     613                 :            : 
     614                 :            : 
     615                 :      24205 : static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
     616                 :            :   const TValue *slot;
     617                 :      24205 :   TString *str = luaS_new(L, k);
     618   [ -  +  +  + ]:      24205 :   if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
     619                 :      35550 :     setobj2s(L, L->top, slot);
     620                 :      35550 :     api_incr_top(L);
     621                 :      35550 :   }
     622                 :            :   else {
     623                 :      11345 :     setsvalue2s(L, L->top, str);
     624                 :      11345 :     api_incr_top(L);
     625                 :      11345 :     luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot);
     626                 :            :   }
     627                 :            :   lua_unlock(L);
     628                 :      46895 :   return ttype(s2v(L->top - 1));
     629                 :            : }
     630                 :            : 
     631                 :            : 
     632                 :       1896 : LUA_API int lua_getglobal (lua_State *L, const char *name) {
     633                 :            :   Table *reg;
     634                 :            :   lua_lock(L);
     635                 :       1896 :   reg = hvalue(&G(L)->l_registry);
     636                 :       1896 :   return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
     637                 :            : }
     638                 :            : 
     639                 :            : 
     640                 :          0 : LUA_API int lua_gettable (lua_State *L, int idx) {
     641                 :            :   const TValue *slot;
     642                 :            :   TValue *t;
     643                 :            :   lua_lock(L);
     644                 :          0 :   t = index2value(L, idx);
     645   [ #  #  #  # ]:          0 :   if (luaV_fastget(L, t, s2v(L->top - 1), slot, luaH_get)) {
     646                 :          0 :     setobj2s(L, L->top - 1, slot);
     647                 :          0 :   }
     648                 :            :   else
     649                 :          0 :     luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot);
     650                 :            :   lua_unlock(L);
     651                 :          0 :   return ttype(s2v(L->top - 1));
     652                 :            : }
     653                 :            : 
     654                 :            : 
     655                 :      22309 : LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
     656                 :            :   lua_lock(L);
     657                 :      22309 :   return auxgetstr(L, index2value(L, idx), k);
     658                 :            : }
     659                 :            : 
     660                 :            : 
     661                 :          0 : LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
     662                 :            :   TValue *t;
     663                 :            :   const TValue *slot;
     664                 :            :   lua_lock(L);
     665                 :          0 :   t = index2value(L, idx);
     666   [ #  #  #  #  :          0 :   if (luaV_fastgeti(L, t, n, slot)) {
                   #  # ]
     667                 :          0 :     setobj2s(L, L->top, slot);
     668                 :          0 :   }
     669                 :            :   else {
     670                 :            :     TValue aux;
     671                 :          0 :     setivalue(&aux, n);
     672                 :          0 :     luaV_finishget(L, t, &aux, L->top, slot);
     673                 :            :   }
     674                 :          0 :   api_incr_top(L);
     675                 :            :   lua_unlock(L);
     676                 :          0 :   return ttype(s2v(L->top - 1));
     677                 :            : }
     678                 :            : 
     679                 :            : 
     680                 :       1687 : static int finishrawget (lua_State *L, const TValue *val) {
     681         [ +  + ]:       1687 :   if (isempty(val))  /* avoid copying empty items to the stack */
     682                 :         51 :     setnilvalue(s2v(L->top));
     683                 :            :   else
     684                 :       1636 :     setobj2s(L, L->top, val);
     685                 :       1687 :   api_incr_top(L);
     686                 :            :   lua_unlock(L);
     687                 :       1687 :   return ttype(s2v(L->top - 1));
     688                 :            : }
     689                 :            : 
     690                 :            : 
     691                 :       4995 : static Table *gettable (lua_State *L, int idx) {
     692                 :       4995 :   TValue *t = index2value(L, idx);
     693                 :       4995 :   api_check(L, ttistable(t), "table expected");
     694                 :       4995 :   return hvalue(t);
     695                 :            : }
     696                 :            : 
     697                 :            : 
     698                 :         51 : LUA_API int lua_rawget (lua_State *L, int idx) {
     699                 :            :   Table *t;
     700                 :            :   const TValue *val;
     701                 :            :   lua_lock(L);
     702                 :         51 :   api_checknelems(L, 1);
     703                 :         51 :   t = gettable(L, idx);
     704                 :         51 :   val = luaH_get(t, s2v(L->top - 1));
     705                 :         51 :   L->top--;  /* remove key */
     706                 :         51 :   return finishrawget(L, val);
     707                 :            : }
     708                 :            : 
     709                 :            : 
     710                 :       1636 : LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
     711                 :            :   Table *t;
     712                 :            :   lua_lock(L);
     713                 :       1636 :   t = gettable(L, idx);
     714                 :       1636 :   return finishrawget(L, luaH_getint(t, n));
     715                 :            : }
     716                 :            : 
     717                 :            : 
     718                 :          0 : LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
     719                 :            :   Table *t;
     720                 :            :   TValue k;
     721                 :            :   lua_lock(L);
     722                 :          0 :   t = gettable(L, idx);
     723                 :          0 :   setpvalue(&k, cast_voidp(p));
     724                 :          0 :   return finishrawget(L, luaH_get(t, &k));
     725                 :            : }
     726                 :            : 
     727                 :            : 
     728                 :      14634 : LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
     729                 :            :   Table *t;
     730                 :            :   lua_lock(L);
     731                 :      14634 :   t = luaH_new(L);
     732                 :      14634 :   sethvalue2s(L, L->top, t);
     733                 :      14634 :   api_incr_top(L);
     734   [ +  +  +  + ]:      14634 :   if (narray > 0 || nrec > 0)
     735                 :      12111 :     luaH_resize(L, t, narray, nrec);
     736         [ +  + ]:      14634 :   luaC_checkGC(L);
     737                 :            :   lua_unlock(L);
     738                 :      14634 : }
     739                 :            : 
     740                 :            : 
     741                 :        543 : LUA_API int lua_getmetatable (lua_State *L, int objindex) {
     742                 :            :   const TValue *obj;
     743                 :            :   Table *mt;
     744                 :        543 :   int res = 0;
     745                 :            :   lua_lock(L);
     746                 :        543 :   obj = index2value(L, objindex);
     747      [ +  -  + ]:        543 :   switch (ttype(obj)) {
     748                 :            :     case LUA_TTABLE:
     749                 :          0 :       mt = hvalue(obj)->metatable;
     750                 :          0 :       break;
     751                 :            :     case LUA_TUSERDATA:
     752                 :        420 :       mt = uvalue(obj)->metatable;
     753                 :        420 :       break;
     754                 :            :     default:
     755                 :        123 :       mt = G(L)->mt[ttype(obj)];
     756                 :        123 :       break;
     757                 :            :   }
     758         [ +  + ]:        543 :   if (mt != NULL) {
     759                 :        471 :     sethvalue2s(L, L->top, mt);
     760                 :        471 :     api_incr_top(L);
     761                 :        471 :     res = 1;
     762                 :        471 :   }
     763                 :            :   lua_unlock(L);
     764                 :        543 :   return res;
     765                 :            : }
     766                 :            : 
     767                 :            : 
     768                 :          0 : LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
     769                 :            :   TValue *o;
     770                 :            :   int t;
     771                 :            :   lua_lock(L);
     772                 :          0 :   o = index2value(L, idx);
     773                 :          0 :   api_check(L, ttisfulluserdata(o), "full userdata expected");
     774   [ #  #  #  # ]:          0 :   if (n <= 0 || n > uvalue(o)->nuvalue) {
     775                 :          0 :     setnilvalue(s2v(L->top));
     776                 :          0 :     t = LUA_TNONE;
     777                 :          0 :   }
     778                 :            :   else {
     779                 :          0 :     setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv);
     780                 :          0 :     t = ttype(s2v(L->top));
     781                 :            :   }
     782                 :          0 :   api_incr_top(L);
     783                 :            :   lua_unlock(L);
     784                 :          0 :   return t;
     785                 :            : }
     786                 :            : 
     787                 :            : 
     788                 :            : /*
     789                 :            : ** set functions (stack -> Lua)
     790                 :            : */
     791                 :            : 
     792                 :            : /*
     793                 :            : ** t[k] = value at the top of the stack (where 'k' is a string)
     794                 :            : */
     795                 :     137611 : static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
     796                 :            :   const TValue *slot;
     797                 :     137611 :   TString *str = luaS_new(L, k);
     798                 :     137611 :   api_checknelems(L, 1);
     799   [ -  +  +  + ]:     137611 :   if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
     800   [ +  +  -  +  :     292550 :     luaV_finishfastset(L, t, slot, s2v(L->top - 1));
                   #  # ]
     801                 :      17328 :     L->top--;  /* pop value */
     802                 :      17328 :   }
     803                 :            :   else {
     804                 :     154939 :     setsvalue2s(L, L->top, str);  /* push 'str' (to make it a TValue) */
     805                 :     154939 :     api_incr_top(L);
     806                 :     154939 :     luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot);
     807                 :     154939 :     L->top -= 2;  /* pop value and key */
     808                 :            :   }
     809                 :            :   lua_unlock(L);  /* lock done by caller */
     810                 :     172267 : }
     811                 :            : 
     812                 :            : 
     813                 :       9667 : LUA_API void lua_setglobal (lua_State *L, const char *name) {
     814                 :            :   Table *reg;
     815                 :            :   lua_lock(L);  /* unlock done in 'auxsetstr' */
     816                 :       9667 :   reg = hvalue(&G(L)->l_registry);
     817                 :       9667 :   auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
     818                 :       9667 : }
     819                 :            : 
     820                 :            : 
     821                 :        150 : LUA_API void lua_settable (lua_State *L, int idx) {
     822                 :            :   TValue *t;
     823                 :            :   const TValue *slot;
     824                 :            :   lua_lock(L);
     825                 :        150 :   api_checknelems(L, 2);
     826                 :        150 :   t = index2value(L, idx);
     827   [ -  +  +  + ]:        150 :   if (luaV_fastget(L, t, s2v(L->top - 2), slot, luaH_get)) {
     828   [ #  #  #  #  :        300 :     luaV_finishfastset(L, t, slot, s2v(L->top - 1));
                   #  # ]
     829                 :          0 :   }
     830                 :            :   else
     831                 :        150 :     luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot);
     832                 :        150 :   L->top -= 2;  /* pop index and value */
     833                 :            :   lua_unlock(L);
     834                 :        150 : }
     835                 :            : 
     836                 :            : 
     837                 :     162600 : LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
     838                 :            :   lua_lock(L);  /* unlock done in 'auxsetstr' */
     839                 :     162600 :   auxsetstr(L, index2value(L, idx), k);
     840                 :     162600 : }
     841                 :            : 
     842                 :            : 
     843                 :          0 : LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
     844                 :            :   TValue *t;
     845                 :            :   const TValue *slot;
     846                 :            :   lua_lock(L);
     847                 :          0 :   api_checknelems(L, 1);
     848                 :          0 :   t = index2value(L, idx);
     849   [ #  #  #  #  :          0 :   if (luaV_fastgeti(L, t, n, slot)) {
                   #  # ]
     850   [ #  #  #  #  :          0 :     luaV_finishfastset(L, t, slot, s2v(L->top - 1));
                   #  # ]
     851                 :          0 :   }
     852                 :            :   else {
     853                 :            :     TValue aux;
     854                 :          0 :     setivalue(&aux, n);
     855                 :          0 :     luaV_finishset(L, t, &aux, s2v(L->top - 1), slot);
     856                 :            :   }
     857                 :          0 :   L->top--;  /* pop value */
     858                 :            :   lua_unlock(L);
     859                 :          0 : }
     860                 :            : 
     861                 :            : 
     862                 :          0 : static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
     863                 :            :   Table *t;
     864                 :            :   TValue *slot;
     865                 :            :   lua_lock(L);
     866                 :          0 :   api_checknelems(L, n);
     867                 :          0 :   t = gettable(L, idx);
     868                 :          0 :   slot = luaH_set(L, t, key);
     869                 :          0 :   setobj2t(L, slot, s2v(L->top - 1));
     870                 :          0 :   invalidateTMcache(t);
     871   [ #  #  #  #  :          0 :   luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
                   #  # ]
     872                 :          0 :   L->top -= n;
     873                 :            :   lua_unlock(L);
     874                 :          0 : }
     875                 :            : 
     876                 :            : 
     877                 :          0 : LUA_API void lua_rawset (lua_State *L, int idx) {
     878                 :          0 :   aux_rawset(L, idx, s2v(L->top - 2), 2);
     879                 :          0 : }
     880                 :            : 
     881                 :            : 
     882                 :          0 : LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
     883                 :            :   TValue k;
     884                 :          0 :   setpvalue(&k, cast_voidp(p));
     885                 :          0 :   aux_rawset(L, idx, &k, 1);
     886                 :          0 : }
     887                 :            : 
     888                 :            : 
     889                 :       3308 : LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
     890                 :            :   Table *t;
     891                 :            :   lua_lock(L);
     892                 :       3308 :   api_checknelems(L, 1);
     893                 :       3308 :   t = gettable(L, idx);
     894                 :       3308 :   luaH_setint(L, t, n, s2v(L->top - 1));
     895   [ +  -  -  +  :       3308 :   luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
                   #  # ]
     896                 :       3308 :   L->top--;
     897                 :            :   lua_unlock(L);
     898                 :       3308 : }
     899                 :            : 
     900                 :            : 
     901                 :       4069 : LUA_API int lua_setmetatable (lua_State *L, int objindex) {
     902                 :            :   TValue *obj;
     903                 :            :   Table *mt;
     904                 :            :   lua_lock(L);
     905                 :       4069 :   api_checknelems(L, 1);
     906                 :       4069 :   obj = index2value(L, objindex);
     907         [ -  + ]:       4069 :   if (ttisnil(s2v(L->top - 1)))
     908                 :          0 :     mt = NULL;
     909                 :            :   else {
     910                 :       4069 :     api_check(L, ttistable(s2v(L->top - 1)), "table expected");
     911                 :       4069 :     mt = hvalue(s2v(L->top - 1));
     912                 :            :   }
     913      [ +  +  + ]:       4069 :   switch (ttype(obj)) {
     914                 :            :     case LUA_TTABLE: {
     915                 :        810 :       hvalue(obj)->metatable = mt;
     916         [ -  + ]:        810 :       if (mt) {
     917   [ -  +  #  # ]:        810 :         luaC_objbarrier(L, gcvalue(obj), mt);
     918                 :        810 :         luaC_checkfinalizer(L, gcvalue(obj), mt);
     919                 :        810 :       }
     920                 :        810 :       break;
     921                 :            :     }
     922                 :            :     case LUA_TUSERDATA: {
     923                 :       2449 :       uvalue(obj)->metatable = mt;
     924         [ -  + ]:       2449 :       if (mt) {
     925   [ -  +  #  # ]:       2449 :         luaC_objbarrier(L, uvalue(obj), mt);
     926                 :       2449 :         luaC_checkfinalizer(L, gcvalue(obj), mt);
     927                 :       2449 :       }
     928                 :       2449 :       break;
     929                 :            :     }
     930                 :            :     default: {
     931                 :        810 :       G(L)->mt[ttype(obj)] = mt;
     932                 :        810 :       break;
     933                 :            :     }
     934                 :            :   }
     935                 :       4069 :   L->top--;
     936                 :            :   lua_unlock(L);
     937                 :       4069 :   return 1;
     938                 :            : }
     939                 :            : 
     940                 :            : 
     941                 :          0 : LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
     942                 :            :   TValue *o;
     943                 :            :   int res;
     944                 :            :   lua_lock(L);
     945                 :          0 :   api_checknelems(L, 1);
     946                 :          0 :   o = index2value(L, idx);
     947                 :          0 :   api_check(L, ttisfulluserdata(o), "full userdata expected");
     948         [ #  # ]:          0 :   if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
     949                 :          0 :     res = 0;  /* 'n' not in [1, uvalue(o)->nuvalue] */
     950                 :            :   else {
     951                 :          0 :     setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1));
     952   [ #  #  #  #  :          0 :     luaC_barrierback(L, gcvalue(o), s2v(L->top - 1));
                   #  # ]
     953                 :          0 :     res = 1;
     954                 :            :   }
     955                 :          0 :   L->top--;
     956                 :            :   lua_unlock(L);
     957                 :          0 :   return res;
     958                 :            : }
     959                 :            : 
     960                 :            : 
     961                 :            : /*
     962                 :            : ** 'load' and 'call' functions (run Lua code)
     963                 :            : */
     964                 :            : 
     965                 :            : 
     966                 :            : #define checkresults(L,na,nr) \
     967                 :            :      api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
     968                 :            :         "results from function overflow current stack size")
     969                 :            : 
     970                 :            : 
     971                 :       8100 : LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
     972                 :            :                         lua_KContext ctx, lua_KFunction k) {
     973                 :            :   StkId func;
     974                 :            :   lua_lock(L);
     975                 :       8100 :   api_check(L, k == NULL || !isLua(L->ci),
     976                 :            :     "cannot use continuations inside hooks");
     977                 :       8100 :   api_checknelems(L, nargs+1);
     978                 :       8100 :   api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
     979                 :       8100 :   checkresults(L, nargs, nresults);
     980                 :       8100 :   func = L->top - (nargs+1);
     981   [ -  +  #  # ]:       8100 :   if (k != NULL && yieldable(L)) {  /* need to prepare continuation? */
     982                 :          0 :     L->ci->u.c.k = k;  /* save continuation */
     983                 :          0 :     L->ci->u.c.ctx = ctx;  /* save context */
     984                 :          0 :     luaD_call(L, func, nresults);  /* do the call */
     985                 :          0 :   }
     986                 :            :   else  /* no continuation or no yieldable */
     987                 :       8100 :     luaD_callnoyield(L, func, nresults);  /* just do the call */
     988   [ -  +  #  # ]:       8100 :   adjustresults(L, nresults);
     989                 :            :   lua_unlock(L);
     990                 :       8100 : }
     991                 :            : 
     992                 :            : 
     993                 :            : 
     994                 :            : /*
     995                 :            : ** Execute a protected call.
     996                 :            : */
     997                 :            : struct CallS {  /* data to 'f_call' */
     998                 :            :   StkId func;
     999                 :            :   int nresults;
    1000                 :            : };
    1001                 :            : 
    1002                 :            : 
    1003                 :        652 : static void f_call (lua_State *L, void *ud) {
    1004                 :        652 :   struct CallS *c = cast(struct CallS *, ud);
    1005                 :        652 :   luaD_callnoyield(L, c->func, c->nresults);
    1006                 :        652 : }
    1007                 :            : 
    1008                 :            : 
    1009                 :            : 
    1010                 :        652 : LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
    1011                 :            :                         lua_KContext ctx, lua_KFunction k) {
    1012                 :            :   struct CallS c;
    1013                 :            :   int status;
    1014                 :            :   ptrdiff_t func;
    1015                 :            :   lua_lock(L);
    1016                 :        652 :   api_check(L, k == NULL || !isLua(L->ci),
    1017                 :            :     "cannot use continuations inside hooks");
    1018                 :        652 :   api_checknelems(L, nargs+1);
    1019                 :        652 :   api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
    1020                 :        652 :   checkresults(L, nargs, nresults);
    1021         [ -  + ]:        652 :   if (errfunc == 0)
    1022                 :        652 :     func = 0;
    1023                 :            :   else {
    1024                 :          0 :     StkId o = index2stack(L, errfunc);
    1025                 :          0 :     api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
    1026                 :          0 :     func = savestack(L, o);
    1027                 :            :   }
    1028                 :        652 :   c.func = L->top - (nargs+1);  /* function to be called */
    1029   [ -  +  #  # ]:        652 :   if (k == NULL || !yieldable(L)) {  /* no continuation or no yieldable? */
    1030                 :        652 :     c.nresults = nresults;  /* do a 'conventional' protected call */
    1031                 :        652 :     status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
    1032                 :        652 :   }
    1033                 :            :   else {  /* prepare continuation (call is already protected by 'resume') */
    1034                 :          0 :     CallInfo *ci = L->ci;
    1035                 :          0 :     ci->u.c.k = k;  /* save continuation */
    1036                 :          0 :     ci->u.c.ctx = ctx;  /* save context */
    1037                 :            :     /* save information for error recovery */
    1038                 :          0 :     ci->u2.funcidx = cast_int(savestack(L, c.func));
    1039                 :          0 :     ci->u.c.old_errfunc = L->errfunc;
    1040                 :          0 :     L->errfunc = func;
    1041                 :          0 :     setoah(ci->callstatus, L->allowhook);  /* save value of 'allowhook' */
    1042                 :          0 :     ci->callstatus |= CIST_YPCALL;  /* function can do error recovery */
    1043                 :          0 :     luaD_call(L, c.func, nresults);  /* do the call */
    1044                 :          0 :     ci->callstatus &= ~CIST_YPCALL;
    1045                 :          0 :     L->errfunc = ci->u.c.old_errfunc;
    1046                 :          0 :     status = LUA_OK;  /* if it is here, there were no errors */
    1047                 :            :   }
    1048   [ +  -  +  - ]:        652 :   adjustresults(L, nresults);
    1049                 :            :   lua_unlock(L);
    1050                 :        652 :   return status;
    1051                 :            : }
    1052                 :            : 
    1053                 :            : 
    1054                 :        656 : LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
    1055                 :            :                       const char *chunkname, const char *mode) {
    1056                 :            :   ZIO z;
    1057                 :            :   int status;
    1058                 :            :   lua_lock(L);
    1059         [ +  - ]:        656 :   if (!chunkname) chunkname = "?";
    1060                 :        656 :   luaZ_init(L, &z, reader, data);
    1061                 :        656 :   status = luaD_protectedparser(L, &z, chunkname, mode);
    1062         [ +  + ]:        656 :   if (status == LUA_OK) {  /* no errors? */
    1063                 :        652 :     LClosure *f = clLvalue(s2v(L->top - 1));  /* get newly created function */
    1064         [ -  + ]:        652 :     if (f->nupvalues >= 1) {  /* does it have an upvalue? */
    1065                 :            :       /* get global table from registry */
    1066                 :        652 :       Table *reg = hvalue(&G(L)->l_registry);
    1067                 :        652 :       const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
    1068                 :            :       /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
    1069                 :        652 :       setobj(L, f->upvals[0]->v, gt);
    1070   [ +  -  -  +  :        652 :       luaC_barrier(L, f->upvals[0], gt);
                   #  # ]
    1071                 :        652 :     }
    1072                 :        652 :   }
    1073                 :            :   lua_unlock(L);
    1074                 :        656 :   return status;
    1075                 :            : }
    1076                 :            : 
    1077                 :            : 
    1078                 :          0 : LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
    1079                 :            :   int status;
    1080                 :            :   TValue *o;
    1081                 :            :   lua_lock(L);
    1082                 :          0 :   api_checknelems(L, 1);
    1083                 :          0 :   o = s2v(L->top - 1);
    1084         [ #  # ]:          0 :   if (isLfunction(o))
    1085                 :          0 :     status = luaU_dump(L, getproto(o), writer, data, strip);
    1086                 :            :   else
    1087                 :          0 :     status = 1;
    1088                 :            :   lua_unlock(L);
    1089                 :          0 :   return status;
    1090                 :            : }
    1091                 :            : 
    1092                 :            : 
    1093                 :          0 : LUA_API int lua_status (lua_State *L) {
    1094                 :          0 :   return L->status;
    1095                 :            : }
    1096                 :            : 
    1097                 :            : 
    1098                 :            : /*
    1099                 :            : ** Garbage-collection function
    1100                 :            : */
    1101                 :          0 : LUA_API int lua_gc (lua_State *L, int what, ...) {
    1102                 :            :   va_list argp;
    1103                 :          0 :   int res = 0;
    1104                 :            :   global_State *g;
    1105                 :            :   lua_lock(L);
    1106                 :          0 :   g = G(L);
    1107                 :          0 :   va_start(argp, what);
    1108   [ #  #  #  #  :          0 :   switch (what) {
          #  #  #  #  #  
                #  #  # ]
    1109                 :            :     case LUA_GCSTOP: {
    1110                 :          0 :       g->gcrunning = 0;
    1111                 :          0 :       break;
    1112                 :            :     }
    1113                 :            :     case LUA_GCRESTART: {
    1114                 :          0 :       luaE_setdebt(g, 0);
    1115                 :          0 :       g->gcrunning = 1;
    1116                 :          0 :       break;
    1117                 :            :     }
    1118                 :            :     case LUA_GCCOLLECT: {
    1119                 :          0 :       luaC_fullgc(L, 0);
    1120                 :          0 :       break;
    1121                 :            :     }
    1122                 :            :     case LUA_GCCOUNT: {
    1123                 :            :       /* GC values are expressed in Kbytes: #bytes/2^10 */
    1124                 :          0 :       res = cast_int(gettotalbytes(g) >> 10);
    1125                 :          0 :       break;
    1126                 :            :     }
    1127                 :            :     case LUA_GCCOUNTB: {
    1128                 :          0 :       res = cast_int(gettotalbytes(g) & 0x3ff);
    1129                 :          0 :       break;
    1130                 :            :     }
    1131                 :            :     case LUA_GCSTEP: {
    1132         [ #  # ]:          0 :       int data = va_arg(argp, int);
    1133                 :          0 :       l_mem debt = 1;  /* =1 to signal that it did an actual step */
    1134                 :          0 :       lu_byte oldrunning = g->gcrunning;
    1135                 :          0 :       g->gcrunning = 1;  /* allow GC to run */
    1136         [ #  # ]:          0 :       if (data == 0) {
    1137                 :          0 :         luaE_setdebt(g, 0);  /* do a basic step */
    1138                 :          0 :         luaC_step(L);
    1139                 :          0 :       }
    1140                 :            :       else {  /* add 'data' to total debt */
    1141                 :          0 :         debt = cast(l_mem, data) * 1024 + g->GCdebt;
    1142                 :          0 :         luaE_setdebt(g, debt);
    1143         [ #  # ]:          0 :         luaC_checkGC(L);
    1144                 :            :       }
    1145                 :          0 :       g->gcrunning = oldrunning;  /* restore previous state */
    1146   [ #  #  #  # ]:          0 :       if (debt > 0 && g->gcstate == GCSpause)  /* end of cycle? */
    1147                 :          0 :         res = 1;  /* signal it */
    1148                 :          0 :       break;
    1149                 :            :     }
    1150                 :            :     case LUA_GCSETPAUSE: {
    1151         [ #  # ]:          0 :       int data = va_arg(argp, int);
    1152                 :          0 :       res = getgcparam(g->gcpause);
    1153                 :          0 :       setgcparam(g->gcpause, data);
    1154                 :          0 :       break;
    1155                 :            :     }
    1156                 :            :     case LUA_GCSETSTEPMUL: {
    1157         [ #  # ]:          0 :       int data = va_arg(argp, int);
    1158                 :          0 :       res = getgcparam(g->gcstepmul);
    1159                 :          0 :       setgcparam(g->gcstepmul, data);
    1160                 :          0 :       break;
    1161                 :            :     }
    1162                 :            :     case LUA_GCISRUNNING: {
    1163                 :          0 :       res = g->gcrunning;
    1164                 :          0 :       break;
    1165                 :            :     }
    1166                 :            :     case LUA_GCGEN: {
    1167         [ #  # ]:          0 :       int minormul = va_arg(argp, int);
    1168         [ #  # ]:          0 :       int majormul = va_arg(argp, int);
    1169         [ #  # ]:          0 :       res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
    1170         [ #  # ]:          0 :       if (minormul != 0)
    1171                 :          0 :         g->genminormul = minormul;
    1172         [ #  # ]:          0 :       if (majormul != 0)
    1173                 :          0 :         setgcparam(g->genmajormul, majormul);
    1174                 :          0 :       luaC_changemode(L, KGC_GEN);
    1175                 :          0 :       break;
    1176                 :            :     }
    1177                 :            :     case LUA_GCINC: {
    1178         [ #  # ]:          0 :       int pause = va_arg(argp, int);
    1179         [ #  # ]:          0 :       int stepmul = va_arg(argp, int);
    1180         [ #  # ]:          0 :       int stepsize = va_arg(argp, int);
    1181         [ #  # ]:          0 :       res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
    1182         [ #  # ]:          0 :       if (pause != 0)
    1183                 :          0 :         setgcparam(g->gcpause, pause);
    1184         [ #  # ]:          0 :       if (stepmul != 0)
    1185                 :          0 :         setgcparam(g->gcstepmul, stepmul);
    1186         [ #  # ]:          0 :       if (stepsize != 0)
    1187                 :          0 :         g->gcstepsize = stepsize;
    1188                 :          0 :       luaC_changemode(L, KGC_INC);
    1189                 :          0 :       break;
    1190                 :            :     }
    1191                 :          0 :     default: res = -1;  /* invalid option */
    1192                 :          0 :   }
    1193                 :          0 :   va_end(argp);
    1194                 :            :   lua_unlock(L);
    1195                 :          0 :   return res;
    1196                 :            : }
    1197                 :            : 
    1198                 :            : 
    1199                 :            : 
    1200                 :            : /*
    1201                 :            : ** miscellaneous functions
    1202                 :            : */
    1203                 :            : 
    1204                 :            : 
    1205                 :        282 : LUA_API int lua_error (lua_State *L) {
    1206                 :            :   TValue *errobj;
    1207                 :            :   lua_lock(L);
    1208                 :        282 :   errobj = s2v(L->top - 1);
    1209                 :        282 :   api_checknelems(L, 1);
    1210                 :            :   /* error object is the memory error message? */
    1211   [ -  +  #  # ]:        282 :   if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
    1212                 :          0 :     luaM_error(L);  /* raise a memory error */
    1213                 :            :   else
    1214                 :        282 :     luaG_errormsg(L);  /* raise a regular error */
    1215                 :            :   /* code unreachable; will unlock when control actually leaves the kernel */
    1216                 :            :   return 0;  /* to avoid warnings */
    1217                 :            : }
    1218                 :            : 
    1219                 :            : 
    1220                 :          0 : LUA_API int lua_next (lua_State *L, int idx) {
    1221                 :            :   Table *t;
    1222                 :            :   int more;
    1223                 :            :   lua_lock(L);
    1224                 :          0 :   api_checknelems(L, 1);
    1225                 :          0 :   t = gettable(L, idx);
    1226                 :          0 :   more = luaH_next(L, t, L->top - 1);
    1227         [ #  # ]:          0 :   if (more) {
    1228                 :          0 :     api_incr_top(L);
    1229                 :          0 :   }
    1230                 :            :   else  /* no more elements */
    1231                 :          0 :     L->top -= 1;  /* remove key */
    1232                 :            :   lua_unlock(L);
    1233                 :          0 :   return more;
    1234                 :            : }
    1235                 :            : 
    1236                 :            : 
    1237                 :          0 : LUA_API void lua_toclose (lua_State *L, int idx) {
    1238                 :            :   int nresults;
    1239                 :            :   StkId o;
    1240                 :            :   lua_lock(L);
    1241                 :          0 :   o = index2stack(L, idx);
    1242                 :          0 :   nresults = L->ci->nresults;
    1243                 :          0 :   api_check(L, L->openupval == NULL || uplevel(L->openupval) <= o,
    1244                 :            :                "marked index below or equal new one");
    1245                 :          0 :   luaF_newtbcupval(L, o);  /* create new to-be-closed upvalue */
    1246         [ #  # ]:          0 :   if (!hastocloseCfunc(nresults))  /* function not marked yet? */
    1247                 :          0 :     L->ci->nresults = codeNresults(nresults);  /* mark it */
    1248                 :            :   lua_assert(hastocloseCfunc(L->ci->nresults));
    1249                 :            :   lua_unlock(L);
    1250                 :          0 : }
    1251                 :            : 
    1252                 :            : 
    1253                 :        282 : LUA_API void lua_concat (lua_State *L, int n) {
    1254                 :            :   lua_lock(L);
    1255                 :        282 :   api_checknelems(L, n);
    1256         [ -  + ]:        282 :   if (n > 0)
    1257                 :        282 :     luaV_concat(L, n);
    1258                 :            :   else {  /* nothing to concatenate */
    1259                 :          0 :     setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));  /* push empty string */
    1260                 :          0 :     api_incr_top(L);
    1261                 :            :   }
    1262         [ +  + ]:        282 :   luaC_checkGC(L);
    1263                 :            :   lua_unlock(L);
    1264                 :        282 : }
    1265                 :            : 
    1266                 :            : 
    1267                 :         40 : LUA_API void lua_len (lua_State *L, int idx) {
    1268                 :            :   TValue *t;
    1269                 :            :   lua_lock(L);
    1270                 :         40 :   t = index2value(L, idx);
    1271                 :         40 :   luaV_objlen(L, L->top, t);
    1272                 :         40 :   api_incr_top(L);
    1273                 :            :   lua_unlock(L);
    1274                 :         40 : }
    1275                 :            : 
    1276                 :            : 
    1277                 :          0 : LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
    1278                 :            :   lua_Alloc f;
    1279                 :            :   lua_lock(L);
    1280         [ #  # ]:          0 :   if (ud) *ud = G(L)->ud;
    1281                 :          0 :   f = G(L)->frealloc;
    1282                 :            :   lua_unlock(L);
    1283                 :          0 :   return f;
    1284                 :            : }
    1285                 :            : 
    1286                 :            : 
    1287                 :          0 : LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
    1288                 :            :   lua_lock(L);
    1289                 :          0 :   G(L)->ud = ud;
    1290                 :          0 :   G(L)->frealloc = f;
    1291                 :            :   lua_unlock(L);
    1292                 :          0 : }
    1293                 :            : 
    1294                 :            : 
    1295                 :        810 : void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
    1296                 :            :   lua_lock(L);
    1297                 :        810 :   G(L)->ud_warn = ud;
    1298                 :        810 :   G(L)->warnf = f;
    1299                 :            :   lua_unlock(L);
    1300                 :        810 : }
    1301                 :            : 
    1302                 :            : 
    1303                 :          0 : void lua_warning (lua_State *L, const char *msg, int tocont) {
    1304                 :            :   lua_lock(L);
    1305                 :          0 :   luaE_warning(L, msg, tocont);
    1306                 :            :   lua_unlock(L);
    1307                 :          0 : }
    1308                 :            : 
    1309                 :            : 
    1310                 :            : 
    1311                 :       3268 : LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
    1312                 :            :   Udata *u;
    1313                 :            :   lua_lock(L);
    1314                 :       3268 :   api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value");
    1315                 :       3268 :   u = luaS_newudata(L, size, nuvalue);
    1316                 :       3268 :   setuvalue(L, s2v(L->top), u);
    1317                 :       3268 :   api_incr_top(L);
    1318         [ +  - ]:       3268 :   luaC_checkGC(L);
    1319                 :            :   lua_unlock(L);
    1320         [ +  + ]:       3268 :   return getudatamem(u);
    1321                 :            : }
    1322                 :            : 
    1323                 :            : 
    1324                 :            : 
    1325                 :          0 : static const char *aux_upvalue (TValue *fi, int n, TValue **val,
    1326                 :            :                                 GCObject **owner) {
    1327      [ #  #  # ]:          0 :   switch (ttypetag(fi)) {
    1328                 :            :     case LUA_VCCL: {  /* C closure */
    1329                 :          0 :       CClosure *f = clCvalue(fi);
    1330         [ #  # ]:          0 :       if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
    1331                 :          0 :         return NULL;  /* 'n' not in [1, f->nupvalues] */
    1332                 :          0 :       *val = &f->upvalue[n-1];
    1333         [ #  # ]:          0 :       if (owner) *owner = obj2gco(f);
    1334                 :          0 :       return "";
    1335                 :            :     }
    1336                 :            :     case LUA_VLCL: {  /* Lua closure */
    1337                 :          0 :       LClosure *f = clLvalue(fi);
    1338                 :            :       TString *name;
    1339                 :          0 :       Proto *p = f->p;
    1340         [ #  # ]:          0 :       if (!(cast_uint(n) - 1u  < cast_uint(p->sizeupvalues)))
    1341                 :          0 :         return NULL;  /* 'n' not in [1, p->sizeupvalues] */
    1342                 :          0 :       *val = f->upvals[n-1]->v;
    1343         [ #  # ]:          0 :       if (owner) *owner = obj2gco(f->upvals[n - 1]);
    1344                 :          0 :       name = p->upvalues[n-1].name;
    1345         [ #  # ]:          0 :       return (name == NULL) ? "(no name)" : getstr(name);
    1346                 :            :     }
    1347                 :          0 :     default: return NULL;  /* not a closure */
    1348                 :            :   }
    1349                 :          0 : }
    1350                 :            : 
    1351                 :            : 
    1352                 :          0 : LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
    1353                 :            :   const char *name;
    1354                 :          0 :   TValue *val = NULL;  /* to avoid warnings */
    1355                 :            :   lua_lock(L);
    1356                 :          0 :   name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
    1357         [ #  # ]:          0 :   if (name) {
    1358                 :          0 :     setobj2s(L, L->top, val);
    1359                 :          0 :     api_incr_top(L);
    1360                 :          0 :   }
    1361                 :            :   lua_unlock(L);
    1362                 :          0 :   return name;
    1363                 :            : }
    1364                 :            : 
    1365                 :            : 
    1366                 :          0 : LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
    1367                 :            :   const char *name;
    1368                 :          0 :   TValue *val = NULL;  /* to avoid warnings */
    1369                 :          0 :   GCObject *owner = NULL;  /* to avoid warnings */
    1370                 :            :   TValue *fi;
    1371                 :            :   lua_lock(L);
    1372                 :          0 :   fi = index2value(L, funcindex);
    1373                 :          0 :   api_checknelems(L, 1);
    1374                 :          0 :   name = aux_upvalue(fi, n, &val, &owner);
    1375         [ #  # ]:          0 :   if (name) {
    1376                 :          0 :     L->top--;
    1377                 :          0 :     setobj(L, val, s2v(L->top));
    1378   [ #  #  #  #  :          0 :     luaC_barrier(L, owner, val);
                   #  # ]
    1379                 :          0 :   }
    1380                 :            :   lua_unlock(L);
    1381                 :          0 :   return name;
    1382                 :            : }
    1383                 :            : 
    1384                 :            : 
    1385                 :          0 : static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
    1386                 :            :   static const UpVal *const nullup = NULL;
    1387                 :            :   LClosure *f;
    1388                 :          0 :   TValue *fi = index2value(L, fidx);
    1389                 :          0 :   api_check(L, ttisLclosure(fi), "Lua function expected");
    1390                 :          0 :   f = clLvalue(fi);
    1391         [ #  # ]:          0 :   if (pf) *pf = f;
    1392   [ #  #  #  # ]:          0 :   if (1 <= n && n <= f->p->sizeupvalues)
    1393                 :          0 :     return &f->upvals[n - 1];  /* get its upvalue pointer */
    1394                 :            :   else
    1395                 :          0 :     return (UpVal**)&nullup;
    1396                 :          0 : }
    1397                 :            : 
    1398                 :            : 
    1399                 :          0 : LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
    1400                 :          0 :   TValue *fi = index2value(L, fidx);
    1401   [ #  #  #  # ]:          0 :   switch (ttypetag(fi)) {
    1402                 :            :     case LUA_VLCL: {  /* lua closure */
    1403                 :          0 :       return *getupvalref(L, fidx, n, NULL);
    1404                 :            :     }
    1405                 :            :     case LUA_VCCL: {  /* C closure */
    1406                 :          0 :       CClosure *f = clCvalue(fi);
    1407   [ #  #  #  # ]:          0 :       if (1 <= n && n <= f->nupvalues)
    1408                 :          0 :         return &f->upvalue[n - 1];
    1409                 :            :       /* else */
    1410                 :          0 :     }  /* FALLTHROUGH */
    1411                 :            :     case LUA_VLCF:
    1412                 :          0 :       return NULL;  /* light C functions have no upvalues */
    1413                 :            :     default: {
    1414                 :          0 :       api_check(L, 0, "function expected");
    1415                 :          0 :       return NULL;
    1416                 :            :     }
    1417                 :            :   }
    1418                 :          0 : }
    1419                 :            : 
    1420                 :            : 
    1421                 :          0 : LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
    1422                 :            :                                             int fidx2, int n2) {
    1423                 :            :   LClosure *f1;
    1424                 :          0 :   UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
    1425                 :          0 :   UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
    1426                 :          0 :   api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index");
    1427                 :          0 :   *up1 = *up2;
    1428   [ #  #  #  # ]:          0 :   luaC_objbarrier(L, f1, *up1);
    1429                 :          0 : }
    1430                 :            : 
    1431                 :            : 

Generated by: LCOV version 1.15