LCOV - code coverage report
Current view: top level - src - lapi.c Coverage Total Hit
Test: Lua 5.2.4 Lines: 87.3 % 605 528
Test Date: 2024-04-28 10:23:12
Legend: Lines: hit not hit

            Line data    Source code
       1              : /*
       2              : ** $Id: lapi.c,v 2.171.1.1 2013/04/12 18:48:47 roberto Exp $
       3              : ** Lua API
       4              : ** See Copyright Notice in lua.h
       5              : */
       6              : 
       7              : 
       8              : #include <stdarg.h>
       9              : #include <string.h>
      10              : 
      11              : #define lapi_c
      12              : #define LUA_CORE
      13              : 
      14              : #include "lua.h"
      15              : 
      16              : #include "lapi.h"
      17              : #include "ldebug.h"
      18              : #include "ldo.h"
      19              : #include "lfunc.h"
      20              : #include "lgc.h"
      21              : #include "lmem.h"
      22              : #include "lobject.h"
      23              : #include "lstate.h"
      24              : #include "lstring.h"
      25              : #include "ltable.h"
      26              : #include "ltm.h"
      27              : #include "lundump.h"
      28              : #include "lvm.h"
      29              : 
      30              : 
      31              : 
      32              : const char lua_ident[] =
      33              :   "$LuaVersion: " LUA_COPYRIGHT " $"
      34              :   "$LuaAuthors: " LUA_AUTHORS " $";
      35              : 
      36              : 
      37              : /* value at a non-valid index */
      38              : #define NONVALIDVALUE           cast(TValue *, luaO_nilobject)
      39              : 
      40              : /* corresponding test */
      41              : #define isvalid(o)      ((o) != luaO_nilobject)
      42              : 
      43              : /* test for pseudo index */
      44              : #define ispseudo(i)             ((i) <= LUA_REGISTRYINDEX)
      45              : 
      46              : /* test for valid but not pseudo index */
      47              : #define isstackindex(i, o)      (isvalid(o) && !ispseudo(i))
      48              : 
      49              : #define api_checkvalidindex(L, o)  api_check(L, isvalid(o), "invalid index")
      50              : 
      51              : #define api_checkstackindex(L, i, o)  \
      52              :         api_check(L, isstackindex(i, o), "index not in the stack")
      53              : 
      54              : 
      55      1308778 : static TValue *index2addr (lua_State *L, int idx) {
      56      1308778 :   CallInfo *ci = L->ci;
      57      1308778 :   if (idx > 0) {
      58       741291 :     TValue *o = ci->func + idx;
      59              :     api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
      60       741291 :     if (o >= L->top) return NONVALIDVALUE;
      61       709831 :     else return o;
      62              :   }
      63       567487 :   else if (!ispseudo(idx)) {  /* negative index */
      64              :     api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
      65       562114 :     return L->top + idx;
      66              :   }
      67         5373 :   else if (idx == LUA_REGISTRYINDEX)
      68         4564 :     return &G(L)->l_registry;
      69              :   else {  /* upvalues */
      70          809 :     idx = LUA_REGISTRYINDEX - idx;
      71              :     api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
      72          809 :     if (ttislcf(ci->func))  /* light C function? */
      73            0 :       return NONVALIDVALUE;  /* it has no upvalues */
      74              :     else {
      75          809 :       CClosure *func = clCvalue(ci->func);
      76          809 :       return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
      77              :     }
      78              :   }
      79              : }
      80              : 
      81              : 
      82              : /*
      83              : ** to be called by 'lua_checkstack' in protected mode, to grow stack
      84              : ** capturing memory errors
      85              : */
      86           15 : static void growstack (lua_State *L, void *ud) {
      87           15 :   int size = *(int *)ud;
      88           15 :   luaD_growstack(L, size);
      89           15 : }
      90              : 
      91              : 
      92        20137 : LUA_API int lua_checkstack (lua_State *L, int size) {
      93              :   int res;
      94        20137 :   CallInfo *ci = L->ci;
      95              :   lua_lock(L);
      96        20137 :   if (L->stack_last - L->top > size)  /* stack large enough? */
      97        20122 :     res = 1;  /* yes; check is OK */
      98              :   else {  /* no; need to grow stack */
      99           15 :     int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
     100           15 :     if (inuse > LUAI_MAXSTACK - size)  /* can grow without overflow? */
     101            0 :       res = 0;  /* no */
     102              :     else  /* try to grow stack */
     103           15 :       res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK);
     104              :   }
     105        20137 :   if (res && ci->top < L->top + size)
     106         7655 :     ci->top = L->top + size;  /* adjust frame top */
     107              :   lua_unlock(L);
     108        20137 :   return res;
     109              : }
     110              : 
     111              : 
     112        11977 : LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
     113              :   int i;
     114        11977 :   if (from == to) return;
     115              :   lua_lock(to);
     116              :   api_checknelems(from, n);
     117              :   api_check(from, G(from) == G(to), "moving among independent states");
     118              :   api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
     119        11967 :   from->top -= n;
     120        17999 :   for (i = 0; i < n; i++) {
     121         6032 :     setobj2s(to, to->top++, from->top + i);
     122              :   }
     123              :   lua_unlock(to);
     124              : }
     125              : 
     126              : 
     127          104 : LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
     128              :   lua_CFunction old;
     129              :   lua_lock(L);
     130          104 :   old = G(L)->panic;
     131          104 :   G(L)->panic = panicf;
     132              :   lua_unlock(L);
     133          104 :   return old;
     134              : }
     135              : 
     136              : 
     137         2340 : LUA_API const lua_Number *lua_version (lua_State *L) {
     138              :   static const lua_Number version = LUA_VERSION_NUM;
     139         2340 :   if (L == NULL) return &version;
     140         1118 :   else return G(L)->version;
     141              : }
     142              : 
     143              : 
     144              : 
     145              : /*
     146              : ** basic stack manipulation
     147              : */
     148              : 
     149              : 
     150              : /*
     151              : ** convert an acceptable stack index into an absolute index
     152              : */
     153         4616 : LUA_API int lua_absindex (lua_State *L, int idx) {
     154          259 :   return (idx > 0 || ispseudo(idx))
     155              :          ? idx
     156         4875 :          : cast_int(L->top - L->ci->func + idx);
     157              : }
     158              : 
     159              : 
     160        27670 : LUA_API int lua_gettop (lua_State *L) {
     161        27670 :   return cast_int(L->top - (L->ci->func + 1));
     162              : }
     163              : 
     164              : 
     165        91515 : LUA_API void lua_settop (lua_State *L, int idx) {
     166        91515 :   StkId func = L->ci->func;
     167              :   lua_lock(L);
     168        91515 :   if (idx >= 0) {
     169              :     api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
     170        12394 :     while (L->top < (func + 1) + idx)
     171         5937 :       setnilvalue(L->top++);
     172         6457 :     L->top = (func + 1) + idx;
     173              :   }
     174              :   else {
     175              :     api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
     176        85058 :     L->top += idx+1;  /* `subtract' index (index is negative) */
     177              :   }
     178              :   lua_unlock(L);
     179        91515 : }
     180              : 
     181              : 
     182        85339 : LUA_API void lua_remove (lua_State *L, int idx) {
     183              :   StkId p;
     184              :   lua_lock(L);
     185        85339 :   p = index2addr(L, idx);
     186              :   api_checkstackindex(L, idx, p);
     187        88185 :   while (++p < L->top) setobjs2s(L, p-1, p);
     188        85339 :   L->top--;
     189              :   lua_unlock(L);
     190        85339 : }
     191              : 
     192              : 
     193         6882 : LUA_API void lua_insert (lua_State *L, int idx) {
     194              :   StkId p;
     195              :   StkId q;
     196              :   lua_lock(L);
     197         6882 :   p = index2addr(L, idx);
     198              :   api_checkstackindex(L, idx, p);
     199        20679 :   for (q = L->top; q > p; q--)  /* use L->top as a temporary */
     200        13797 :     setobjs2s(L, q, q - 1);
     201         6882 :   setobjs2s(L, p, L->top);
     202              :   lua_unlock(L);
     203         6882 : }
     204              : 
     205              : 
     206          548 : static void moveto (lua_State *L, TValue *fr, int idx) {
     207          548 :   TValue *to = index2addr(L, idx);
     208              :   api_checkvalidindex(L, to);
     209          548 :   setobj(L, to, fr);
     210          548 :   if (idx < LUA_REGISTRYINDEX)  /* function upvalue? */
     211           10 :     luaC_barrier(L, clCvalue(L->ci->func), fr);
     212              :   /* LUA_REGISTRYINDEX does not need gc barrier
     213              :      (collector revisits it before finishing collection) */
     214          548 : }
     215              : 
     216              : 
     217          540 : LUA_API void lua_replace (lua_State *L, int idx) {
     218              :   lua_lock(L);
     219              :   api_checknelems(L, 1);
     220          540 :   moveto(L, L->top - 1, idx);
     221          540 :   L->top--;
     222              :   lua_unlock(L);
     223          540 : }
     224              : 
     225              : 
     226            8 : LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
     227              :   TValue *fr;
     228              :   lua_lock(L);
     229            8 :   fr = index2addr(L, fromidx);
     230            8 :   moveto(L, fr, toidx);
     231              :   lua_unlock(L);
     232            8 : }
     233              : 
     234              : 
     235        27443 : LUA_API void lua_pushvalue (lua_State *L, int idx) {
     236              :   lua_lock(L);
     237        27443 :   setobj2s(L, L->top, index2addr(L, idx));
     238        27443 :   api_incr_top(L);
     239              :   lua_unlock(L);
     240        27443 : }
     241              : 
     242              : 
     243              : 
     244              : /*
     245              : ** access functions (stack -> C)
     246              : */
     247              : 
     248              : 
     249       356509 : LUA_API int lua_type (lua_State *L, int idx) {
     250       356509 :   StkId o = index2addr(L, idx);
     251       356509 :   return (isvalid(o) ? ttypenv(o) : LUA_TNONE);
     252              : }
     253              : 
     254              : 
     255          184 : LUA_API const char *lua_typename (lua_State *L, int t) {
     256              :   UNUSED(L);
     257          184 :   return ttypename(t);
     258              : }
     259              : 
     260              : 
     261            4 : LUA_API int lua_iscfunction (lua_State *L, int idx) {
     262            4 :   StkId o = index2addr(L, idx);
     263            4 :   return (ttislcf(o) || (ttisCclosure(o)));
     264              : }
     265              : 
     266              : 
     267            6 : LUA_API int lua_isnumber (lua_State *L, int idx) {
     268              :   TValue n;
     269            6 :   const TValue *o = index2addr(L, idx);
     270            6 :   return tonumber(o, &n);
     271              : }
     272              : 
     273              : 
     274        81209 : LUA_API int lua_isstring (lua_State *L, int idx) {
     275        81209 :   int t = lua_type(L, idx);
     276        81209 :   return (t == LUA_TSTRING || t == LUA_TNUMBER);
     277              : }
     278              : 
     279              : 
     280            0 : LUA_API int lua_isuserdata (lua_State *L, int idx) {
     281            0 :   const TValue *o = index2addr(L, idx);
     282            0 :   return (ttisuserdata(o) || ttislightuserdata(o));
     283              : }
     284              : 
     285              : 
     286         2369 : LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
     287         2369 :   StkId o1 = index2addr(L, index1);
     288         2369 :   StkId o2 = index2addr(L, index2);
     289         2369 :   return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
     290              : }
     291              : 
     292              : 
     293            0 : LUA_API void lua_arith (lua_State *L, int op) {
     294              :   StkId o1;  /* 1st operand */
     295              :   StkId o2;  /* 2nd operand */
     296              :   lua_lock(L);
     297            0 :   if (op != LUA_OPUNM) /* all other operations expect two operands */
     298              :     api_checknelems(L, 2);
     299              :   else {  /* for unary minus, add fake 2nd operand */
     300              :     api_checknelems(L, 1);
     301            0 :     setobjs2s(L, L->top, L->top - 1);
     302            0 :     L->top++;
     303              :   }
     304            0 :   o1 = L->top - 2;
     305            0 :   o2 = L->top - 1;
     306            0 :   if (ttisnumber(o1) && ttisnumber(o2)) {
     307            0 :     setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
     308              :   }
     309              :   else
     310            0 :     luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
     311            0 :   L->top--;
     312              :   lua_unlock(L);
     313            0 : }
     314              : 
     315              : 
     316        91611 : LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
     317              :   StkId o1, o2;
     318        91611 :   int i = 0;
     319              :   lua_lock(L);  /* may call tag method */
     320        91611 :   o1 = index2addr(L, index1);
     321        91611 :   o2 = index2addr(L, index2);
     322        91611 :   if (isvalid(o1) && isvalid(o2)) {
     323        91611 :     switch (op) {
     324            0 :       case LUA_OPEQ: i = equalobj(L, o1, o2); break;
     325        91611 :       case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
     326            0 :       case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
     327            0 :       default: api_check(L, 0, "invalid option");
     328              :     }
     329              :   }
     330              :   lua_unlock(L);
     331        91611 :   return i;
     332              : }
     333              : 
     334              : 
     335          275 : LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) {
     336              :   TValue n;
     337          275 :   const TValue *o = index2addr(L, idx);
     338          275 :   if (tonumber(o, &n)) {
     339          268 :     if (isnum) *isnum = 1;
     340          268 :     return nvalue(o);
     341              :   }
     342              :   else {
     343            7 :     if (isnum) *isnum = 0;
     344            7 :     return 0;
     345              :   }
     346              : }
     347              : 
     348              : 
     349        78584 : LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
     350              :   TValue n;
     351        78584 :   const TValue *o = index2addr(L, idx);
     352        78584 :   if (tonumber(o, &n)) {
     353              :     lua_Integer res;
     354        78570 :     lua_Number num = nvalue(o);
     355        78570 :     lua_number2integer(res, num);
     356        78570 :     if (isnum) *isnum = 1;
     357        78570 :     return res;
     358              :   }
     359              :   else {
     360           14 :     if (isnum) *isnum = 0;
     361           14 :     return 0;
     362              :   }
     363              : }
     364              : 
     365              : 
     366         1151 : LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) {
     367              :   TValue n;
     368         1151 :   const TValue *o = index2addr(L, idx);
     369         1151 :   if (tonumber(o, &n)) {
     370              :     lua_Unsigned res;
     371         1151 :     lua_Number num = nvalue(o);
     372         1151 :     lua_number2unsigned(res, num);
     373         1151 :     if (isnum) *isnum = 1;
     374         1151 :     return res;
     375              :   }
     376              :   else {
     377            0 :     if (isnum) *isnum = 0;
     378            0 :     return 0;
     379              :   }
     380              : }
     381              : 
     382              : 
     383         6402 : LUA_API int lua_toboolean (lua_State *L, int idx) {
     384         6402 :   const TValue *o = index2addr(L, idx);
     385         6402 :   return !l_isfalse(o);
     386              : }
     387              : 
     388              : 
     389       113266 : LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
     390       113266 :   StkId o = index2addr(L, idx);
     391       113266 :   if (!ttisstring(o)) {
     392              :     lua_lock(L);  /* `luaV_tostring' may create a new string */
     393         1910 :     if (!luaV_tostring(L, o)) {  /* conversion failed? */
     394           18 :       if (len != NULL) *len = 0;
     395              :       lua_unlock(L);
     396           18 :       return NULL;
     397              :     }
     398         1892 :     luaC_checkGC(L);
     399         1892 :     o = index2addr(L, idx);  /* previous call may reallocate the stack */
     400              :     lua_unlock(L);
     401              :   }
     402       113248 :   if (len != NULL) *len = tsvalue(o)->len;
     403       113248 :   return svalue(o);
     404              : }
     405              : 
     406              : 
     407           19 : LUA_API size_t lua_rawlen (lua_State *L, int idx) {
     408           19 :   StkId o = index2addr(L, idx);
     409           19 :   switch (ttypenv(o)) {
     410           18 :     case LUA_TSTRING: return tsvalue(o)->len;
     411            0 :     case LUA_TUSERDATA: return uvalue(o)->len;
     412            1 :     case LUA_TTABLE: return luaH_getn(hvalue(o));
     413            0 :     default: return 0;
     414              :   }
     415              : }
     416              : 
     417              : 
     418            0 : LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
     419            0 :   StkId o = index2addr(L, idx);
     420            0 :   if (ttislcf(o)) return fvalue(o);
     421            0 :   else if (ttisCclosure(o))
     422            0 :     return clCvalue(o)->f;
     423            0 :   else return NULL;  /* not a C function */
     424              : }
     425              : 
     426              : 
     427         1645 : LUA_API void *lua_touserdata (lua_State *L, int idx) {
     428         1645 :   StkId o = index2addr(L, idx);
     429         1645 :   switch (ttypenv(o)) {
     430         1538 :     case LUA_TUSERDATA: return (rawuvalue(o) + 1);
     431          104 :     case LUA_TLIGHTUSERDATA: return pvalue(o);
     432            3 :     default: return NULL;
     433              :   }
     434              : }
     435              : 
     436              : 
     437         5978 : LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
     438         5978 :   StkId o = index2addr(L, idx);
     439         5978 :   return (!ttisthread(o)) ? NULL : thvalue(o);
     440              : }
     441              : 
     442              : 
     443            5 : LUA_API const void *lua_topointer (lua_State *L, int idx) {
     444            5 :   StkId o = index2addr(L, idx);
     445            5 :   switch (ttype(o)) {
     446            1 :     case LUA_TTABLE: return hvalue(o);
     447            1 :     case LUA_TLCL: return clLvalue(o);
     448            0 :     case LUA_TCCL: return clCvalue(o);
     449            2 :     case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
     450            1 :     case LUA_TTHREAD: return thvalue(o);
     451            0 :     case LUA_TUSERDATA:
     452              :     case LUA_TLIGHTUSERDATA:
     453            0 :       return lua_touserdata(L, idx);
     454            0 :     default: return NULL;
     455              :   }
     456              : }
     457              : 
     458              : 
     459              : 
     460              : /*
     461              : ** push functions (C -> stack)
     462              : */
     463              : 
     464              : 
     465         1230 : LUA_API void lua_pushnil (lua_State *L) {
     466              :   lua_lock(L);
     467         1230 :   setnilvalue(L->top);
     468         1230 :   api_incr_top(L);
     469              :   lua_unlock(L);
     470         1230 : }
     471              : 
     472              : 
     473         1541 : LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
     474              :   lua_lock(L);
     475         1541 :   setnvalue(L->top, n);
     476              :   luai_checknum(L, L->top,
     477              :     luaG_runerror(L, "C API - attempt to push a signaling NaN"));
     478         1541 :   api_incr_top(L);
     479              :   lua_unlock(L);
     480         1541 : }
     481              : 
     482              : 
     483        52699 : LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
     484              :   lua_lock(L);
     485        52699 :   setnvalue(L->top, cast_num(n));
     486        52699 :   api_incr_top(L);
     487              :   lua_unlock(L);
     488        52699 : }
     489              : 
     490              : 
     491           13 : LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
     492              :   lua_Number n;
     493              :   lua_lock(L);
     494           13 :   n = lua_unsigned2number(u);
     495           13 :   setnvalue(L->top, n);
     496           13 :   api_incr_top(L);
     497              :   lua_unlock(L);
     498           13 : }
     499              : 
     500              : 
     501        23092 : LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
     502              :   TString *ts;
     503              :   lua_lock(L);
     504        23092 :   luaC_checkGC(L);
     505        23092 :   ts = luaS_newlstr(L, s, len);
     506        23092 :   setsvalue2s(L, L->top, ts);
     507        23092 :   api_incr_top(L);
     508              :   lua_unlock(L);
     509        23092 :   return getstr(ts);
     510              : }
     511              : 
     512              : 
     513         4705 : LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
     514         4705 :   if (s == NULL) {
     515            5 :     lua_pushnil(L);
     516            5 :     return NULL;
     517              :   }
     518              :   else {
     519              :     TString *ts;
     520              :     lua_lock(L);
     521         4700 :     luaC_checkGC(L);
     522         4700 :     ts = luaS_new(L, s);
     523         4700 :     setsvalue2s(L, L->top, ts);
     524         4700 :     api_incr_top(L);
     525              :     lua_unlock(L);
     526         4700 :     return getstr(ts);
     527              :   }
     528              : }
     529              : 
     530              : 
     531          141 : 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          141 :   luaC_checkGC(L);
     536          141 :   ret = luaO_pushvfstring(L, fmt, argp);
     537              :   lua_unlock(L);
     538          141 :   return ret;
     539              : }
     540              : 
     541              : 
     542         1620 : 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         1620 :   luaC_checkGC(L);
     547         1620 :   va_start(argp, fmt);
     548         1620 :   ret = luaO_pushvfstring(L, fmt, argp);
     549         1620 :   va_end(argp);
     550              :   lua_unlock(L);
     551         1620 :   return ret;
     552              : }
     553              : 
     554              : 
     555        19779 : LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
     556              :   lua_lock(L);
     557        19779 :   if (n == 0) {
     558        19244 :     setfvalue(L->top, fn);
     559              :   }
     560              :   else {
     561              :     Closure *cl;
     562              :     api_checknelems(L, n);
     563              :     api_check(L, n <= MAXUPVAL, "upvalue index too large");
     564          535 :     luaC_checkGC(L);
     565          535 :     cl = luaF_newCclosure(L, n);
     566          535 :     cl->c.f = fn;
     567          535 :     L->top -= n;
     568         1095 :     while (n--)
     569          560 :       setobj2n(L, &cl->c.upvalue[n], L->top + n);
     570          535 :     setclCvalue(L, L->top, cl);
     571              :   }
     572        19779 :   api_incr_top(L);
     573              :   lua_unlock(L);
     574        19779 : }
     575              : 
     576              : 
     577         6757 : LUA_API void lua_pushboolean (lua_State *L, int b) {
     578              :   lua_lock(L);
     579         6757 :   setbvalue(L->top, (b != 0));  /* ensure that true is 1 */
     580         6757 :   api_incr_top(L);
     581              :   lua_unlock(L);
     582         6757 : }
     583              : 
     584              : 
     585          105 : LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
     586              :   lua_lock(L);
     587          105 :   setpvalue(L->top, p);
     588          105 :   api_incr_top(L);
     589              :   lua_unlock(L);
     590          105 : }
     591              : 
     592              : 
     593          233 : LUA_API int lua_pushthread (lua_State *L) {
     594              :   lua_lock(L);
     595          233 :   setthvalue(L, L->top, L);
     596          233 :   api_incr_top(L);
     597              :   lua_unlock(L);
     598          233 :   return (G(L)->mainthread == L);
     599              : }
     600              : 
     601              : 
     602              : 
     603              : /*
     604              : ** get functions (Lua -> stack)
     605              : */
     606              : 
     607              : 
     608         1965 : LUA_API void lua_getglobal (lua_State *L, const char *var) {
     609         1965 :   Table *reg = hvalue(&G(L)->l_registry);
     610              :   const TValue *gt;  /* global table */
     611              :   lua_lock(L);
     612         1965 :   gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
     613         1965 :   setsvalue2s(L, L->top++, luaS_new(L, var));
     614         1965 :   luaV_gettable(L, gt, L->top - 1, L->top - 1);
     615              :   lua_unlock(L);
     616         1965 : }
     617              : 
     618              : 
     619            8 : LUA_API void lua_gettable (lua_State *L, int idx) {
     620              :   StkId t;
     621              :   lua_lock(L);
     622            8 :   t = index2addr(L, idx);
     623            8 :   luaV_gettable(L, t, L->top - 1, L->top - 1);
     624              :   lua_unlock(L);
     625            8 : }
     626              : 
     627              : 
     628         4679 : LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
     629              :   StkId t;
     630              :   lua_lock(L);
     631         4679 :   t = index2addr(L, idx);
     632         4679 :   setsvalue2s(L, L->top, luaS_new(L, k));
     633         4679 :   api_incr_top(L);
     634         4679 :   luaV_gettable(L, t, L->top - 1, L->top - 1);
     635              :   lua_unlock(L);
     636         4679 : }
     637              : 
     638              : 
     639         2749 : LUA_API void lua_rawget (lua_State *L, int idx) {
     640              :   StkId t;
     641              :   lua_lock(L);
     642         2749 :   t = index2addr(L, idx);
     643              :   api_check(L, ttistable(t), "table expected");
     644         2749 :   setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
     645              :   lua_unlock(L);
     646         2749 : }
     647              : 
     648              : 
     649       293686 : LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
     650              :   StkId t;
     651              :   lua_lock(L);
     652       293686 :   t = index2addr(L, idx);
     653              :   api_check(L, ttistable(t), "table expected");
     654       293686 :   setobj2s(L, L->top, luaH_getint(hvalue(t), n));
     655       293686 :   api_incr_top(L);
     656              :   lua_unlock(L);
     657       293686 : }
     658              : 
     659              : 
     660            0 : LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
     661              :   StkId t;
     662              :   TValue k;
     663              :   lua_lock(L);
     664            0 :   t = index2addr(L, idx);
     665              :   api_check(L, ttistable(t), "table expected");
     666            0 :   setpvalue(&k, cast(void *, p));
     667            0 :   setobj2s(L, L->top, luaH_get(hvalue(t), &k));
     668            0 :   api_incr_top(L);
     669              :   lua_unlock(L);
     670            0 : }
     671              : 
     672              : 
     673         1446 : LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
     674              :   Table *t;
     675              :   lua_lock(L);
     676         1446 :   luaC_checkGC(L);
     677         1446 :   t = luaH_new(L);
     678         1446 :   sethvalue(L, L->top, t);
     679         1446 :   api_incr_top(L);
     680         1446 :   if (narray > 0 || nrec > 0)
     681         1101 :     luaH_resize(L, t, narray, nrec);
     682              :   lua_unlock(L);
     683         1446 : }
     684              : 
     685              : 
     686        11789 : LUA_API int lua_getmetatable (lua_State *L, int objindex) {
     687              :   const TValue *obj;
     688        11789 :   Table *mt = NULL;
     689              :   int res;
     690              :   lua_lock(L);
     691        11789 :   obj = index2addr(L, objindex);
     692        11789 :   switch (ttypenv(obj)) {
     693         6089 :     case LUA_TTABLE:
     694         6089 :       mt = hvalue(obj)->metatable;
     695         6089 :       break;
     696         1370 :     case LUA_TUSERDATA:
     697         1370 :       mt = uvalue(obj)->metatable;
     698         1370 :       break;
     699         4330 :     default:
     700         4330 :       mt = G(L)->mt[ttypenv(obj)];
     701         4330 :       break;
     702              :   }
     703        11789 :   if (mt == NULL)
     704         7907 :     res = 0;
     705              :   else {
     706         3882 :     sethvalue(L, L->top, mt);
     707         3882 :     api_incr_top(L);
     708         3882 :     res = 1;
     709              :   }
     710              :   lua_unlock(L);
     711        11789 :   return res;
     712              : }
     713              : 
     714              : 
     715            3 : LUA_API void lua_getuservalue (lua_State *L, int idx) {
     716              :   StkId o;
     717              :   lua_lock(L);
     718            3 :   o = index2addr(L, idx);
     719              :   api_check(L, ttisuserdata(o), "userdata expected");
     720            3 :   if (uvalue(o)->env) {
     721            1 :     sethvalue(L, L->top, uvalue(o)->env);
     722              :   } else
     723            2 :     setnilvalue(L->top);
     724            3 :   api_incr_top(L);
     725              :   lua_unlock(L);
     726            3 : }
     727              : 
     728              : 
     729              : /*
     730              : ** set functions (stack -> Lua)
     731              : */
     732              : 
     733              : 
     734         1057 : LUA_API void lua_setglobal (lua_State *L, const char *var) {
     735         1057 :   Table *reg = hvalue(&G(L)->l_registry);
     736              :   const TValue *gt;  /* global table */
     737              :   lua_lock(L);
     738              :   api_checknelems(L, 1);
     739         1057 :   gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
     740         1057 :   setsvalue2s(L, L->top++, luaS_new(L, var));
     741         1057 :   luaV_settable(L, gt, L->top - 1, L->top - 2);
     742         1057 :   L->top -= 2;  /* pop value and key */
     743              :   lua_unlock(L);
     744         1057 : }
     745              : 
     746              : 
     747            2 : LUA_API void lua_settable (lua_State *L, int idx) {
     748              :   StkId t;
     749              :   lua_lock(L);
     750              :   api_checknelems(L, 2);
     751            2 :   t = index2addr(L, idx);
     752            2 :   luaV_settable(L, t, L->top - 2, L->top - 1);
     753            2 :   L->top -= 2;  /* pop index and value */
     754              :   lua_unlock(L);
     755            2 : }
     756              : 
     757              : 
     758        15206 : LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
     759              :   StkId t;
     760              :   lua_lock(L);
     761              :   api_checknelems(L, 1);
     762        15206 :   t = index2addr(L, idx);
     763        15206 :   setsvalue2s(L, L->top++, luaS_new(L, k));
     764        15206 :   luaV_settable(L, t, L->top - 1, L->top - 2);
     765        15206 :   L->top -= 2;  /* pop value and key */
     766              :   lua_unlock(L);
     767        15206 : }
     768              : 
     769              : 
     770            5 : LUA_API void lua_rawset (lua_State *L, int idx) {
     771              :   StkId t;
     772              :   lua_lock(L);
     773              :   api_checknelems(L, 2);
     774            5 :   t = index2addr(L, idx);
     775              :   api_check(L, ttistable(t), "table expected");
     776            5 :   setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
     777            4 :   invalidateTMcache(hvalue(t));
     778            4 :   luaC_barrierback(L, gcvalue(t), L->top-1);
     779            4 :   L->top -= 2;
     780              :   lua_unlock(L);
     781            4 : }
     782              : 
     783              : 
     784        86683 : LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
     785              :   StkId t;
     786              :   lua_lock(L);
     787              :   api_checknelems(L, 1);
     788        86683 :   t = index2addr(L, idx);
     789              :   api_check(L, ttistable(t), "table expected");
     790        86683 :   luaH_setint(L, hvalue(t), n, L->top - 1);
     791        86683 :   luaC_barrierback(L, gcvalue(t), L->top-1);
     792        86683 :   L->top--;
     793              :   lua_unlock(L);
     794        86683 : }
     795              : 
     796              : 
     797            0 : LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
     798              :   StkId t;
     799              :   TValue k;
     800              :   lua_lock(L);
     801              :   api_checknelems(L, 1);
     802            0 :   t = index2addr(L, idx);
     803              :   api_check(L, ttistable(t), "table expected");
     804            0 :   setpvalue(&k, cast(void *, p));
     805            0 :   setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
     806            0 :   luaC_barrierback(L, gcvalue(t), L->top - 1);
     807            0 :   L->top--;
     808              :   lua_unlock(L);
     809            0 : }
     810              : 
     811              : 
     812          658 : LUA_API int lua_setmetatable (lua_State *L, int objindex) {
     813              :   TValue *obj;
     814              :   Table *mt;
     815              :   lua_lock(L);
     816              :   api_checknelems(L, 1);
     817          658 :   obj = index2addr(L, objindex);
     818          658 :   if (ttisnil(L->top - 1))
     819            2 :     mt = NULL;
     820              :   else {
     821              :     api_check(L, ttistable(L->top - 1), "table expected");
     822          656 :     mt = hvalue(L->top - 1);
     823              :   }
     824          658 :   switch (ttypenv(obj)) {
     825          186 :     case LUA_TTABLE: {
     826          186 :       hvalue(obj)->metatable = mt;
     827          186 :       if (mt) {
     828          184 :         luaC_objbarrierback(L, gcvalue(obj), mt);
     829          184 :         luaC_checkfinalizer(L, gcvalue(obj), mt);
     830              :       }
     831          186 :       break;
     832              :     }
     833          384 :     case LUA_TUSERDATA: {
     834          384 :       uvalue(obj)->metatable = mt;
     835          384 :       if (mt) {
     836          384 :         luaC_objbarrier(L, rawuvalue(obj), mt);
     837          384 :         luaC_checkfinalizer(L, gcvalue(obj), mt);
     838              :       }
     839          384 :       break;
     840              :     }
     841           88 :     default: {
     842           88 :       G(L)->mt[ttypenv(obj)] = mt;
     843           88 :       break;
     844              :     }
     845              :   }
     846          658 :   L->top--;
     847              :   lua_unlock(L);
     848          658 :   return 1;
     849              : }
     850              : 
     851              : 
     852            2 : LUA_API void lua_setuservalue (lua_State *L, int idx) {
     853              :   StkId o;
     854              :   lua_lock(L);
     855              :   api_checknelems(L, 1);
     856            2 :   o = index2addr(L, idx);
     857              :   api_check(L, ttisuserdata(o), "userdata expected");
     858            2 :   if (ttisnil(L->top - 1))
     859            1 :     uvalue(o)->env = NULL;
     860              :   else {
     861              :     api_check(L, ttistable(L->top - 1), "table expected");
     862            1 :     uvalue(o)->env = hvalue(L->top - 1);
     863            1 :     luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
     864              :   }
     865            2 :   L->top--;
     866              :   lua_unlock(L);
     867            2 : }
     868              : 
     869              : 
     870              : /*
     871              : ** `load' and `call' functions (run Lua code)
     872              : */
     873              : 
     874              : 
     875              : #define checkresults(L,na,nr) \
     876              :      api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
     877              :         "results from function overflow current stack size")
     878              : 
     879              : 
     880            2 : LUA_API int lua_getctx (lua_State *L, int *ctx) {
     881            2 :   if (L->ci->callstatus & CIST_YIELDED) {
     882            2 :     if (ctx) *ctx = L->ci->u.c.ctx;
     883            2 :     return L->ci->u.c.status;
     884              :   }
     885            0 :   else return LUA_OK;
     886              : }
     887              : 
     888              : 
     889         3553 : LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
     890              :                         lua_CFunction k) {
     891              :   StkId func;
     892              :   lua_lock(L);
     893              :   api_check(L, k == NULL || !isLua(L->ci),
     894              :     "cannot use continuations inside hooks");
     895              :   api_checknelems(L, nargs+1);
     896              :   api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
     897              :   checkresults(L, nargs, nresults);
     898         3553 :   func = L->top - (nargs+1);
     899         3553 :   if (k != NULL && L->nny == 0) {  /* need to prepare continuation? */
     900            0 :     L->ci->u.c.k = k;  /* save continuation */
     901            0 :     L->ci->u.c.ctx = ctx;  /* save context */
     902            0 :     luaD_call(L, func, nresults, 1);  /* do the call */
     903              :   }
     904              :   else  /* no continuation or no yieldable */
     905         3553 :     luaD_call(L, func, nresults, 0);  /* just do the call */
     906         3551 :   adjustresults(L, nresults);
     907              :   lua_unlock(L);
     908         3551 : }
     909              : 
     910              : 
     911              : 
     912              : /*
     913              : ** Execute a protected call.
     914              : */
     915              : struct CallS {  /* data to `f_call' */
     916              :   StkId func;
     917              :   int nresults;
     918              : };
     919              : 
     920              : 
     921          842 : static void f_call (lua_State *L, void *ud) {
     922          842 :   struct CallS *c = cast(struct CallS *, ud);
     923          842 :   luaD_call(L, c->func, c->nresults, 0);
     924          469 : }
     925              : 
     926              : 
     927              : 
     928          844 : LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
     929              :                         int ctx, lua_CFunction k) {
     930              :   struct CallS c;
     931              :   int status;
     932              :   ptrdiff_t func;
     933              :   lua_lock(L);
     934              :   api_check(L, k == NULL || !isLua(L->ci),
     935              :     "cannot use continuations inside hooks");
     936              :   api_checknelems(L, nargs+1);
     937              :   api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
     938              :   checkresults(L, nargs, nresults);
     939          844 :   if (errfunc == 0)
     940          614 :     func = 0;
     941              :   else {
     942          230 :     StkId o = index2addr(L, errfunc);
     943              :     api_checkstackindex(L, errfunc, o);
     944          230 :     func = savestack(L, o);
     945              :   }
     946          844 :   c.func = L->top - (nargs+1);  /* function to be called */
     947          844 :   if (k == NULL || L->nny > 0) {  /* no continuation or no yieldable? */
     948          842 :     c.nresults = nresults;  /* do a 'conventional' protected call */
     949          842 :     status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
     950              :   }
     951              :   else {  /* prepare continuation (call is already protected by 'resume') */
     952            2 :     CallInfo *ci = L->ci;
     953            2 :     ci->u.c.k = k;  /* save continuation */
     954            2 :     ci->u.c.ctx = ctx;  /* save context */
     955              :     /* save information for error recovery */
     956            2 :     ci->extra = savestack(L, c.func);
     957            2 :     ci->u.c.old_allowhook = L->allowhook;
     958            2 :     ci->u.c.old_errfunc = L->errfunc;
     959            2 :     L->errfunc = func;
     960              :     /* mark that function may do error recovery */
     961            2 :     ci->callstatus |= CIST_YPCALL;
     962            2 :     luaD_call(L, c.func, nresults, 1);  /* do the call */
     963            0 :     ci->callstatus &= ~CIST_YPCALL;
     964            0 :     L->errfunc = ci->u.c.old_errfunc;
     965            0 :     status = LUA_OK;  /* if it is here, there were no errors */
     966              :   }
     967          815 :   adjustresults(L, nresults);
     968              :   lua_unlock(L);
     969          815 :   return status;
     970              : }
     971              : 
     972              : 
     973          513 : LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
     974              :                       const char *chunkname, const char *mode) {
     975              :   ZIO z;
     976              :   int status;
     977              :   lua_lock(L);
     978          513 :   if (!chunkname) chunkname = "?";
     979          513 :   luaZ_init(L, &z, reader, data);
     980          513 :   status = luaD_protectedparser(L, &z, chunkname, mode);
     981          513 :   if (status == LUA_OK) {  /* no errors? */
     982          465 :     LClosure *f = clLvalue(L->top - 1);  /* get newly created function */
     983          465 :     if (f->nupvalues == 1) {  /* does it have one upvalue? */
     984              :       /* get global table from registry */
     985          464 :       Table *reg = hvalue(&G(L)->l_registry);
     986          464 :       const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
     987              :       /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
     988          464 :       setobj(L, f->upvals[0]->v, gt);
     989          464 :       luaC_barrier(L, f->upvals[0], gt);
     990              :     }
     991              :   }
     992              :   lua_unlock(L);
     993          513 :   return status;
     994              : }
     995              : 
     996              : 
     997            3 : LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
     998              :   int status;
     999              :   TValue *o;
    1000              :   lua_lock(L);
    1001              :   api_checknelems(L, 1);
    1002            3 :   o = L->top - 1;
    1003            3 :   if (isLfunction(o))
    1004            2 :     status = luaU_dump(L, getproto(o), writer, data, 0);
    1005              :   else
    1006            1 :     status = 1;
    1007              :   lua_unlock(L);
    1008            3 :   return status;
    1009              : }
    1010              : 
    1011              : 
    1012         5975 : LUA_API int lua_status (lua_State *L) {
    1013         5975 :   return L->status;
    1014              : }
    1015              : 
    1016              : 
    1017              : /*
    1018              : ** Garbage-collection function
    1019              : */
    1020              : 
    1021          193 : LUA_API int lua_gc (lua_State *L, int what, int data) {
    1022          193 :   int res = 0;
    1023              :   global_State *g;
    1024              :   lua_lock(L);
    1025          193 :   g = G(L);
    1026          193 :   switch (what) {
    1027           87 :     case LUA_GCSTOP: {
    1028           87 :       g->gcrunning = 0;
    1029           87 :       break;
    1030              :     }
    1031           87 :     case LUA_GCRESTART: {
    1032           87 :       luaE_setdebt(g, 0);
    1033           87 :       g->gcrunning = 1;
    1034           87 :       break;
    1035              :     }
    1036            7 :     case LUA_GCCOLLECT: {
    1037            7 :       luaC_fullgc(L, 0);
    1038            7 :       break;
    1039              :     }
    1040            1 :     case LUA_GCCOUNT: {
    1041              :       /* GC values are expressed in Kbytes: #bytes/2^10 */
    1042            1 :       res = cast_int(gettotalbytes(g) >> 10);
    1043            1 :       break;
    1044              :     }
    1045            1 :     case LUA_GCCOUNTB: {
    1046            1 :       res = cast_int(gettotalbytes(g) & 0x3ff);
    1047            1 :       break;
    1048              :     }
    1049            3 :     case LUA_GCSTEP: {
    1050            3 :       if (g->gckind == KGC_GEN) {  /* generational mode? */
    1051            1 :         res = (g->GCestimate == 0);  /* true if it will do major collection */
    1052            1 :         luaC_forcestep(L);  /* do a single step */
    1053              :       }
    1054              :       else {
    1055            2 :        lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE;
    1056            2 :        if (g->gcrunning)
    1057            1 :          debt += g->GCdebt;  /* include current debt */
    1058            2 :        luaE_setdebt(g, debt);
    1059            2 :        luaC_forcestep(L);
    1060            2 :        if (g->gcstate == GCSpause)  /* end of cycle? */
    1061            0 :          res = 1;  /* signal it */
    1062              :       }
    1063            3 :       break;
    1064              :     }
    1065            1 :     case LUA_GCSETPAUSE: {
    1066            1 :       res = g->gcpause;
    1067            1 :       g->gcpause = data;
    1068            1 :       break;
    1069              :     }
    1070            1 :     case LUA_GCSETMAJORINC: {
    1071            1 :       res = g->gcmajorinc;
    1072            1 :       g->gcmajorinc = data;
    1073            1 :       break;
    1074              :     }
    1075            1 :     case LUA_GCSETSTEPMUL: {
    1076            1 :       res = g->gcstepmul;
    1077            1 :       g->gcstepmul = data;
    1078            1 :       break;
    1079              :     }
    1080            2 :     case LUA_GCISRUNNING: {
    1081            2 :       res = g->gcrunning;
    1082            2 :       break;
    1083              :     }
    1084            1 :     case LUA_GCGEN: {  /* change collector to generational mode */
    1085            1 :       luaC_changemode(L, KGC_GEN);
    1086            1 :       break;
    1087              :     }
    1088            1 :     case LUA_GCINC: {  /* change collector to incremental mode */
    1089            1 :       luaC_changemode(L, KGC_NORMAL);
    1090            1 :       break;
    1091              :     }
    1092            0 :     default: res = -1;  /* invalid option */
    1093              :   }
    1094              :   lua_unlock(L);
    1095          193 :   return res;
    1096              : }
    1097              : 
    1098              : 
    1099              : 
    1100              : /*
    1101              : ** miscellaneous functions
    1102              : */
    1103              : 
    1104              : 
    1105          157 : LUA_API int lua_error (lua_State *L) {
    1106              :   lua_lock(L);
    1107              :   api_checknelems(L, 1);
    1108          157 :   luaG_errormsg(L);
    1109              :   /* code unreachable; will unlock when control actually leaves the kernel */
    1110              :   return 0;  /* to avoid warnings */
    1111              : }
    1112              : 
    1113              : 
    1114         1112 : LUA_API int lua_next (lua_State *L, int idx) {
    1115              :   StkId t;
    1116              :   int more;
    1117              :   lua_lock(L);
    1118         1112 :   t = index2addr(L, idx);
    1119              :   api_check(L, ttistable(t), "table expected");
    1120         1112 :   more = luaH_next(L, hvalue(t), L->top - 1);
    1121         1111 :   if (more) {
    1122         1045 :     api_incr_top(L);
    1123              :   }
    1124              :   else  /* no more elements */
    1125           66 :     L->top -= 1;  /* remove key */
    1126              :   lua_unlock(L);
    1127         1111 :   return more;
    1128              : }
    1129              : 
    1130              : 
    1131          164 : LUA_API void lua_concat (lua_State *L, int n) {
    1132              :   lua_lock(L);
    1133              :   api_checknelems(L, n);
    1134          164 :   if (n >= 2) {
    1135          160 :     luaC_checkGC(L);
    1136          160 :     luaV_concat(L, n);
    1137              :   }
    1138            4 :   else if (n == 0) {  /* push empty string */
    1139            0 :     setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
    1140            0 :     api_incr_top(L);
    1141              :   }
    1142              :   /* else n == 1; nothing to do */
    1143              :   lua_unlock(L);
    1144          164 : }
    1145              : 
    1146              : 
    1147        18039 : LUA_API void lua_len (lua_State *L, int idx) {
    1148              :   StkId t;
    1149              :   lua_lock(L);
    1150        18039 :   t = index2addr(L, idx);
    1151        18039 :   luaV_objlen(L, L->top, t);
    1152        18039 :   api_incr_top(L);
    1153              :   lua_unlock(L);
    1154        18039 : }
    1155              : 
    1156              : 
    1157            0 : LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
    1158              :   lua_Alloc f;
    1159              :   lua_lock(L);
    1160            0 :   if (ud) *ud = G(L)->ud;
    1161            0 :   f = G(L)->frealloc;
    1162              :   lua_unlock(L);
    1163            0 :   return f;
    1164              : }
    1165              : 
    1166              : 
    1167            0 : LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
    1168              :   lua_lock(L);
    1169            0 :   G(L)->ud = ud;
    1170            0 :   G(L)->frealloc = f;
    1171              :   lua_unlock(L);
    1172            0 : }
    1173              : 
    1174              : 
    1175          389 : LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
    1176              :   Udata *u;
    1177              :   lua_lock(L);
    1178          389 :   luaC_checkGC(L);
    1179          389 :   u = luaS_newudata(L, size, NULL);
    1180          389 :   setuvalue(L, L->top, u);
    1181          389 :   api_incr_top(L);
    1182              :   lua_unlock(L);
    1183          389 :   return u + 1;
    1184              : }
    1185              : 
    1186              : 
    1187              : 
    1188            7 : static const char *aux_upvalue (StkId fi, int n, TValue **val,
    1189              :                                 GCObject **owner) {
    1190            7 :   switch (ttype(fi)) {
    1191            0 :     case LUA_TCCL: {  /* C closure */
    1192            0 :       CClosure *f = clCvalue(fi);
    1193            0 :       if (!(1 <= n && n <= f->nupvalues)) return NULL;
    1194            0 :       *val = &f->upvalue[n-1];
    1195            0 :       if (owner) *owner = obj2gco(f);
    1196            0 :       return "";
    1197              :     }
    1198            7 :     case LUA_TLCL: {  /* Lua closure */
    1199            7 :       LClosure *f = clLvalue(fi);
    1200              :       TString *name;
    1201            7 :       Proto *p = f->p;
    1202            7 :       if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
    1203            6 :       *val = f->upvals[n-1]->v;
    1204            6 :       if (owner) *owner = obj2gco(f->upvals[n - 1]);
    1205            6 :       name = p->upvalues[n-1].name;
    1206            6 :       return (name == NULL) ? "" : getstr(name);
    1207              :     }
    1208            0 :     default: return NULL;  /* not a closure */
    1209              :   }
    1210              : }
    1211              : 
    1212              : 
    1213            1 : LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
    1214              :   const char *name;
    1215            1 :   TValue *val = NULL;  /* to avoid warnings */
    1216              :   lua_lock(L);
    1217            1 :   name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL);
    1218            1 :   if (name) {
    1219            1 :     setobj2s(L, L->top, val);
    1220            1 :     api_incr_top(L);
    1221              :   }
    1222              :   lua_unlock(L);
    1223            1 :   return name;
    1224              : }
    1225              : 
    1226              : 
    1227            6 : LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
    1228              :   const char *name;
    1229            6 :   TValue *val = NULL;  /* to avoid warnings */
    1230            6 :   GCObject *owner = NULL;  /* to avoid warnings */
    1231              :   StkId fi;
    1232              :   lua_lock(L);
    1233            6 :   fi = index2addr(L, funcindex);
    1234              :   api_checknelems(L, 1);
    1235            6 :   name = aux_upvalue(fi, n, &val, &owner);
    1236            6 :   if (name) {
    1237            5 :     L->top--;
    1238            5 :     setobj(L, val, L->top);
    1239            5 :     luaC_barrier(L, owner, L->top);
    1240              :   }
    1241              :   lua_unlock(L);
    1242            6 :   return name;
    1243              : }
    1244              : 
    1245              : 
    1246            3 : static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
    1247              :   LClosure *f;
    1248            3 :   StkId fi = index2addr(L, fidx);
    1249              :   api_check(L, ttisLclosure(fi), "Lua function expected");
    1250            3 :   f = clLvalue(fi);
    1251              :   api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
    1252            3 :   if (pf) *pf = f;
    1253            3 :   return &f->upvals[n - 1];  /* get its upvalue pointer */
    1254              : }
    1255              : 
    1256              : 
    1257            1 : LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
    1258            1 :   StkId fi = index2addr(L, fidx);
    1259            1 :   switch (ttype(fi)) {
    1260            1 :     case LUA_TLCL: {  /* lua closure */
    1261            1 :       return *getupvalref(L, fidx, n, NULL);
    1262              :     }
    1263            0 :     case LUA_TCCL: {  /* C closure */
    1264            0 :       CClosure *f = clCvalue(fi);
    1265              :       api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
    1266            0 :       return &f->upvalue[n - 1];
    1267              :     }
    1268            0 :     default: {
    1269              :       api_check(L, 0, "closure expected");
    1270            0 :       return NULL;
    1271              :     }
    1272              :   }
    1273              : }
    1274              : 
    1275              : 
    1276            1 : LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
    1277              :                                             int fidx2, int n2) {
    1278              :   LClosure *f1;
    1279            1 :   UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
    1280            1 :   UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
    1281            1 :   *up1 = *up2;
    1282            1 :   luaC_objbarrier(L, f1, *up2);
    1283            1 : }
    1284              : 
        

Generated by: LCOV version 2.0-1