Coverage Report

Created: 2025-10-13 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/testdir/build/lua-master/source/ltm.c
Line
Count
Source
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
60.3k
void luaT_init (lua_State *L) {
39
60.3k
  static const char *const luaT_eventname[] = {  /* ORDER TM */
40
60.3k
    "__index", "__newindex",
41
60.3k
    "__gc", "__mode", "__len", "__eq",
42
60.3k
    "__add", "__sub", "__mul", "__mod", "__pow",
43
60.3k
    "__div", "__idiv",
44
60.3k
    "__band", "__bor", "__bxor", "__shl", "__shr",
45
60.3k
    "__unm", "__bnot", "__lt", "__le",
46
60.3k
    "__concat", "__call", "__close"
47
60.3k
  };
48
60.3k
  int i;
49
1.56M
  for (i=0; i<TM_N; i++) {
50
1.50M
    G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
51
1.50M
    luaC_fix(L, obj2gco(G(L)->tmname[i]));  /* never collect these names */
52
1.50M
  }
53
60.3k
}
54
55
56
/*
57
** function to be used with macro "fasttm": optimized for absence of
58
** tag methods
59
*/
60
3.07M
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
61
3.07M
  const TValue *tm = luaH_Hgetshortstr(events, ename);
62
3.07M
  lua_assert(event <= TM_EQ);
63
3.07M
  if (notm(tm)) {  /* no tag method? */
64
59.7k
    events->flags |= cast_byte(1u<<event);  /* cache this fact */
65
59.7k
    return NULL;
66
59.7k
  }
67
3.01M
  else return tm;
68
3.07M
}
69
70
71
182M
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
72
182M
  Table *mt;
73
182M
  switch (ttype(o)) {
74
810k
    case LUA_TTABLE:
75
810k
      mt = hvalue(o)->metatable;
76
810k
      break;
77
4.68M
    case LUA_TUSERDATA:
78
4.68M
      mt = uvalue(o)->metatable;
79
4.68M
      break;
80
176M
    default:
81
176M
      mt = G(L)->mt[ttype(o)];
82
182M
  }
83
182M
  return (mt ? luaH_Hgetshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue);
84
182M
}
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
815k
const char *luaT_objtypename (lua_State *L, const TValue *o) {
92
815k
  Table *mt;
93
815k
  if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
94
815k
      (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
95
756
    const TValue *name = luaH_Hgetshortstr(mt, luaS_new(L, "__name"));
96
756
    if (ttisstring(name))  /* is '__name' a string? */
97
1.39k
      return getstr(tsvalue(name));  /* use it as type name */
98
756
  }
99
814k
  return ttypename(ttype(o));  /* else use standard type name */
100
815k
}
101
102
103
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
104
34.4M
                  const TValue *p2, const TValue *p3) {
105
34.4M
  StkId func = L->top.p;
106
34.4M
  setobj2s(L, func, f);  /* push function (assume EXTRA_STACK) */
107
34.4M
  setobj2s(L, func + 1, p1);  /* 1st argument */
108
34.4M
  setobj2s(L, func + 2, p2);  /* 2nd argument */
109
34.4M
  setobj2s(L, func + 3, p3);  /* 3rd argument */
110
34.4M
  L->top.p = func + 4;
111
  /* metamethod may yield only when called from Lua code */
112
34.4M
  if (isLuacode(L->ci))
113
34.4M
    luaD_call(L, func, 0);
114
0
  else
115
0
    luaD_callnoyield(L, func, 0);
116
34.4M
}
117
118
119
lu_byte luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
120
91.5M
                        const TValue *p2, StkId res) {
121
91.5M
  ptrdiff_t result = savestack(L, res);
122
91.5M
  StkId func = L->top.p;
123
91.5M
  setobj2s(L, func, f);  /* push function (assume EXTRA_STACK) */
124
91.5M
  setobj2s(L, func + 1, p1);  /* 1st argument */
125
91.5M
  setobj2s(L, func + 2, p2);  /* 2nd argument */
126
91.5M
  L->top.p += 3;
127
  /* metamethod may yield only when called from Lua code */
128
91.5M
  if (isLuacode(L->ci))
129
91.5M
    luaD_call(L, func, 1);
130
63.1k
  else
131
63.1k
    luaD_callnoyield(L, func, 1);
132
91.5M
  res = restorestack(L, result);
133
91.5M
  setobjs2s(L, res, --L->top.p);  /* move result to its place */
134
91.5M
  return ttypetag(s2v(res));  /* return tag of the result */
135
91.5M
}
136
137
138
static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
139
12.9M
                      StkId res, TMS event) {
140
12.9M
  const TValue *tm = luaT_gettmbyobj(L, p1, event);  /* try first operand */
141
12.9M
  if (notm(tm))
142
3.45M
    tm = luaT_gettmbyobj(L, p2, event);  /* try second operand */
143
12.9M
  if (notm(tm))
144
159k
    return -1;  /* tag method not found */
145
12.7M
  else  /* call tag method and return the tag of the result */
146
12.7M
    return luaT_callTMres(L, tm, p1, p2, res);
147
12.9M
}
148
149
150
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
151
7.93M
                    StkId res, TMS event) {
152
7.93M
  if (l_unlikely(callbinTM(L, p1, p2, res, event) < 0)) {
153
123k
    switch (event) {
154
29.4k
      case TM_BAND: case TM_BOR: case TM_BXOR:
155
101k
      case TM_SHL: case TM_SHR: case TM_BNOT: {
156
101k
        if (ttisnumber(p1) && ttisnumber(p2))
157
38.7k
          luaG_tointerror(L, p1, p2);
158
62.3k
        else
159
62.3k
          luaG_opinterror(L, p1, p2, "perform bitwise operation on");
160
101k
      }
161
      /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
162
22.4k
      default:
163
22.4k
        luaG_opinterror(L, p1, p2, "perform arithmetic on");
164
123k
    }
165
123k
  }
166
7.93M
}
167
168
169
/*
170
** The use of 'p1' after 'callbinTM' is safe because, when a tag
171
** method is not found, 'callbinTM' cannot change the stack.
172
*/
173
2.06M
void luaT_tryconcatTM (lua_State *L) {
174
2.06M
  StkId p1 = L->top.p - 2;  /* first argument */
175
2.06M
  if (l_unlikely(callbinTM(L, s2v(p1), s2v(p1 + 1), p1, TM_CONCAT) < 0))
176
2.31k
    luaG_concaterror(L, s2v(p1), s2v(p1 + 1));
177
2.06M
}
178
179
180
void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
181
1.97M
                                       int flip, StkId res, TMS event) {
182
1.97M
  if (flip)
183
118k
    luaT_trybinTM(L, p2, p1, res, event);
184
1.85M
  else
185
1.85M
    luaT_trybinTM(L, p1, p2, res, event);
186
1.97M
}
187
188
189
void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
190
1.49M
                                   int flip, StkId res, TMS event) {
191
1.49M
  TValue aux;
192
1.49M
  setivalue(&aux, i2);
193
1.49M
  luaT_trybinassocTM(L, p1, &aux, flip, res, event);
194
1.49M
}
195
196
197
/*
198
** Calls an order tag method.
199
*/
200
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
201
2.92M
                      TMS event) {
202
2.92M
  int tag = callbinTM(L, p1, p2, L->top.p, event);  /* try original event */
203
2.92M
  if (tag >= 0)  /* found tag method? */
204
2.89M
    return !tagisfalse(tag);
205
34.3k
  luaG_ordererror(L, p1, p2);  /* no metamethod found */
206
0
  return 0;  /* to avoid warnings */
207
2.92M
}
208
209
210
int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
211
1.41M
                       int flip, int isfloat, TMS event) {
212
1.41M
  TValue aux; const TValue *p2;
213
1.41M
  if (isfloat) {
214
2.81k
    setfltvalue(&aux, cast_num(v2));
215
2.81k
  }
216
1.41M
  else
217
1.41M
    setivalue(&aux, v2);
218
1.41M
  if (flip) {  /* arguments were exchanged? */
219
1.36M
    p2 = p1; p1 = &aux;  /* correct them */
220
1.36M
  }
221
49.5k
  else
222
49.5k
    p2 = &aux;
223
1.41M
  return luaT_callorderTM(L, p1, p2, event);
224
1.41M
}
225
226
227
void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci,
228
14.4M
                         const Proto *p) {
229
14.4M
  int i;
230
14.4M
  int actual = cast_int(L->top.p - ci->func.p) - 1;  /* number of arguments */
231
14.4M
  int nextra = actual - nfixparams;  /* number of extra arguments */
232
14.4M
  ci->u.l.nextraargs = nextra;
233
14.4M
  luaD_checkstack(L, p->maxstacksize + 1);
234
  /* copy function to the top of the stack */
235
14.4M
  setobjs2s(L, L->top.p++, ci->func.p);
236
  /* move fixed parameters to the top of the stack */
237
46.7M
  for (i = 1; i <= nfixparams; i++) {
238
32.2M
    setobjs2s(L, L->top.p++, ci->func.p + i);
239
32.2M
    setnilvalue(s2v(ci->func.p + i));  /* erase original parameter (for GC) */
240
32.2M
  }
241
14.4M
  ci->func.p += actual + 1;
242
14.4M
  ci->top.p += actual + 1;
243
14.4M
  lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p);
244
14.4M
}
245
246
247
2.39M
void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
248
2.39M
  int i;
249
2.39M
  int nextra = ci->u.l.nextraargs;
250
2.39M
  if (wanted < 0) {
251
412k
    wanted = nextra;  /* get all extra arguments available */
252
412k
    checkstackp(L, nextra, where);  /* ensure stack space */
253
412k
    L->top.p = where + nextra;  /* next instruction will need top */
254
412k
  }
255
44.1M
  for (i = 0; i < wanted && i < nextra; i++)
256
41.7M
    setobjs2s(L, where + i, ci->func.p - nextra + i);
257
3.59M
  for (; i < wanted; i++)   /* complete required results with nil */
258
2.39M
    setnilvalue(s2v(where + i));
259
2.39M
}
260