Coverage Report

Created: 2026-01-25 07:04

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