The Battle for Wesnoth  1.17.0-dev
lfunc.cpp
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 static void callclose (lua_State *L, void *ud) {
104  UNUSED(ud);
105  luaD_callnoyield(L, L->top - 3, 0);
106 }
107 
108 
109 /*
110 ** Prepare closing method plus its arguments for object 'obj' with
111 ** error message 'err'. (This function assumes EXTRA_STACK.)
112 */
113 static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) {
114  StkId top = L->top;
115  const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
116  if (ttisnil(tm)) /* no metamethod? */
117  return 0; /* nothing to call */
118  setobj2s(L, top, tm); /* will call metamethod... */
119  setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */
120  setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */
121  L->top = top + 3; /* add function and arguments */
122  return 1;
123 }
124 
125 
126 /*
127 ** Raise an error with message 'msg', inserting the name of the
128 ** local variable at position 'level' in the stack.
129 */
130 static void varerror (lua_State *L, StkId level, const char *msg) {
131  int idx = cast_int(level - L->ci->func);
132  const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
133  if (vname == NULL) vname = "?";
134  luaG_runerror(L, msg, vname);
135 }
136 
137 
138 /*
139 ** Prepare and call a closing method. If status is OK, code is still
140 ** inside the original protected call, and so any error will be handled
141 ** there. Otherwise, a previous error already activated the original
142 ** protected call, and so the call to the closing method must be
143 ** protected here. (A status == CLOSEPROTECT behaves like a previous
144 ** error, to also run the closing method in protected mode).
145 ** If status is OK, the call to the closing method will be pushed
146 ** at the top of the stack. Otherwise, values are pushed after
147 ** the 'level' of the upvalue being closed, as everything after
148 ** that won't be used again.
149 */
150 static int callclosemth (lua_State *L, StkId level, int status) {
151  TValue *uv = s2v(level); /* value being closed */
152  if (likely(status == LUA_OK)) {
153  if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */
154  callclose(L, NULL); /* call closing method */
155  else if (!l_isfalse(uv)) /* non-closable non-false value? */
156  varerror(L, level, "attempt to close non-closable variable '%s'");
157  }
158  else { /* must close the object in protected mode */
159  ptrdiff_t oldtop;
160  level++; /* space for error message */
161  oldtop = savestack(L, level + 1); /* top will be after that */
162  luaD_seterrorobj(L, status, level); /* set error message */
163  if (prepclosingmethod(L, uv, s2v(level))) { /* something to call? */
164  int newstatus = luaD_pcall(L, callclose, NULL, oldtop, 0);
165  if (newstatus != LUA_OK && status == CLOSEPROTECT) /* first error? */
166  status = newstatus; /* this will be the new error */
167  else {
168  if (newstatus != LUA_OK) /* suppressed error? */
169  luaE_warnerror(L, "__close metamethod");
170  /* leave original error (or nil) on top */
171  L->top = restorestack(L, oldtop);
172  }
173  }
174  /* else no metamethod; ignore this case and keep original error */
175  }
176  return status;
177 }
178 
179 
180 /*
181 ** Try to create a to-be-closed upvalue
182 ** (can raise a memory-allocation error)
183 */
184 static void trynewtbcupval (lua_State *L, void *ud) {
185  newupval(L, 1, cast(StkId, ud), &L->openupval);
186 }
187 
188 
189 /*
190 ** Create a to-be-closed upvalue. If there is a memory error
191 ** when creating the upvalue, the closing method must be called here,
192 ** as there is no upvalue to call it later.
193 */
195  TValue *obj = s2v(level);
196  lua_assert(L->openupval == NULL || uplevel(L->openupval) < level);
197  if (!l_isfalse(obj)) { /* false doesn't need to be closed */
198  int status;
199  const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
200  if (ttisnil(tm)) /* no metamethod? */
201  varerror(L, level, "variable '%s' got a non-closable value");
202  status = luaD_rawrunprotected(L, trynewtbcupval, level);
203  if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */
204  lua_assert(status == LUA_ERRMEM);
205  luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */
206  /* next call must succeed, as object is closable */
207  prepclosingmethod(L, s2v(level), s2v(level + 1));
208  callclose(L, NULL); /* call closing method */
209  luaD_throw(L, LUA_ERRMEM); /* throw memory error */
210  }
211  }
212 }
213 
214 
216  lua_assert(upisopen(uv));
217  *uv->u.open.previous = uv->u.open.next;
218  if (uv->u.open.next)
219  uv->u.open.next->u.open.previous = uv->u.open.previous;
220 }
221 
222 
223 int luaF_close (lua_State *L, StkId level, int status) {
224  UpVal *uv;
225  while ((uv = L->openupval) != NULL && uplevel(uv) >= level) {
226  TValue *slot = &uv->u.value; /* new position for value */
227  lua_assert(uplevel(uv) < L->top);
228  if (uv->tbc && status != NOCLOSINGMETH) {
229  /* must run closing method, which may change the stack */
230  ptrdiff_t levelrel = savestack(L, level);
231  status = callclosemth(L, uplevel(uv), status);
232  level = restorestack(L, levelrel);
233  }
234  luaF_unlinkupval(uv);
235  setobj(L, slot, uv->v); /* move value to upvalue slot */
236  uv->v = slot; /* now current value lives here */
237  if (!iswhite(uv)) { /* neither white nor dead? */
238  nw2black(uv); /* closed upvalues cannot be gray */
239  luaC_barrier(L, uv, slot);
240  }
241  }
242  return status;
243 }
244 
245 
247  GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
248  Proto *f = gco2p(o);
249  f->k = NULL;
250  f->sizek = 0;
251  f->p = NULL;
252  f->sizep = 0;
253  f->code = NULL;
254  f->sizecode = 0;
255  f->lineinfo = NULL;
256  f->sizelineinfo = 0;
257  f->abslineinfo = NULL;
258  f->sizeabslineinfo = 0;
259  f->upvalues = NULL;
260  f->sizeupvalues = 0;
261  f->numparams = 0;
262  f->is_vararg = 0;
263  f->maxstacksize = 0;
264  f->locvars = NULL;
265  f->sizelocvars = 0;
266  f->linedefined = 0;
267  f->lastlinedefined = 0;
268  f->source = NULL;
269  return f;
270 }
271 
272 
274  luaM_freearray(L, f->code, f->sizecode);
275  luaM_freearray(L, f->p, f->sizep);
276  luaM_freearray(L, f->k, f->sizek);
281  luaM_free(L, f);
282 }
283 
284 
285 /*
286 ** Look for n-th local variable at line 'line' in function 'func'.
287 ** Returns NULL if not found.
288 */
289 const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
290  int i;
291  for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
292  if (pc < f->locvars[i].endpc) { /* is variable active? */
293  local_number--;
294  if (local_number == 0)
295  return getstr(f->locvars[i].varname);
296  }
297  }
298  return NULL; /* not found */
299 }
300 
TString * source
Definition: lobject.h:551
l_noret luaG_runerror(lua_State *L, const char *fmt,...)
Definition: ldebug.cpp:767
#define s2v(o)
Definition: lobject.h:150
Definition: ltm.h:43
#define LUA_VCCL
Definition: lobject.h:570
void luaF_unlinkupval(UpVal *uv)
Definition: lfunc.cpp:215
#define luaM_freearray(L, b, n)
Definition: lmem.h:57
Definition: lobject.h:530
LClosure * luaF_newLclosure(lua_State *L, int nupvals)
Definition: lfunc.cpp:35
Proto * luaF_newproto(lua_State *L)
Definition: lfunc.cpp:246
static void callclose(lua_State *L, void *ud)
Definition: lfunc.cpp:103
#define gco2lcl(o)
Definition: lstate.h:333
#define iswhite(x)
Definition: lgc.h:87
static void varerror(lua_State *L, StkId level, const char *msg)
Definition: lfunc.cpp:130
int sizep
Definition: lobject.h:539
#define CLOSEPROTECT
Definition: lfunc.h:53
Definition: lobject.h:65
#define cast(t, exp)
Definition: llimits.h:123
#define cast_byte(i)
Definition: llimits.h:130
#define setnilvalue(obj)
Definition: lobject.h:178
#define G(L)
Definition: lstate.h:298
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:110
#define upisopen(up)
Definition: lfunc.h:32
CClosure * luaF_newCclosure(lua_State *L, int nupvals)
Definition: lfunc.cpp:27
LocVar * locvars
Definition: lobject.h:550
#define NOCLOSINGMETH
Definition: lfunc.h:50
AbsLineInfo * abslineinfo
Definition: lobject.h:549
const char * luaG_findlocal(lua_State *L, CallInfo *ci, int n, StkId *pos)
Definition: ldebug.cpp:200
#define luaC_barrier(L, p, v)
Definition: lgc.h:165
UpVal * upvals[1]
Definition: lobject.h:634
#define unlikely(x)
Definition: llimits.h:162
int sizelocvars
Definition: lobject.h:540
#define likely(x)
Definition: llimits.h:161
UpVal * luaF_findupval(lua_State *L, StkId level)
Definition: lfunc.cpp:88
ls_byte * lineinfo
Definition: lobject.h:548
Upvaldesc * upvalues
Definition: lobject.h:547
GCObject * luaC_newobj(lua_State *L, int tt, size_t sz)
Definition: lgc.cpp:258
StkId top
Definition: lstate.h:278
union UpVal::@5 u
#define restorestack(L, n)
Definition: ldo.h:36
const char * luaF_getlocalname(const Proto *f, int local_number, int pc)
Definition: lfunc.cpp:289
#define getstr(ts)
Definition: lobject.h:381
Definition: lobject.h:606
static void trynewtbcupval(lua_State *L, void *ud)
Definition: lfunc.cpp:184
#define nw2black(x)
Definition: lgc.h:99
TValue * v
Definition: lobject.h:609
void luaD_seterrorobj(lua_State *L, int errcode, StkId oldtop)
Definition: ldo.cpp:91
#define sizeCclosure(n)
Definition: lfunc.h:14
int luaD_pcall(lua_State *L, Pfunc func, void *u, ptrdiff_t old_top, ptrdiff_t ef)
Definition: ldo.cpp:777
#define uplevel(up)
Definition: lfunc.h:35
CallInfo * ci
Definition: lstate.h:280
#define ttisnil(v)
Definition: lobject.h:171
StkId func
Definition: lstate.h:161
#define isdead(g, v)
Definition: lgc.h:96
#define gco2p(o)
Definition: lstate.h:338
static UpVal * newupval(lua_State *L, int tbc, StkId level, UpVal **prev)
Definition: lfunc.cpp:65
int lastlinedefined
Definition: lobject.h:543
#define luaM_free(L, b)
Definition: lmem.h:56
TString * varname
Definition: lobject.h:506
int sizeabslineinfo
Definition: lobject.h:541
#define gco2ccl(o)
Definition: lstate.h:334
Instruction * code
Definition: lobject.h:545
struct Proto ** p
Definition: lobject.h:546
lu_byte maxstacksize
Definition: lobject.h:534
int sizelineinfo
Definition: lobject.h:538
l_noret luaD_throw(lua_State *L, int errcode)
Definition: ldo.cpp:114
const TValue * luaT_gettmbyobj(lua_State *L, const TValue *o, TMS event)
Definition: ltm.cpp:71
#define UNUSED(x)
Definition: global.hpp:31
void luaE_warnerror(lua_State *L, const char *where)
Definition: lstate.cpp:422
#define lua_assert(c)
Definition: llimits.h:101
std::size_t i
Definition: function.cpp:967
logger & err()
Definition: log.cpp:77
void luaF_newtbcupval(lua_State *L, StkId level)
Definition: lfunc.cpp:194
int luaF_close(lua_State *L, StkId level, int status)
Definition: lfunc.cpp:223
mock_party p
#define gco2upv(o)
Definition: lstate.h:340
#define setobj(L, obj1, obj2)
Definition: lobject.h:116
int sizek
Definition: lobject.h:536
UpVal * openupval
Definition: lstate.h:283
lu_byte is_vararg
Definition: lobject.h:533
struct lua_State * twups
Definition: lstate.h:285
#define LUA_VUPVAL
Definition: lobject.h:564
#define LUA_VPROTO
Definition: lobject.h:487
int linedefined
Definition: lobject.h:542
#define LUA_ERRMEM
Definition: lua.h:53
int luaD_rawrunprotected(lua_State *L, Pfunc f, void *ud)
Definition: ldo.cpp:141
int sizecode
Definition: lobject.h:537
static int callclosemth(lua_State *L, StkId level, int status)
Definition: lfunc.cpp:150
void luaD_callnoyield(lua_State *L, StkId func, int nResults)
Definition: ldo.cpp:565
#define savestack(L, p)
Definition: ldo.h:35
#define next(ls)
Definition: llex.cpp:32
void luaF_initupvals(lua_State *L, LClosure *cl)
Definition: lfunc.cpp:48
#define LUA_VLCL
Definition: lobject.h:568
struct UpVal::@5::@6 open
#define f
#define LUA_OK
Definition: lua.h:49
map_location prev
Definition: astarsearch.cpp:66
static int prepclosingmethod(lua_State *L, TValue *obj, TValue *err)
Definition: lfunc.cpp:113
#define cast_int(i)
Definition: llimits.h:128
struct Proto * p
Definition: lobject.h:633
TValue value
Definition: lobject.h:615
#define isintwups(L)
Definition: lfunc.h:22
int startpc
Definition: lobject.h:507
#define l_isfalse(o)
Definition: lobject.h:225
int sizeupvalues
Definition: lobject.h:535
void luaF_freeproto(lua_State *L, Proto *f)
Definition: lfunc.cpp:273
TValue * k
Definition: lobject.h:544
#define sizeLclosure(n)
Definition: lfunc.h:17
#define luaC_objbarrier(L, p, o)
Definition: lgc.h:173
mock_char c
lu_byte tbc
Definition: lobject.h:608
lu_byte numparams
Definition: lobject.h:532
#define setobj2s(L, o1, o2)
Definition: lobject.h:129