/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 | 38 | CClosure *luaF_newCclosure (lua_State *L, int nupvals) { |
28 | 38 | GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals)); |
29 | 38 | CClosure *c = gco2ccl(o); |
30 | 38 | c->nupvalues = cast_byte(nupvals); |
31 | 38 | return c; |
32 | 38 | } |
33 | | |
34 | | |
35 | 0 | LClosure *luaF_newLclosure (lua_State *L, int nupvals) { |
36 | 0 | GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals)); |
37 | 0 | LClosure *c = gco2lcl(o); |
38 | 0 | c->p = NULL; |
39 | 0 | c->nupvalues = cast_byte(nupvals); |
40 | 0 | while (nupvals--) c->upvals[nupvals] = NULL; |
41 | 0 | return c; |
42 | 0 | } |
43 | | |
44 | | |
45 | | /* |
46 | | ** fill a closure with new closed upvalues |
47 | | */ |
48 | 0 | void luaF_initupvals (lua_State *L, LClosure *cl) { |
49 | 0 | int i; |
50 | 0 | for (i = 0; i < cl->nupvalues; i++) { |
51 | 0 | GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); |
52 | 0 | UpVal *uv = gco2upv(o); |
53 | 0 | uv->v.p = &uv->u.value; /* make it closed */ |
54 | 0 | setnilvalue(uv->v.p); |
55 | 0 | cl->upvals[i] = uv; |
56 | 0 | luaC_objbarrier(L, cl, uv); |
57 | 0 | } |
58 | 0 | } |
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 message 'err'. The |
104 | | ** boolean 'yy' controls whether the call is yieldable. |
105 | | ** (This function assumes EXTRA_STACK.) |
106 | | */ |
107 | 0 | static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) { |
108 | 0 | StkId top = L->top.p; |
109 | 0 | const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); |
110 | 0 | setobj2s(L, top, tm); /* will call metamethod... */ |
111 | 0 | setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */ |
112 | 0 | setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */ |
113 | 0 | L->top.p = top + 3; /* add function and arguments */ |
114 | 0 | if (yy) |
115 | 0 | luaD_call(L, top, 0); |
116 | 0 | else |
117 | 0 | luaD_callnoyield(L, top, 0); |
118 | 0 | } |
119 | | |
120 | | |
121 | | /* |
122 | | ** Check whether object at given level has a close metamethod and raise |
123 | | ** an error if not. |
124 | | */ |
125 | 0 | static void checkclosemth (lua_State *L, StkId level) { |
126 | 0 | const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE); |
127 | 0 | 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 | 0 | } |
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 | 0 | static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) { |
144 | 0 | TValue *uv = s2v(level); /* value being closed */ |
145 | 0 | TValue *errobj; |
146 | 0 | if (status == CLOSEKTOP) |
147 | 0 | 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 | 0 | callclosemethod(L, uv, errobj, yy); |
153 | 0 | } |
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 | 0 | ((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 | 0 | void luaF_newtbcupval (lua_State *L, StkId level) { |
169 | 0 | lua_assert(level > L->tbclist.p); |
170 | 0 | if (l_isfalse(s2v(level))) |
171 | 0 | return; /* false doesn't need to be closed */ |
172 | 0 | checkclosemth(L, level); /* value must have a close method */ |
173 | 0 | 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 | 0 | level->tbclist.delta = cast(unsigned short, level - L->tbclist.p); |
178 | 0 | L->tbclist.p = level; |
179 | 0 | } |
180 | | |
181 | | |
182 | 0 | void luaF_unlinkupval (UpVal *uv) { |
183 | 0 | lua_assert(upisopen(uv)); |
184 | 0 | *uv->u.open.previous = uv->u.open.next; |
185 | 0 | if (uv->u.open.next) |
186 | 0 | uv->u.open.next->u.open.previous = uv->u.open.previous; |
187 | 0 | } |
188 | | |
189 | | |
190 | | /* |
191 | | ** Close all upvalues up to the given stack level. |
192 | | */ |
193 | 1.44k | void luaF_closeupval (lua_State *L, StkId level) { |
194 | 1.44k | UpVal *uv; |
195 | 1.44k | StkId upl; /* stack index pointed by 'uv' */ |
196 | 1.44k | while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) { |
197 | 0 | TValue *slot = &uv->u.value; /* new position for value */ |
198 | 0 | lua_assert(uplevel(uv) < L->top.p); |
199 | 0 | luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */ |
200 | 0 | setobj(L, slot, uv->v.p); /* move value to upvalue slot */ |
201 | 0 | uv->v.p = slot; /* now current value lives here */ |
202 | 0 | 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 | 0 | } |
207 | 1.44k | } |
208 | | |
209 | | |
210 | | /* |
211 | | ** Remove first element from the tbclist plus its dummy nodes. |
212 | | */ |
213 | 0 | static void poptbclist (lua_State *L) { |
214 | 0 | StkId tbc = L->tbclist.p; |
215 | 0 | lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */ |
216 | 0 | tbc -= tbc->tbclist.delta; |
217 | 0 | while (tbc > L->stack.p && tbc->tbclist.delta == 0) |
218 | 0 | tbc -= MAXDELTA; /* remove dummy nodes */ |
219 | 0 | L->tbclist.p = tbc; |
220 | 0 | } |
221 | | |
222 | | |
223 | | /* |
224 | | ** Close all upvalues and to-be-closed variables up to the given stack |
225 | | ** level. Return restored 'level'. |
226 | | */ |
227 | 1.10k | StkId luaF_close (lua_State *L, StkId level, int status, int yy) { |
228 | 1.10k | ptrdiff_t levelrel = savestack(L, level); |
229 | 1.10k | luaF_closeupval(L, level); /* first, close the upvalues */ |
230 | 1.10k | while (L->tbclist.p >= level) { /* traverse tbc's down to that level */ |
231 | 0 | StkId tbc = L->tbclist.p; /* get variable index */ |
232 | 0 | poptbclist(L); /* remove it from list */ |
233 | 0 | prepcallclosemth(L, tbc, status, yy); /* close variable */ |
234 | 0 | level = restorestack(L, levelrel); |
235 | 0 | } |
236 | 1.10k | return level; |
237 | 1.10k | } |
238 | | |
239 | | |
240 | 0 | Proto *luaF_newproto (lua_State *L) { |
241 | 0 | GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto)); |
242 | 0 | Proto *f = gco2p(o); |
243 | 0 | f->k = NULL; |
244 | 0 | f->sizek = 0; |
245 | 0 | f->p = NULL; |
246 | 0 | f->sizep = 0; |
247 | 0 | f->code = NULL; |
248 | 0 | f->sizecode = 0; |
249 | 0 | f->lineinfo = NULL; |
250 | 0 | f->sizelineinfo = 0; |
251 | 0 | f->abslineinfo = NULL; |
252 | 0 | f->sizeabslineinfo = 0; |
253 | 0 | f->upvalues = NULL; |
254 | 0 | f->sizeupvalues = 0; |
255 | 0 | f->numparams = 0; |
256 | 0 | f->is_vararg = 0; |
257 | 0 | f->maxstacksize = 0; |
258 | 0 | f->locvars = NULL; |
259 | 0 | f->sizelocvars = 0; |
260 | 0 | f->linedefined = 0; |
261 | 0 | f->lastlinedefined = 0; |
262 | 0 | f->source = NULL; |
263 | 0 | return f; |
264 | 0 | } |
265 | | |
266 | | |
267 | 0 | void luaF_freeproto (lua_State *L, Proto *f) { |
268 | 0 | luaM_freearray(L, f->code, f->sizecode); |
269 | 0 | luaM_freearray(L, f->p, f->sizep); |
270 | 0 | luaM_freearray(L, f->k, f->sizek); |
271 | 0 | luaM_freearray(L, f->lineinfo, f->sizelineinfo); |
272 | 0 | luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo); |
273 | 0 | luaM_freearray(L, f->locvars, f->sizelocvars); |
274 | 0 | luaM_freearray(L, f->upvalues, f->sizeupvalues); |
275 | 0 | luaM_free(L, f); |
276 | 0 | } |
277 | | |
278 | | |
279 | | /* |
280 | | ** Look for n-th local variable at line 'line' in function 'func'. |
281 | | ** Returns NULL if not found. |
282 | | */ |
283 | 0 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { |
284 | 0 | int i; |
285 | 0 | for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) { |
286 | 0 | if (pc < f->locvars[i].endpc) { /* is variable active? */ |
287 | 0 | local_number--; |
288 | 0 | if (local_number == 0) |
289 | 0 | return getstr(f->locvars[i].varname); |
290 | 0 | } |
291 | 0 | } |
292 | 0 | return NULL; /* not found */ |
293 | 0 | } |
294 | | |