Coverage Report

Created: 2025-08-28 06:30

/src/testdir/build/lua-master/source/lfunc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** $Id: lfunc.c $
3
** Auxiliary functions to manipulate prototypes and closures
4
** See Copyright Notice in lua.h
5
*/
6
7
#define lfunc_c
8
#define LUA_CORE
9
10
#include "lprefix.h"
11
12
13
#include <stddef.h>
14
15
#include "lua.h"
16
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
25
26
27
747k
CClosure *luaF_newCclosure (lua_State *L, int nupvals) {
28
747k
  GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals));
29
747k
  CClosure *c = gco2ccl(o);
30
747k
  c->nupvalues = cast_byte(nupvals);
31
747k
  return c;
32
747k
}
33
34
35
24.6M
LClosure *luaF_newLclosure (lua_State *L, int nupvals) {
36
24.6M
  GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals));
37
24.6M
  LClosure *c = gco2lcl(o);
38
0
  c->p = NULL;
39
24.6M
  c->nupvalues = cast_byte(nupvals);
40
48.0M
  while (nupvals--) c->upvals[nupvals] = NULL;
41
24.6M
  return c;
42
24.6M
}
43
44
45
/*
46
** fill a closure with new closed upvalues
47
*/
48
3.29M
void luaF_initupvals (lua_State *L, LClosure *cl) {
49
3.29M
  int i;
50
6.59M
  for (i = 0; i < cl->nupvalues; i++) {
51
3.29M
    GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
52
3.29M
    UpVal *uv = gco2upv(o);
53
0
    uv->v.p = &uv->u.value;  /* make it closed */
54
3.29M
    setnilvalue(uv->v.p);
55
3.29M
    cl->upvals[i] = uv;
56
3.29M
    luaC_objbarrier(L, cl, uv);
57
3.29M
  }
58
3.29M
}
59
60
61
/*
62
** Create a new upvalue at the given level, and link it to the list of
63
** open upvalues of 'L' after entry 'prev'.
64
**/
65
9.77M
static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) {
66
9.77M
  GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
67
9.77M
  UpVal *uv = gco2upv(o);
68
0
  UpVal *next = *prev;
69
9.77M
  uv->v.p = s2v(level);  /* current value lives in the stack */
70
9.77M
  uv->u.open.next = next;  /* link it to list of open upvalues */
71
9.77M
  uv->u.open.previous = prev;
72
9.77M
  if (next)
73
9.22M
    next->u.open.previous = &uv->u.open.next;
74
9.77M
  *prev = uv;
75
9.77M
  if (!isintwups(L)) {  /* thread not in list of threads with upvalues? */
76
45.6k
    L->twups = G(L)->twups;  /* link it to the list */
77
45.6k
    G(L)->twups = L;
78
45.6k
  }
79
9.77M
  return uv;
80
9.77M
}
81
82
83
/*
84
** Find and reuse, or create if it does not exist, an upvalue
85
** at the given level.
86
*/
87
11.4M
UpVal *luaF_findupval (lua_State *L, StkId level) {
88
11.4M
  UpVal **pp = &L->openupval;
89
11.4M
  UpVal *p;
90
11.4M
  lua_assert(isintwups(L) || L->openupval == NULL);
91
25.5M
  while ((p = *pp) != NULL && uplevel(p) >= level) {  /* search for it */
92
3.57M
    lua_assert(!isdead(G(L), p));
93
7.15M
    if (uplevel(p) == level)  /* corresponding upvalue? */
94
1.62M
      return p;  /* return it */
95
1.94M
    pp = &p->u.open.next;
96
1.94M
  }
97
  /* not found: create a new upvalue after 'pp' */
98
9.77M
  return newupval(L, level, pp);
99
11.4M
}
100
101
102
/*
103
** Call closing method for object 'obj' with error object 'err'. The
104
** boolean 'yy' controls whether the call is yieldable.
105
** (This function assumes EXTRA_STACK.)
106
*/
107
885k
static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) {
108
885k
  StkId top = L->top.p;
109
885k
  StkId func = top;
110
885k
  const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
111
885k
  setobj2s(L, top++, tm);  /* will call metamethod... */
112
885k
  setobj2s(L, top++, obj);  /* with 'self' as the 1st argument */
113
885k
  if (err != NULL)  /* if there was an error... */
114
885k
    setobj2s(L, top++, err);  /* then error object will be 2nd argument */
115
885k
  L->top.p = top;  /* add function and arguments */
116
885k
  if (yy)
117
33.0k
    luaD_call(L, func, 0);
118
852k
  else
119
852k
    luaD_callnoyield(L, func, 0);
120
885k
}
121
122
123
/*
124
** Check whether object at given level has a close metamethod and raise
125
** an error if not.
126
*/
127
886k
static void checkclosemth (lua_State *L, StkId level) {
128
886k
  const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE);
129
886k
  if (ttisnil(tm)) {  /* no metamethod? */
130
623
    int idx = cast_int(level - L->ci->func.p);  /* variable index */
131
623
    const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
132
623
    if (vname == NULL) vname = "?";
133
623
    luaG_runerror(L, "variable '%s' got a non-closable value", vname);
134
623
  }
135
886k
}
136
137
138
/*
139
** Prepare and call a closing method.
140
** If status is CLOSEKTOP, the call to the closing method will be pushed
141
** at the top of the stack. Otherwise, values can be pushed right after
142
** the 'level' of the upvalue being closed, as everything after that
143
** won't be used again.
144
*/
145
static void prepcallclosemth (lua_State *L, StkId level, TStatus status,
146
885k
                                            int yy) {
147
885k
  TValue *uv = s2v(level);  /* value being closed */
148
885k
  TValue *errobj;
149
885k
  switch (status) {
150
32.7k
    case LUA_OK:
151
32.7k
      L->top.p = level + 1;  /* call will be at this level */
152
      /* FALLTHROUGH */
153
877k
    case CLOSEKTOP:  /* don't need to change top */
154
877k
      errobj = NULL;  /* no error object */
155
877k
      break;
156
8.55k
    default:  /* 'luaD_seterrorobj' will set top to level + 2 */
157
8.55k
      errobj = s2v(level + 1);  /* error object goes after 'uv' */
158
8.55k
      luaD_seterrorobj(L, status, level + 1);  /* set error object */
159
8.55k
      break;
160
885k
  }
161
885k
  callclosemethod(L, uv, errobj, yy);
162
885k
}
163
164
165
/* Maximum value for deltas in 'tbclist' */
166
2.03M
#define MAXDELTA       USHRT_MAX
167
168
169
/*
170
** Insert a variable in the list of to-be-closed variables.
171
*/
172
1.00M
void luaF_newtbcupval (lua_State *L, StkId level) {
173
1.00M
  lua_assert(level > L->tbclist.p);
174
1.00M
  if (l_isfalse(s2v(level)))
175
114k
    return;  /* false doesn't need to be closed */
176
886k
  checkclosemth(L, level);  /* value must have a close method */
177
974k
  while (cast_uint(level - L->tbclist.p) > MAXDELTA) {
178
88.0k
    L->tbclist.p += MAXDELTA;  /* create a dummy node at maximum delta */
179
88.0k
    L->tbclist.p->tbclist.delta = 0;
180
88.0k
  }
181
886k
  level->tbclist.delta = cast(unsigned short, level - L->tbclist.p);
182
886k
  L->tbclist.p = level;
183
886k
}
184
185
186
9.77M
void luaF_unlinkupval (UpVal *uv) {
187
9.77M
  lua_assert(upisopen(uv));
188
9.77M
  *uv->u.open.previous = uv->u.open.next;
189
9.77M
  if (uv->u.open.next)
190
9.44M
    uv->u.open.next->u.open.previous = uv->u.open.previous;
191
9.77M
}
192
193
194
/*
195
** Close all upvalues up to the given stack level.
196
*/
197
11.3M
void luaF_closeupval (lua_State *L, StkId level) {
198
11.3M
  UpVal *uv;
199
34.2M
  while ((uv = L->openupval) != NULL && uplevel(uv) >= level) {
200
9.76M
    TValue *slot = &uv->u.value;  /* new position for value */
201
9.76M
    lua_assert(uplevel(uv) < L->top.p);
202
9.76M
    luaF_unlinkupval(uv);  /* remove upvalue from 'openupval' list */
203
9.76M
    setobj(L, slot, uv->v.p);  /* move value to upvalue slot */
204
9.76M
    uv->v.p = slot;  /* now current value lives here */
205
9.76M
    if (!iswhite(uv)) {  /* neither white nor dead? */
206
1.02M
      nw2black(uv);  /* closed upvalues cannot be gray */
207
1.02M
      luaC_barrier(L, uv, slot);
208
1.02M
    }
209
9.76M
  }
210
11.3M
}
211
212
213
/*
214
** Remove first element from the tbclist plus its dummy nodes.
215
*/
216
885k
static void poptbclist (lua_State *L) {
217
885k
  StkId tbc = L->tbclist.p;
218
885k
  lua_assert(tbc->tbclist.delta > 0);  /* first element cannot be dummy */
219
885k
  tbc -= tbc->tbclist.delta;
220
973k
  while (tbc > L->stack.p && tbc->tbclist.delta == 0)
221
88.0k
    tbc -= MAXDELTA;  /* remove dummy nodes */
222
885k
  L->tbclist.p = tbc;
223
885k
}
224
225
226
/*
227
** Close all upvalues and to-be-closed variables up to the given stack
228
** level. Return restored 'level'.
229
*/
230
11.3M
StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy) {
231
11.3M
  ptrdiff_t levelrel = savestack(L, level);
232
11.3M
  luaF_closeupval(L, level);  /* first, close the upvalues */
233
12.1M
  while (L->tbclist.p >= level) {  /* traverse tbc's down to that level */
234
885k
    StkId tbc = L->tbclist.p;  /* get variable index */
235
885k
    poptbclist(L);  /* remove it from list */
236
885k
    prepcallclosemth(L, tbc, status, yy);  /* close variable */
237
885k
    level = restorestack(L, levelrel);
238
885k
  }
239
11.3M
  return level;
240
11.3M
}
241
242
243
9.63M
Proto *luaF_newproto (lua_State *L) {
244
9.63M
  GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
245
9.63M
  Proto *f = gco2p(o);
246
0
  f->k = NULL;
247
9.63M
  f->sizek = 0;
248
9.63M
  f->p = NULL;
249
9.63M
  f->sizep = 0;
250
9.63M
  f->code = NULL;
251
9.63M
  f->sizecode = 0;
252
9.63M
  f->lineinfo = NULL;
253
9.63M
  f->sizelineinfo = 0;
254
9.63M
  f->abslineinfo = NULL;
255
9.63M
  f->sizeabslineinfo = 0;
256
9.63M
  f->upvalues = NULL;
257
9.63M
  f->sizeupvalues = 0;
258
9.63M
  f->numparams = 0;
259
9.63M
  f->flag = 0;
260
9.63M
  f->maxstacksize = 0;
261
9.63M
  f->locvars = NULL;
262
9.63M
  f->sizelocvars = 0;
263
9.63M
  f->linedefined = 0;
264
9.63M
  f->lastlinedefined = 0;
265
9.63M
  f->source = NULL;
266
9.63M
  return f;
267
9.63M
}
268
269
270
21.2M
lu_mem luaF_protosize (Proto *p) {
271
21.2M
  lu_mem sz = cast(lu_mem, sizeof(Proto))
272
21.2M
            + cast_uint(p->sizep) * sizeof(Proto*)
273
21.2M
            + cast_uint(p->sizek) * sizeof(TValue)
274
21.2M
            + cast_uint(p->sizelocvars) * sizeof(LocVar)
275
21.2M
            + cast_uint(p->sizeupvalues) * sizeof(Upvaldesc);
276
21.2M
  if (!(p->flag & PF_FIXED)) {
277
21.2M
    sz += cast_uint(p->sizecode) * sizeof(Instruction);
278
21.2M
    sz += cast_uint(p->sizelineinfo) * sizeof(lu_byte);
279
21.2M
    sz += cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo);
280
21.2M
  }
281
21.2M
  return sz;
282
21.2M
}
283
284
285
9.63M
void luaF_freeproto (lua_State *L, Proto *f) {
286
9.63M
  if (!(f->flag & PF_FIXED)) {
287
9.63M
    luaM_freearray(L, f->code, cast_sizet(f->sizecode));
288
9.63M
    luaM_freearray(L, f->lineinfo, cast_sizet(f->sizelineinfo));
289
9.63M
    luaM_freearray(L, f->abslineinfo, cast_sizet(f->sizeabslineinfo));
290
9.63M
  }
291
9.63M
  luaM_freearray(L, f->p, cast_sizet(f->sizep));
292
9.63M
  luaM_freearray(L, f->k, cast_sizet(f->sizek));
293
9.63M
  luaM_freearray(L, f->locvars, cast_sizet(f->sizelocvars));
294
9.63M
  luaM_freearray(L, f->upvalues, cast_sizet(f->sizeupvalues));
295
9.63M
  luaM_free(L, f);
296
9.63M
}
297
298
299
/*
300
** Look for n-th local variable at line 'line' in function 'func'.
301
** Returns NULL if not found.
302
*/
303
2.62M
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
304
2.62M
  int i;
305
42.8M
  for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
306
40.3M
    if (pc < f->locvars[i].endpc) {  /* is variable active? */
307
17.2M
      local_number--;
308
17.2M
      if (local_number == 0)
309
125k
        return getstr(f->locvars[i].varname);
310
17.2M
    }
311
40.3M
  }
312
2.50M
  return NULL;  /* not found */
313
2.62M
}
314