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


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:44