Coverage Report

Created: 2023-08-27 06:20

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