LCOV - code coverage report
Current view: top level - external/lua/src - ltm.c (source / functions) Hit Total Coverage
Test: rapport Lines: 37 124 29.8 %
Date: 2021-12-10 16:22:55 Functions: 5 15 33.3 %
Branches: 13 61 21.3 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            : ** $Id: ltm.c $
       3                 :            : ** Tag methods
       4                 :            : ** See Copyright Notice in lua.h
       5                 :            : */
       6                 :            : 
       7                 :            : #define ltm_c
       8                 :            : #define LUA_CORE
       9                 :            : 
      10                 :            : #include "lprefix.h"
      11                 :            : 
      12                 :            : 
      13                 :            : #include <string.h>
      14                 :            : 
      15                 :            : #include "lua.h"
      16                 :            : 
      17                 :            : #include "ldebug.h"
      18                 :            : #include "ldo.h"
      19                 :            : #include "lgc.h"
      20                 :            : #include "lobject.h"
      21                 :            : #include "lstate.h"
      22                 :            : #include "lstring.h"
      23                 :            : #include "ltable.h"
      24                 :            : #include "ltm.h"
      25                 :            : #include "lvm.h"
      26                 :            : 
      27                 :            : 
      28                 :            : static const char udatatypename[] = "userdata";
      29                 :            : 
      30                 :            : LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = {
      31                 :            :   "no value",
      32                 :            :   "nil", "boolean", udatatypename, "number",
      33                 :            :   "string", "table", "function", udatatypename, "thread",
      34                 :            :   "upvalue", "proto" /* these last cases are used for tests only */
      35                 :            : };
      36                 :            : 
      37                 :            : 
      38                 :        810 : void luaT_init (lua_State *L) {
      39                 :            :   static const char *const luaT_eventname[] = {  /* ORDER TM */
      40                 :            :     "__index", "__newindex",
      41                 :            :     "__gc", "__mode", "__len", "__eq",
      42                 :            :     "__add", "__sub", "__mul", "__mod", "__pow",
      43                 :            :     "__div", "__idiv",
      44                 :            :     "__band", "__bor", "__bxor", "__shl", "__shr",
      45                 :            :     "__unm", "__bnot", "__lt", "__le",
      46                 :            :     "__concat", "__call", "__close"
      47                 :            :   };
      48                 :            :   int i;
      49         [ +  + ]:      21060 :   for (i=0; i<TM_N; i++) {
      50                 :      20250 :     G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
      51                 :      20250 :     luaC_fix(L, obj2gco(G(L)->tmname[i]));  /* never collect these names */
      52                 :      20250 :   }
      53                 :        810 : }
      54                 :            : 
      55                 :            : 
      56                 :            : /*
      57                 :            : ** function to be used with macro "fasttm": optimized for absence of
      58                 :            : ** tag methods
      59                 :            : */
      60                 :       4109 : const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
      61                 :       4109 :   const TValue *tm = luaH_getshortstr(events, ename);
      62                 :            :   lua_assert(event <= TM_EQ);
      63         [ +  + ]:       4109 :   if (notm(tm)) {  /* no tag method? */
      64                 :        850 :     events->flags |= cast_byte(1u<<event);  /* cache this fact */
      65                 :        850 :     return NULL;
      66                 :            :   }
      67                 :       3259 :   else return tm;
      68                 :       4109 : }
      69                 :            : 
      70                 :            : 
      71                 :        196 : const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
      72                 :            :   Table *mt;
      73      [ +  +  + ]:        196 :   switch (ttype(o)) {
      74                 :            :     case LUA_TTABLE:
      75                 :         40 :       mt = hvalue(o)->metatable;
      76                 :         40 :       break;
      77                 :            :     case LUA_TUSERDATA:
      78                 :        153 :       mt = uvalue(o)->metatable;
      79                 :        153 :       break;
      80                 :            :     default:
      81                 :          3 :       mt = G(L)->mt[ttype(o)];
      82                 :          3 :   }
      83         [ +  + ]:        196 :   return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue);
      84                 :            : }
      85                 :            : 
      86                 :            : 
      87                 :            : /*
      88                 :            : ** Return the name of the type of an object. For tables and userdata
      89                 :            : ** with metatable, use their '__name' metafield, if present.
      90                 :            : */
      91                 :          3 : const char *luaT_objtypename (lua_State *L, const TValue *o) {
      92                 :            :   Table *mt;
      93   [ -  +  #  # ]:          3 :   if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
      94         [ -  + ]:          3 :       (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
      95                 :          0 :     const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
      96         [ #  # ]:          0 :     if (ttisstring(name))  /* is '__name' a string? */
      97                 :          0 :       return getstr(tsvalue(name));  /* use it as type name */
      98                 :          0 :   }
      99                 :          3 :   return ttypename(ttype(o));  /* else use standard type name */
     100                 :          3 : }
     101                 :            : 
     102                 :            : 
     103                 :          0 : void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
     104                 :            :                   const TValue *p2, const TValue *p3) {
     105                 :          0 :   StkId func = L->top;
     106                 :          0 :   setobj2s(L, func, f);  /* push function (assume EXTRA_STACK) */
     107                 :          0 :   setobj2s(L, func + 1, p1);  /* 1st argument */
     108                 :          0 :   setobj2s(L, func + 2, p2);  /* 2nd argument */
     109                 :          0 :   setobj2s(L, func + 3, p3);  /* 3rd argument */
     110                 :          0 :   L->top = func + 4;
     111                 :            :   /* metamethod may yield only when called from Lua code */
     112         [ #  # ]:          0 :   if (isLuacode(L->ci))
     113                 :          0 :     luaD_call(L, func, 0);
     114                 :            :   else
     115                 :          0 :     luaD_callnoyield(L, func, 0);
     116                 :          0 : }
     117                 :            : 
     118                 :            : 
     119                 :          0 : void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
     120                 :            :                      const TValue *p2, StkId res) {
     121                 :          0 :   ptrdiff_t result = savestack(L, res);
     122                 :          0 :   StkId func = L->top;
     123                 :          0 :   setobj2s(L, func, f);  /* push function (assume EXTRA_STACK) */
     124                 :          0 :   setobj2s(L, func + 1, p1);  /* 1st argument */
     125                 :          0 :   setobj2s(L, func + 2, p2);  /* 2nd argument */
     126                 :          0 :   L->top += 3;
     127                 :            :   /* metamethod may yield only when called from Lua code */
     128         [ #  # ]:          0 :   if (isLuacode(L->ci))
     129                 :          0 :     luaD_call(L, func, 1);
     130                 :            :   else
     131                 :          0 :     luaD_callnoyield(L, func, 1);
     132                 :          0 :   res = restorestack(L, result);
     133                 :          0 :   setobjs2s(L, res, --L->top);  /* move result to its place */
     134                 :          0 : }
     135                 :            : 
     136                 :            : 
     137                 :          0 : static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
     138                 :            :                       StkId res, TMS event) {
     139                 :          0 :   const TValue *tm = luaT_gettmbyobj(L, p1, event);  /* try first operand */
     140         [ #  # ]:          0 :   if (notm(tm))
     141                 :          0 :     tm = luaT_gettmbyobj(L, p2, event);  /* try second operand */
     142         [ #  # ]:          0 :   if (notm(tm)) return 0;
     143                 :          0 :   luaT_callTMres(L, tm, p1, p2, res);
     144                 :          0 :   return 1;
     145                 :          0 : }
     146                 :            : 
     147                 :            : 
     148                 :          0 : void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
     149                 :            :                     StkId res, TMS event) {
     150         [ #  # ]:          0 :   if (!callbinTM(L, p1, p2, res, event)) {
     151         [ #  # ]:          0 :     switch (event) {
     152                 :            :       case TM_BAND: case TM_BOR: case TM_BXOR:
     153                 :            :       case TM_SHL: case TM_SHR: case TM_BNOT: {
     154   [ #  #  #  # ]:          0 :         if (ttisnumber(p1) && ttisnumber(p2))
     155                 :          0 :           luaG_tointerror(L, p1, p2);
     156                 :            :         else
     157                 :          0 :           luaG_opinterror(L, p1, p2, "perform bitwise operation on");
     158                 :            :       }
     159                 :            :       /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
     160                 :            :       default:
     161                 :          0 :         luaG_opinterror(L, p1, p2, "perform arithmetic on");
     162                 :            :     }
     163                 :            :   }
     164                 :          0 : }
     165                 :            : 
     166                 :            : 
     167                 :          0 : void luaT_tryconcatTM (lua_State *L) {
     168                 :          0 :   StkId top = L->top;
     169         [ #  # ]:          0 :   if (!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, TM_CONCAT))
     170                 :          0 :     luaG_concaterror(L, s2v(top - 2), s2v(top - 1));
     171                 :          0 : }
     172                 :            : 
     173                 :            : 
     174                 :          0 : void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
     175                 :            :                                        int flip, StkId res, TMS event) {
     176         [ #  # ]:          0 :   if (flip)
     177                 :          0 :     luaT_trybinTM(L, p2, p1, res, event);
     178                 :            :   else
     179                 :          0 :     luaT_trybinTM(L, p1, p2, res, event);
     180                 :          0 : }
     181                 :            : 
     182                 :            : 
     183                 :          0 : void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
     184                 :            :                                    int flip, StkId res, TMS event) {
     185                 :            :   TValue aux;
     186                 :          0 :   setivalue(&aux, i2);
     187                 :          0 :   luaT_trybinassocTM(L, p1, &aux, flip, res, event);
     188                 :          0 : }
     189                 :            : 
     190                 :            : 
     191                 :            : /*
     192                 :            : ** Calls an order tag method.
     193                 :            : ** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old
     194                 :            : ** behavior: if there is no '__le', try '__lt', based on l <= r iff
     195                 :            : ** !(r < l) (assuming a total order). If the metamethod yields during
     196                 :            : ** this substitution, the continuation has to know about it (to negate
     197                 :            : ** the result of r<l); bit CIST_LEQ in the call status keeps that
     198                 :            : ** information.
     199                 :            : */
     200                 :          0 : int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
     201                 :            :                       TMS event) {
     202         [ #  # ]:          0 :   if (callbinTM(L, p1, p2, L->top, event))  /* try original event */
     203         [ #  # ]:          0 :     return !l_isfalse(s2v(L->top));
     204                 :            : #if defined(LUA_COMPAT_LT_LE)
     205                 :            :   else if (event == TM_LE) {
     206                 :            :       /* try '!(p2 < p1)' for '(p1 <= p2)' */
     207                 :            :       L->ci->callstatus |= CIST_LEQ;  /* mark it is doing 'lt' for 'le' */
     208                 :            :       if (callbinTM(L, p2, p1, L->top, TM_LT)) {
     209                 :            :         L->ci->callstatus ^= CIST_LEQ;  /* clear mark */
     210                 :            :         return l_isfalse(s2v(L->top));
     211                 :            :       }
     212                 :            :       /* else error will remove this 'ci'; no need to clear mark */
     213                 :            :   }
     214                 :            : #endif
     215                 :          0 :   luaG_ordererror(L, p1, p2);  /* no metamethod found */
     216                 :            :   return 0;  /* to avoid warnings */
     217                 :            : }
     218                 :            : 
     219                 :            : 
     220                 :          0 : int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
     221                 :            :                        int flip, int isfloat, TMS event) {
     222                 :            :   TValue aux; const TValue *p2;
     223         [ #  # ]:          0 :   if (isfloat) {
     224                 :          0 :     setfltvalue(&aux, cast_num(v2));
     225                 :          0 :   }
     226                 :            :   else
     227                 :          0 :     setivalue(&aux, v2);
     228         [ #  # ]:          0 :   if (flip) {  /* arguments were exchanged? */
     229                 :          0 :     p2 = p1; p1 = &aux;  /* correct them */
     230                 :          0 :   }
     231                 :            :   else
     232                 :          0 :     p2 = &aux;
     233                 :          0 :   return luaT_callorderTM(L, p1, p2, event);
     234                 :            : }
     235                 :            : 
     236                 :            : 
     237                 :        652 : void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci,
     238                 :            :                          const Proto *p) {
     239                 :            :   int i;
     240                 :        652 :   int actual = cast_int(L->top - ci->func) - 1;  /* number of arguments */
     241                 :        652 :   int nextra = actual - nfixparams;  /* number of extra arguments */
     242                 :        652 :   ci->u.l.nextraargs = nextra;
     243         [ -  + ]:        652 :   luaD_checkstack(L, p->maxstacksize + 1);
     244                 :            :   /* copy function to the top of the stack */
     245                 :        652 :   setobjs2s(L, L->top++, ci->func);
     246                 :            :   /* move fixed parameters to the top of the stack */
     247         [ -  + ]:        652 :   for (i = 1; i <= nfixparams; i++) {
     248                 :          0 :     setobjs2s(L, L->top++, ci->func + i);
     249                 :          0 :     setnilvalue(s2v(ci->func + i));  /* erase original parameter (for GC) */
     250                 :          0 :   }
     251                 :        652 :   ci->func += actual + 1;
     252                 :        652 :   ci->top += actual + 1;
     253                 :            :   lua_assert(L->top <= ci->top && ci->top <= L->stack_last);
     254                 :        652 : }
     255                 :            : 
     256                 :            : 
     257                 :          0 : void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
     258                 :            :   int i;
     259                 :          0 :   int nextra = ci->u.l.nextraargs;
     260         [ #  # ]:          0 :   if (wanted < 0) {
     261                 :          0 :     wanted = nextra;  /* get all extra arguments available */
     262   [ #  #  #  # ]:          0 :     checkstackGCp(L, nextra, where);  /* ensure stack space */
     263                 :          0 :     L->top = where + nextra;  /* next instruction will need top */
     264                 :          0 :   }
     265   [ #  #  #  # ]:          0 :   for (i = 0; i < wanted && i < nextra; i++)
     266                 :          0 :     setobjs2s(L, where + i, ci->func - nextra + i);
     267         [ #  # ]:          0 :   for (; i < wanted; i++)   /* complete required results with nil */
     268                 :          0 :     setnilvalue(s2v(where + i));
     269                 :          0 : }
     270                 :            : 

Generated by: LCOV version 1.15