The Battle for Wesnoth  1.15.0-dev
lfunc.cpp
Go to the documentation of this file.
1 /*
2 ** $Id: lfunc.c,v 2.45 2014/11/02 19:19:04 roberto Exp $
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 "lfunc.h"
18 #include "lgc.h"
19 #include "lmem.h"
20 #include "lobject.h"
21 #include "lstate.h"
22 
23 
24 
27  CClosure *c = gco2ccl(o);
28  c->nupvalues = cast_byte(n);
29  return c;
30 }
31 
32 
35  LClosure *c = gco2lcl(o);
36  c->p = NULL;
37  c->nupvalues = cast_byte(n);
38  while (n--) c->upvals[n] = NULL;
39  return c;
40 }
41 
42 /*
43 ** fill a closure with new closed upvalues
44 */
46  int i;
47  for (i = 0; i < cl->nupvalues; i++) {
48  UpVal *uv = luaM_new(L, UpVal);
49  uv->refcount = 1;
50  uv->v = &uv->u.value; /* make it closed */
51  setnilvalue(uv->v);
52  cl->upvals[i] = uv;
53  }
54 }
55 
56 
58  UpVal **pp = &L->openupval;
59  UpVal *p;
60  UpVal *uv;
61  lua_assert(isintwups(L) || L->openupval == NULL);
62  while (*pp != NULL && (p = *pp)->v >= level) {
63  lua_assert(upisopen(p));
64  if (p->v == level) /* found a corresponding upvalue? */
65  return p; /* return it */
66  pp = &p->u.open.next;
67  }
68  /* not found: create a new upvalue */
69  uv = luaM_new(L, UpVal);
70  uv->refcount = 0;
71  uv->u.open.next = *pp; /* link it to list of open upvalues */
72  uv->u.open.touched = 1;
73  *pp = uv;
74  uv->v = level; /* current value lives in the stack */
75  if (!isintwups(L)) { /* thread not in list of threads with upvalues? */
76  L->twups = G(L)->twups; /* link it to the list */
77  G(L)->twups = L;
78  }
79  return uv;
80 }
81 
82 
84  UpVal *uv;
85  while (L->openupval != NULL && (uv = L->openupval)->v >= level) {
86  lua_assert(upisopen(uv));
87  L->openupval = uv->u.open.next; /* remove from 'open' list */
88  if (uv->refcount == 0) /* no references? */
89  luaM_free(L, uv); /* free upvalue */
90  else {
91  setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */
92  uv->v = &uv->u.value; /* now current value lives here */
93  luaC_upvalbarrier(L, uv);
94  }
95  }
96 }
97 
98 
100  GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto));
101  Proto *f = gco2p(o);
102  f->k = NULL;
103  f->sizek = 0;
104  f->p = NULL;
105  f->sizep = 0;
106  f->code = NULL;
107  f->cache = NULL;
108  f->sizecode = 0;
109  f->lineinfo = NULL;
110  f->sizelineinfo = 0;
111  f->upvalues = NULL;
112  f->sizeupvalues = 0;
113  f->numparams = 0;
114  f->is_vararg = 0;
115  f->maxstacksize = 0;
116  f->locvars = NULL;
117  f->sizelocvars = 0;
118  f->linedefined = 0;
119  f->lastlinedefined = 0;
120  f->source = NULL;
121  return f;
122 }
123 
124 
126  luaM_freearray(L, f->code, f->sizecode);
127  luaM_freearray(L, f->p, f->sizep);
128  luaM_freearray(L, f->k, f->sizek);
132  luaM_free(L, f);
133 }
134 
135 
136 /*
137 ** Look for n-th local variable at line 'line' in function 'func'.
138 ** Returns NULL if not found.
139 */
140 const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
141  int i;
142  for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
143  if (pc < f->locvars[i].endpc) { /* is variable active? */
144  local_number--;
145  if (local_number == 0)
146  return getstr(f->locvars[i].varname);
147  }
148  }
149  return NULL; /* not found */
150 }
151 
#define LUA_TCCL
Definition: lobject.h:49
TString * source
Definition: lobject.h:427
void luaF_close(lua_State *L, StkId level)
Definition: lfunc.cpp:83
#define luaM_freearray(L, b, n)
Definition: lmem.h:43
Definition: lobject.h:407
#define LUA_TPROTO
Definition: lobject.h:22
Proto * luaF_newproto(lua_State *L)
Definition: lfunc.cpp:99
#define gco2lcl(o)
Definition: lstate.h:210
int sizep
Definition: lobject.h:416
#define LUA_TLCL
Definition: lobject.h:47
#define cast_byte(i)
Definition: llimits.h:114
#define setnilvalue(obj)
Definition: lobject.h:210
#define G(L)
Definition: lstate.h:187
#define upisopen(up)
Definition: lfunc.h:47
LocVar * locvars
Definition: lobject.h:424
UpVal * upvals[1]
Definition: lobject.h:456
int sizelocvars
Definition: lobject.h:417
UpVal * luaF_findupval(lua_State *L, StkId level)
Definition: lfunc.cpp:57
Upvaldesc * upvalues
Definition: lobject.h:425
GCObject * luaC_newobj(lua_State *L, int tt, size_t sz)
Definition: lgc.cpp:208
CClosure * luaF_newCclosure(lua_State *L, int n)
Definition: lfunc.cpp:25
#define luaM_new(L, t)
Definition: lmem.h:46
struct LClosure * cache
Definition: lobject.h:426
const char * luaF_getlocalname(const Proto *f, int local_number, int pc)
Definition: lfunc.cpp:140
#define getstr(ts)
Definition: lobject.h:328
Definition: lfunc.h:35
TValue * v
Definition: lfunc.h:36
struct UpVal::@1::@2 open
#define sizeCclosure(n)
Definition: lfunc.h:14
#define gco2p(o)
Definition: lstate.h:215
int * lineinfo
Definition: lobject.h:423
int lastlinedefined
Definition: lobject.h:419
#define luaM_free(L, b)
Definition: lmem.h:42
TString * varname
Definition: lobject.h:398
#define gco2ccl(o)
Definition: lstate.h:211
Instruction * code
Definition: lobject.h:421
struct Proto ** p
Definition: lobject.h:422
#define luaC_upvalbarrier(L, uv)
Definition: lgc.h:130
lu_byte maxstacksize
Definition: lobject.h:411
int sizelineinfo
Definition: lobject.h:415
lu_mem refcount
Definition: lfunc.h:37
#define lua_assert(c)
Definition: llimits.h:89
std::size_t i
Definition: function.cpp:933
mock_party p
#define setobj(L, obj1, obj2)
Definition: lobject.h:259
int sizek
Definition: lobject.h:413
UpVal * openupval
Definition: lstate.h:170
lu_byte is_vararg
Definition: lobject.h:410
struct lua_State * twups
Definition: lstate.h:172
int linedefined
Definition: lobject.h:418
int sizecode
Definition: lobject.h:414
void luaF_initupvals(lua_State *L, LClosure *cl)
Definition: lfunc.cpp:45
LClosure * luaF_newLclosure(lua_State *L, int n)
Definition: lfunc.cpp:33
union UpVal::@1 u
#define f
struct Proto * p
Definition: lobject.h:455
TValue value
Definition: lfunc.h:43
#define isintwups(L)
Definition: lfunc.h:22
int startpc
Definition: lobject.h:399
int sizeupvalues
Definition: lobject.h:412
void luaF_freeproto(lua_State *L, Proto *f)
Definition: lfunc.cpp:125
TValue * k
Definition: lobject.h:420
#define sizeLclosure(n)
Definition: lfunc.h:17
mock_char c
static map_location::DIRECTION n
lu_byte numparams
Definition: lobject.h:409