Coverage Report

Created: 2025-07-18 06:59

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