Coverage Report

Created: 2023-09-20 06:32

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