Coverage Report

Created: 2025-07-11 06:33

/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
779k
CClosure *luaF_newCclosure (lua_State *L, int nupvals) {
28
779k
  GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals));
29
779k
  CClosure *c = gco2ccl(o);
30
779k
  c->nupvalues = cast_byte(nupvals);
31
779k
  return c;
32
779k
}
33
34
35
21.6M
LClosure *luaF_newLclosure (lua_State *L, int nupvals) {
36
21.6M
  GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals));
37
21.6M
  LClosure *c = gco2lcl(o);
38
0
  c->p = NULL;
39
21.6M
  c->nupvalues = cast_byte(nupvals);
40
40.6M
  while (nupvals--) c->upvals[nupvals] = NULL;
41
21.6M
  return c;
42
21.6M
}
43
44
45
/*
46
** fill a closure with new closed upvalues
47
*/
48
2.96M
void luaF_initupvals (lua_State *L, LClosure *cl) {
49
2.96M
  int i;
50
5.89M
  for (i = 0; i < cl->nupvalues; i++) {
51
2.92M
    GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
52
2.92M
    UpVal *uv = gco2upv(o);
53
0
    uv->v.p = &uv->u.value;  /* make it closed */
54
2.92M
    setnilvalue(uv->v.p);
55
2.92M
    cl->upvals[i] = uv;
56
2.92M
    luaC_objbarrier(L, cl, uv);
57
2.92M
  }
58
2.96M
}
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
5.57M
static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) {
66
5.57M
  GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
67
5.57M
  UpVal *uv = gco2upv(o);
68
0
  UpVal *next = *prev;
69
5.57M
  uv->v.p = s2v(level);  /* current value lives in the stack */
70
5.57M
  uv->u.open.next = next;  /* link it to list of open upvalues */
71
5.57M
  uv->u.open.previous = prev;
72
5.57M
  if (next)
73
5.41M
    next->u.open.previous = &uv->u.open.next;
74
5.57M
  *prev = uv;
75
5.57M
  if (!isintwups(L)) {  /* thread not in list of threads with upvalues? */
76
53.6k
    L->twups = G(L)->twups;  /* link it to the list */
77
53.6k
    G(L)->twups = L;
78
53.6k
  }
79
5.57M
  return uv;
80
5.57M
}
81
82
83
/*
84
** Find and reuse, or create if it does not exist, an upvalue
85
** at the given level.
86
*/
87
7.37M
UpVal *luaF_findupval (lua_State *L, StkId level) {
88
7.37M
  UpVal **pp = &L->openupval;
89
7.37M
  UpVal *p;
90
7.37M
  lua_assert(isintwups(L) || L->openupval == NULL);
91
17.7M
  while ((p = *pp) != NULL && uplevel(p) >= level) {  /* search for it */
92
3.44M
    lua_assert(!isdead(G(L), p));
93
6.89M
    if (uplevel(p) == level)  /* corresponding upvalue? */
94
1.79M
      return p;  /* return it */
95
1.64M
    pp = &p->u.open.next;
96
1.64M
  }
97
  /* not found: create a new upvalue after 'pp' */
98
5.57M
  return newupval(L, level, pp);
99
7.37M
}
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
1.04M
static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) {
108
1.04M
  StkId top = L->top.p;
109
1.04M
  StkId func = top;
110
1.04M
  const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
111
1.04M
  setobj2s(L, top++, tm);  /* will call metamethod... */
112
1.04M
  setobj2s(L, top++, obj);  /* with 'self' as the 1st argument */
113
1.04M
  if (err != NULL)  /* if there was an error... */
114
1.04M
    setobj2s(L, top++, err);  /* then error object will be 2nd argument */
115
1.04M
  L->top.p = top;  /* add function and arguments */
116
1.04M
  if (yy)
117
21.7k
    luaD_call(L, func, 0);
118
1.02M
  else
119
1.02M
    luaD_callnoyield(L, func, 0);
120
1.04M
}
121
122
123
/*
124
** Check whether object at given level has a close metamethod and raise
125
** an error if not.
126
*/
127
1.04M
static void checkclosemth (lua_State *L, StkId level) {
128
1.04M
  const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE);
129
1.04M
  if (ttisnil(tm)) {  /* no metamethod? */
130
215
    int idx = cast_int(level - L->ci->func.p);  /* variable index */
131
215
    const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
132
215
    if (vname == NULL) vname = "?";
133
215
    luaG_runerror(L, "variable '%s' got a non-closable value", vname);
134
215
  }
135
1.04M
}
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
1.04M
                                            int yy) {
147
1.04M
  TValue *uv = s2v(level);  /* value being closed */
148
1.04M
  TValue *errobj;
149
1.04M
  switch (status) {
150
16.0k
    case LUA_OK:
151
16.0k
      L->top.p = level + 1;  /* call will be at this level */
152
      /* FALLTHROUGH */
153
1.02M
    case CLOSEKTOP:  /* don't need to change top */
154
1.02M
      errobj = NULL;  /* no error object */
155
1.02M
      break;
156
22.6k
    default:  /* 'luaD_seterrorobj' will set top to level + 2 */
157
22.6k
      errobj = s2v(level + 1);  /* error object goes after 'uv' */
158
22.6k
      luaD_seterrorobj(L, status, level + 1);  /* set error object */
159
22.6k
      break;
160
1.04M
  }
161
1.04M
  callclosemethod(L, uv, errobj, yy);
162
1.04M
}
163
164
165
/* Maximum value for deltas in 'tbclist' */
166
2.08M
#define MAXDELTA       USHRT_MAX
167
168
169
/*
170
** Insert a variable in the list of to-be-closed variables.
171
*/
172
1.35M
void luaF_newtbcupval (lua_State *L, StkId level) {
173
1.35M
  lua_assert(level > L->tbclist.p);
174
1.35M
  if (l_isfalse(s2v(level)))
175
314k
    return;  /* false doesn't need to be closed */
176
1.04M
  checkclosemth(L, level);  /* value must have a close method */
177
1.04M
  while (cast_uint(level - L->tbclist.p) > MAXDELTA) {
178
150
    L->tbclist.p += MAXDELTA;  /* create a dummy node at maximum delta */
179
150
    L->tbclist.p->tbclist.delta = 0;
180
150
  }
181
1.04M
  level->tbclist.delta = cast(unsigned short, level - L->tbclist.p);
182
1.04M
  L->tbclist.p = level;
183
1.04M
}
184
185
186
5.57M
void luaF_unlinkupval (UpVal *uv) {
187
5.57M
  lua_assert(upisopen(uv));
188
5.57M
  *uv->u.open.previous = uv->u.open.next;
189
5.57M
  if (uv->u.open.next)
190
5.42M
    uv->u.open.next->u.open.previous = uv->u.open.previous;
191
5.57M
}
192
193
194
/*
195
** Close all upvalues up to the given stack level.
196
*/
197
12.3M
void luaF_closeupval (lua_State *L, StkId level) {
198
12.3M
  UpVal *uv;
199
12.3M
  StkId upl;  /* stack index pointed by 'uv' */
200
17.9M
  while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) {
201
5.57M
    TValue *slot = &uv->u.value;  /* new position for value */
202
5.57M
    lua_assert(uplevel(uv) < L->top.p);
203
5.57M
    luaF_unlinkupval(uv);  /* remove upvalue from 'openupval' list */
204
5.57M
    setobj(L, slot, uv->v.p);  /* move value to upvalue slot */
205
5.57M
    uv->v.p = slot;  /* now current value lives here */
206
5.57M
    if (!iswhite(uv)) {  /* neither white nor dead? */
207
364k
      nw2black(uv);  /* closed upvalues cannot be gray */
208
364k
      luaC_barrier(L, uv, slot);
209
364k
    }
210
5.57M
  }
211
12.3M
}
212
213
214
/*
215
** Remove first element from the tbclist plus its dummy nodes.
216
*/
217
1.04M
static void poptbclist (lua_State *L) {
218
1.04M
  StkId tbc = L->tbclist.p;
219
1.04M
  lua_assert(tbc->tbclist.delta > 0);  /* first element cannot be dummy */
220
1.04M
  tbc -= tbc->tbclist.delta;
221
1.04M
  while (tbc > L->stack.p && tbc->tbclist.delta == 0)
222
150
    tbc -= MAXDELTA;  /* remove dummy nodes */
223
1.04M
  L->tbclist.p = tbc;
224
1.04M
}
225
226
227
/*
228
** Close all upvalues and to-be-closed variables up to the given stack
229
** level. Return restored 'level'.
230
*/
231
12.2M
StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy) {
232
12.2M
  ptrdiff_t levelrel = savestack(L, level);
233
12.2M
  luaF_closeupval(L, level);  /* first, close the upvalues */
234
13.3M
  while (L->tbclist.p >= level) {  /* traverse tbc's down to that level */
235
1.04M
    StkId tbc = L->tbclist.p;  /* get variable index */
236
1.04M
    poptbclist(L);  /* remove it from list */
237
1.04M
    prepcallclosemth(L, tbc, status, yy);  /* close variable */
238
1.04M
    level = restorestack(L, levelrel);
239
1.04M
  }
240
12.2M
  return level;
241
12.2M
}
242
243
244
11.1M
Proto *luaF_newproto (lua_State *L) {
245
11.1M
  GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
246
11.1M
  Proto *f = gco2p(o);
247
0
  f->k = NULL;
248
11.1M
  f->sizek = 0;
249
11.1M
  f->p = NULL;
250
11.1M
  f->sizep = 0;
251
11.1M
  f->code = NULL;
252
11.1M
  f->sizecode = 0;
253
11.1M
  f->lineinfo = NULL;
254
11.1M
  f->sizelineinfo = 0;
255
11.1M
  f->abslineinfo = NULL;
256
11.1M
  f->sizeabslineinfo = 0;
257
11.1M
  f->upvalues = NULL;
258
11.1M
  f->sizeupvalues = 0;
259
11.1M
  f->numparams = 0;
260
11.1M
  f->flag = 0;
261
11.1M
  f->maxstacksize = 0;
262
11.1M
  f->locvars = NULL;
263
11.1M
  f->sizelocvars = 0;
264
11.1M
  f->linedefined = 0;
265
11.1M
  f->lastlinedefined = 0;
266
11.1M
  f->source = NULL;
267
11.1M
  return f;
268
11.1M
}
269
270
271
21.3M
lu_mem luaF_protosize (Proto *p) {
272
21.3M
  lu_mem sz = cast(lu_mem, sizeof(Proto))
273
21.3M
            + cast_uint(p->sizep) * sizeof(Proto*)
274
21.3M
            + cast_uint(p->sizek) * sizeof(TValue)
275
21.3M
            + cast_uint(p->sizelocvars) * sizeof(LocVar)
276
21.3M
            + cast_uint(p->sizeupvalues) * sizeof(Upvaldesc);
277
21.3M
  if (!(p->flag & PF_FIXED)) {
278
21.3M
    sz += cast_uint(p->sizecode) * sizeof(Instruction);
279
21.3M
    sz += cast_uint(p->sizelineinfo) * sizeof(lu_byte);
280
21.3M
    sz += cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo);
281
21.3M
  }
282
21.3M
  return sz;
283
21.3M
}
284
285
286
11.1M
void luaF_freeproto (lua_State *L, Proto *f) {
287
11.1M
  if (!(f->flag & PF_FIXED)) {
288
11.1M
    luaM_freearray(L, f->code, cast_sizet(f->sizecode));
289
11.1M
    luaM_freearray(L, f->lineinfo, cast_sizet(f->sizelineinfo));
290
11.1M
    luaM_freearray(L, f->abslineinfo, cast_sizet(f->sizeabslineinfo));
291
11.1M
  }
292
11.1M
  luaM_freearray(L, f->p, cast_sizet(f->sizep));
293
11.1M
  luaM_freearray(L, f->k, cast_sizet(f->sizek));
294
11.1M
  luaM_freearray(L, f->locvars, cast_sizet(f->sizelocvars));
295
11.1M
  luaM_freearray(L, f->upvalues, cast_sizet(f->sizeupvalues));
296
11.1M
  luaM_free(L, f);
297
11.1M
}
298
299
300
/*
301
** Look for n-th local variable at line 'line' in function 'func'.
302
** Returns NULL if not found.
303
*/
304
3.85M
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
305
3.85M
  int i;
306
35.8M
  for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
307
32.1M
    if (pc < f->locvars[i].endpc) {  /* is variable active? */
308
19.6M
      local_number--;
309
19.6M
      if (local_number == 0)
310
91.4k
        return getstr(f->locvars[i].varname);
311
19.6M
    }
312
32.1M
  }
313
3.76M
  return NULL;  /* not found */
314
3.85M
}
315