Coverage Report

Created: 2025-10-27 06:39

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
62.8k
void luaT_init (lua_State *L) {
39
62.8k
  static const char *const luaT_eventname[] = {  /* ORDER TM */
40
62.8k
    "__index", "__newindex",
41
62.8k
    "__gc", "__mode", "__len", "__eq",
42
62.8k
    "__add", "__sub", "__mul", "__mod", "__pow",
43
62.8k
    "__div", "__idiv",
44
62.8k
    "__band", "__bor", "__bxor", "__shl", "__shr",
45
62.8k
    "__unm", "__bnot", "__lt", "__le",
46
62.8k
    "__concat", "__call", "__close"
47
62.8k
  };
48
62.8k
  int i;
49
1.63M
  for (i=0; i<TM_N; i++) {
50
1.57M
    G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
51
1.57M
    luaC_fix(L, obj2gco(G(L)->tmname[i]));  /* never collect these names */
52
1.57M
  }
53
62.8k
}
54
55
56
/*
57
** function to be used with macro "fasttm": optimized for absence of
58
** tag methods
59
*/
60
4.79M
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
61
4.79M
  const TValue *tm = luaH_Hgetshortstr(events, ename);
62
4.79M
  lua_assert(event <= TM_EQ);
63
4.79M
  if (notm(tm)) {  /* no tag method? */
64
144k
    events->flags |= cast_byte(1u<<event);  /* cache this fact */
65
144k
    return NULL;
66
144k
  }
67
4.65M
  else return tm;
68
4.79M
}
69
70
71
219M
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
72
219M
  Table *mt;
73
219M
  switch (ttype(o)) {
74
842k
    case LUA_TTABLE:
75
842k
      mt = hvalue(o)->metatable;
76
842k
      break;
77
4.59M
    case LUA_TUSERDATA:
78
4.59M
      mt = uvalue(o)->metatable;
79
4.59M
      break;
80
213M
    default:
81
213M
      mt = G(L)->mt[ttype(o)];
82
219M
  }
83
219M
  return (mt ? luaH_Hgetshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue);
84
219M
}
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
779k
const char *luaT_objtypename (lua_State *L, const TValue *o) {
92
779k
  Table *mt;
93
779k
  if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
94
779k
      (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
95
1.33k
    const TValue *name = luaH_Hgetshortstr(mt, luaS_new(L, "__name"));
96
1.33k
    if (ttisstring(name))  /* is '__name' a string? */
97
1.38k
      return getstr(tsvalue(name));  /* use it as type name */
98
1.33k
  }
99
778k
  return ttypename(ttype(o));  /* else use standard type name */
100
779k
}
101
102
103
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
104
40.4M
                  const TValue *p2, const TValue *p3) {
105
40.4M
  StkId func = L->top.p;
106
40.4M
  setobj2s(L, func, f);  /* push function (assume EXTRA_STACK) */
107
40.4M
  setobj2s(L, func + 1, p1);  /* 1st argument */
108
40.4M
  setobj2s(L, func + 2, p2);  /* 2nd argument */
109
40.4M
  setobj2s(L, func + 3, p3);  /* 3rd argument */
110
40.4M
  L->top.p = func + 4;
111
  /* metamethod may yield only when called from Lua code */
112
40.4M
  if (isLuacode(L->ci))
113
40.4M
    luaD_call(L, func, 0);
114
0
  else
115
0
    luaD_callnoyield(L, func, 0);
116
40.4M
}
117
118
119
lu_byte luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
120
114M
                        const TValue *p2, StkId res) {
121
114M
  ptrdiff_t result = savestack(L, res);
122
114M
  StkId func = L->top.p;
123
114M
  setobj2s(L, func, f);  /* push function (assume EXTRA_STACK) */
124
114M
  setobj2s(L, func + 1, p1);  /* 1st argument */
125
114M
  setobj2s(L, func + 2, p2);  /* 2nd argument */
126
114M
  L->top.p += 3;
127
  /* metamethod may yield only when called from Lua code */
128
114M
  if (isLuacode(L->ci))
129
114M
    luaD_call(L, func, 1);
130
62.8k
  else
131
62.8k
    luaD_callnoyield(L, func, 1);
132
114M
  res = restorestack(L, result);
133
114M
  setobjs2s(L, res, --L->top.p);  /* move result to its place */
134
114M
  return ttypetag(s2v(res));  /* return tag of the result */
135
114M
}
136
137
138
static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
139
18.4M
                      StkId res, TMS event) {
140
18.4M
  const TValue *tm = luaT_gettmbyobj(L, p1, event);  /* try first operand */
141
18.4M
  if (notm(tm))
142
5.53M
    tm = luaT_gettmbyobj(L, p2, event);  /* try second operand */
143
18.4M
  if (notm(tm))
144
172k
    return -1;  /* tag method not found */
145
18.2M
  else  /* call tag method and return the tag of the result */
146
18.2M
    return luaT_callTMres(L, tm, p1, p2, res);
147
18.4M
}
148
149
150
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
151
11.7M
                    StkId res, TMS event) {
152
11.7M
  if (l_unlikely(callbinTM(L, p1, p2, res, event) < 0)) {
153
132k
    switch (event) {
154
34.2k
      case TM_BAND: case TM_BOR: case TM_BXOR:
155
108k
      case TM_SHL: case TM_SHR: case TM_BNOT: {
156
108k
        if (ttisnumber(p1) && ttisnumber(p2))
157
38.6k
          luaG_tointerror(L, p1, p2);
158
69.3k
        else
159
69.3k
          luaG_opinterror(L, p1, p2, "perform bitwise operation on");
160
108k
      }
161
      /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
162
24.8k
      default:
163
24.8k
        luaG_opinterror(L, p1, p2, "perform arithmetic on");
164
132k
    }
165
132k
  }
166
11.7M
}
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.18M
void luaT_tryconcatTM (lua_State *L) {
174
2.18M
  StkId p1 = L->top.p - 2;  /* first argument */
175
2.18M
  if (l_unlikely(callbinTM(L, s2v(p1), s2v(p1 + 1), p1, TM_CONCAT) < 0))
176
5.96k
    luaG_concaterror(L, s2v(p1), s2v(p1 + 1));
177
2.18M
}
178
179
180
void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
181
3.68M
                                       int flip, StkId res, TMS event) {
182
3.68M
  if (flip)
183
203k
    luaT_trybinTM(L, p2, p1, res, event);
184
3.48M
  else
185
3.48M
    luaT_trybinTM(L, p1, p2, res, event);
186
3.68M
}
187
188
189
void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
190
2.76M
                                   int flip, StkId res, TMS event) {
191
2.76M
  TValue aux;
192
2.76M
  setivalue(&aux, i2);
193
2.76M
  luaT_trybinassocTM(L, p1, &aux, flip, res, event);
194
2.76M
}
195
196
197
/*
198
** Calls an order tag method.
199
*/
200
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
201
4.49M
                      TMS event) {
202
4.49M
  int tag = callbinTM(L, p1, p2, L->top.p, event);  /* try original event */
203
4.49M
  if (tag >= 0)  /* found tag method? */
204
4.45M
    return !tagisfalse(tag);
205
35.1k
  luaG_ordererror(L, p1, p2);  /* no metamethod found */
206
0
  return 0;  /* to avoid warnings */
207
4.49M
}
208
209
210
int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
211
2.67M
                       int flip, int isfloat, TMS event) {
212
2.67M
  TValue aux; const TValue *p2;
213
2.67M
  if (isfloat) {
214
2.81k
    setfltvalue(&aux, cast_num(v2));
215
2.81k
  }
216
2.67M
  else
217
2.67M
    setivalue(&aux, v2);
218
2.67M
  if (flip) {  /* arguments were exchanged? */
219
2.60M
    p2 = p1; p1 = &aux;  /* correct them */
220
2.60M
  }
221
75.0k
  else
222
75.0k
    p2 = &aux;
223
2.67M
  return luaT_callorderTM(L, p1, p2, event);
224
2.67M
}
225
226
227
/*
228
** Create a vararg table at the top of the stack, with 'n' elements
229
** starting at 'f'.
230
*/
231
0
static void createvarargtab (lua_State *L, StkId f, int n) {
232
0
  int i;
233
0
  TValue key, value;
234
0
  Table *t = luaH_new(L);
235
0
  sethvalue(L, s2v(L->top.p), t);
236
0
  L->top.p++;
237
0
  luaH_resize(L, t, cast_uint(n), 1);
238
0
  setsvalue(L, &key, luaS_new(L, "n"));  /* key is "n" */
239
0
  setivalue(&value, n);  /* value is n */
240
  /* No need to anchor the key: Due to the resize, the next operation
241
     cannot trigger a garbage collection */
242
0
  luaH_set(L, t, &key, &value);  /* t.n = n */
243
0
  for (i = 0; i < n; i++)
244
0
    luaH_setint(L, t, i + 1, s2v(f + i));
245
0
}
246
247
248
/*
249
** initial stack:  func arg1 ... argn extra1 ...
250
**                 ^ ci->func                    ^ L->top
251
** final stack: func nil ... nil extra1 ... func arg1 ... argn
252
**                                          ^ ci->func         ^ L->top
253
*/
254
19.4M
void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) {
255
19.4M
  int i;
256
19.4M
  int totalargs = cast_int(L->top.p - ci->func.p) - 1;
257
19.4M
  int nfixparams = p->numparams;
258
19.4M
  int nextra = totalargs - nfixparams;  /* number of extra arguments */
259
19.4M
  ci->u.l.nextraargs = nextra;
260
19.4M
  luaD_checkstack(L, p->maxstacksize + 1);
261
  /* copy function to the top of the stack */
262
19.4M
  setobjs2s(L, L->top.p++, ci->func.p);
263
  /* move fixed parameters to the top of the stack */
264
63.8M
  for (i = 1; i <= nfixparams; i++) {
265
44.3M
    setobjs2s(L, L->top.p++, ci->func.p + i);
266
44.3M
    setnilvalue(s2v(ci->func.p + i));  /* erase original parameter (for GC) */
267
44.3M
  }
268
19.4M
  if (p->flag & PF_VAVAR) {  /* is there a vararg parameter? */
269
0
    if (p->flag & PF_VATAB)  /* does it need a vararg table? */
270
0
      createvarargtab(L, ci->func.p + nfixparams + 1, nextra);
271
0
    else  /* no table; set parameter to nil */
272
0
      setnilvalue(s2v(L->top.p));
273
0
  }
274
19.4M
  ci->func.p += totalargs + 1;
275
19.4M
  ci->top.p += totalargs + 1;
276
19.4M
  lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p);
277
19.4M
}
278
279
280
0
void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc) {
281
0
  int nextra = ci->u.l.nextraargs;
282
0
  lua_Integer n;
283
0
  if (tointegerns(rc, &n)) {  /* integral value? */
284
0
    if (l_castS2U(n) - 1 < cast_uint(nextra)) {
285
0
      StkId slot = ci->func.p - nextra + cast_int(n) - 1;
286
0
      setobjs2s(((lua_State*)NULL), ra, slot);
287
0
      return;
288
0
    }
289
0
  }
290
0
  else if (ttisshrstring(rc)) {  /* short-string value? */
291
0
    size_t len;
292
0
    const char *s = getlstr(tsvalue(rc), len);
293
0
    if (len == 1 && s[0] == 'n') {  /* key is "n"? */
294
0
      setivalue(s2v(ra), nextra);
295
0
      return;
296
0
    }
297
0
  }
298
0
  setnilvalue(s2v(ra));  /* else produce nil */
299
0
}
300
301
302
4.50M
void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
303
4.50M
  int i;
304
4.50M
  int nextra = ci->u.l.nextraargs;
305
4.50M
  if (wanted < 0) {
306
2.51M
    wanted = nextra;  /* get all extra arguments available */
307
2.51M
    checkstackp(L, nextra, where);  /* ensure stack space */
308
2.51M
    L->top.p = where + nextra;  /* next instruction will need top */
309
2.51M
  }
310
96.8M
  for (i = 0; i < wanted && i < nextra; i++)
311
92.3M
    setobjs2s(L, where + i, ci->func.p - nextra + i);
312
5.57M
  for (; i < wanted; i++)   /* complete required results with nil */
313
4.50M
    setnilvalue(s2v(where + i));
314
4.50M
}
315