The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lfunc.cpp
Go to the documentation of this file.
1 /*
2 ** Auxiliary functions to manipulate prototypes and closures
3 ** See Copyright Notice in lua.h
4 */
5 
6 
7 #include <stddef.h>
8 
9 #define lfunc_c
10 #define LUA_CORE
11 
12 #include "lua.h"
13 
14 #include "lfunc.h"
15 #include "lgc.h"
16 #include "lmem.h"
17 #include "lobject.h"
18 #include "lstate.h"
19 
20 
21 
23  Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n), NULL, 0)->cl;
24  c->c.nupvalues = cast_byte(n);
25  return c;
26 }
27 
28 
30  Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl;
31  c->l.p = NULL;
32  c->l.nupvalues = cast_byte(n);
33  while (n--) c->l.upvals[n] = NULL;
34  return c;
35 }
36 
37 
39  UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv;
40  uv->v = &uv->u.value;
41  setnilvalue(uv->v);
42  return uv;
43 }
44 
45 
47  global_State *g = G(L);
48  GCObject **pp = &L->openupval;
49  UpVal *p;
50  UpVal *uv;
51  while (*pp != NULL && (p = gco2uv(*pp))->v >= level) {
52  GCObject *o = obj2gco(p);
53  lua_assert(p->v != &p->u.value);
54  lua_assert(!isold(o) || isold(obj2gco(L)));
55  if (p->v == level) { /* found a corresponding upvalue? */
56  if (isdead(g, o)) /* is it dead? */
57  changewhite(o); /* resurrect it */
58  return p;
59  }
60  pp = &p->next;
61  }
62  /* not found: create a new one */
63  uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv;
64  uv->v = level; /* current value lives in the stack */
65  uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */
66  uv->u.l.next = g->uvhead.u.l.next;
67  uv->u.l.next->u.l.prev = uv;
68  g->uvhead.u.l.next = uv;
69  lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
70  return uv;
71 }
72 
73 
74 static void unlinkupval (UpVal *uv) {
75  lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
76  uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */
77  uv->u.l.prev->u.l.next = uv->u.l.next;
78 }
79 
80 
81 void luaF_freeupval (lua_State *L, UpVal *uv) {
82  if (uv->v != &uv->u.value) /* is it open? */
83  unlinkupval(uv); /* remove from open list */
84  luaM_free(L, uv); /* free upvalue */
85 }
86 
87 
89  UpVal *uv;
90  global_State *g = G(L);
91  while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) {
92  GCObject *o = obj2gco(uv);
93  lua_assert(!isblack(o) && uv->v != &uv->u.value);
94  L->openupval = uv->next; /* remove from `open' list */
95  if (isdead(g, o))
96  luaF_freeupval(L, uv); /* free upvalue */
97  else {
98  unlinkupval(uv); /* remove upvalue from 'uvhead' list */
99  setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */
100  uv->v = &uv->u.value; /* now current value lives here */
101  gch(o)->next = g->allgc; /* link upvalue into 'allgc' list */
102  g->allgc = o;
103  luaC_checkupvalcolor(g, uv);
104  }
105  }
106 }
107 
108 
110  Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p;
111  f->k = NULL;
112  f->sizek = 0;
113  f->p = NULL;
114  f->sizep = 0;
115  f->code = NULL;
116  f->cache = NULL;
117  f->sizecode = 0;
118  f->lineinfo = NULL;
119  f->sizelineinfo = 0;
120  f->upvalues = NULL;
121  f->sizeupvalues = 0;
122  f->numparams = 0;
123  f->is_vararg = 0;
124  f->maxstacksize = 0;
125  f->locvars = NULL;
126  f->sizelocvars = 0;
127  f->linedefined = 0;
128  f->lastlinedefined = 0;
129  f->source = NULL;
130  return f;
131 }
132 
133 
135  luaM_freearray(L, f->code, f->sizecode);
136  luaM_freearray(L, f->p, f->sizep);
137  luaM_freearray(L, f->k, f->sizek);
141  luaM_free(L, f);
142 }
143 
144 
145 /*
146 ** Look for n-th local variable at line `line' in function `func'.
147 ** Returns NULL if not found.
148 */
149 const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
150  int i;
151  for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
152  if (pc < f->locvars[i].endpc) { /* is variable active? */
153  local_number--;
154  if (local_number == 0)
155  return getstr(f->locvars[i].varname);
156  }
157  }
158  return NULL; /* not found */
159 }
160 
Definition: plugin.cc:260
#define LUA_TCCL
Definition: lobject.h:51
#define changewhite(x)
Definition: lgc.h:113
TString * source
Definition: lobject.h:475
#define LUA_TUPVAL
Definition: lobject.h:22
void luaF_close(lua_State *L, StkId level)
Definition: lfunc.cpp:88
union Closure * cache
Definition: lobject.h:474
struct UpVal::@9::@10 l
#define obj2gco(v)
Definition: lstate.h:214
GLint level
Definition: glew.h:1220
#define luaM_freearray(L, b, n)
Definition: lmem.h:30
Definition: lobject.h:466
const GLfloat * c
Definition: glew.h:12741
#define LUA_TPROTO
Definition: lobject.h:21
Closure * luaF_newLclosure(lua_State *L, int n)
Definition: lfunc.cpp:29
Proto * luaF_newproto(lua_State *L)
Definition: lfunc.cpp:109
CClosure c
Definition: lobject.h:530
int sizep
Definition: lobject.h:480
#define LUA_TLCL
Definition: lobject.h:49
GCObject * luaC_newobj(lua_State *L, int tt, size_t sz, GCObject **list, int offset)
Definition: lgc.cpp:211
#define cast_byte(i)
Definition: llimits.h:94
#define setnilvalue(obj)
Definition: lobject.h:189
#define G(L)
Definition: lstate.h:178
#define gco2uv(o)
Definition: lstate.h:210
GLboolean GLboolean g
Definition: glew.h:7319
LocVar * locvars
Definition: lobject.h:472
UpVal * upvals[1]
Definition: lobject.h:525
#define isold(x)
Definition: lgc.h:103
GCObject * allgc
Definition: lstate.h:126
int sizelocvars
Definition: lobject.h:481
UpVal * luaF_findupval(lua_State *L, StkId level)
Definition: lfunc.cpp:46
Upvaldesc * upvalues
Definition: lobject.h:473
void luaC_checkupvalcolor(global_State *g, UpVal *uv)
Definition: lgc.cpp:189
struct UpVal uv
Definition: lstate.h:191
const char * luaF_getlocalname(const Proto *f, int local_number, int pc)
Definition: lfunc.cpp:149
#define getstr(ts)
Definition: lobject.h:421
Definition: lobject.h:495
TValue * v
Definition: lobject.h:497
struct Proto p
Definition: lstate.h:190
#define sizeCclosure(n)
Definition: lfunc.h:13
const GLdouble * v
Definition: glew.h:1359
LClosure l
Definition: lobject.h:531
struct UpVal * next
Definition: lobject.h:502
#define isdead(g, v)
Definition: lgc.h:111
int * lineinfo
Definition: lobject.h:471
int lastlinedefined
Definition: lobject.h:483
#define luaM_free(L, b)
Definition: lmem.h:29
TString * varname
Definition: lobject.h:457
GLfloat GLfloat p
Definition: glew.h:12766
Closure * luaF_newCclosure(lua_State *L, int n)
Definition: lfunc.cpp:22
Instruction * code
Definition: lobject.h:469
struct Proto ** p
Definition: lobject.h:470
lu_byte maxstacksize
Definition: lobject.h:487
int sizelineinfo
Definition: lobject.h:479
#define lua_assert(c)
Definition: llimits.h:65
#define gch(o)
Definition: lstate.h:196
#define setobj(L, obj1, obj2)
Definition: lobject.h:239
UpVal uvhead
Definition: lstate.h:136
int sizek
Definition: lobject.h:477
size_t i
Definition: function.cpp:1057
lu_byte is_vararg
Definition: lobject.h:486
int linedefined
Definition: lobject.h:482
int sizecode
Definition: lobject.h:478
#define isblack(x)
Definition: lgc.h:99
UpVal * luaF_newupval(lua_State *L)
Definition: lfunc.cpp:38
GLclampd n
Definition: glew.h:5903
void luaF_freeupval(lua_State *L, UpVal *uv)
Definition: lfunc.cpp:81
struct Proto * p
Definition: lobject.h:524
TValue value
Definition: lobject.h:499
#define c
Definition: glew.h:12743
int startpc
Definition: lobject.h:458
int sizeupvalues
Definition: lobject.h:476
static void unlinkupval(UpVal *uv)
Definition: lfunc.cpp:74
void luaF_freeproto(lua_State *L, Proto *f)
Definition: lfunc.cpp:134
TValue * k
Definition: lobject.h:468
#define sizeLclosure(n)
Definition: lfunc.h:16
union UpVal::@9 u
GCObject * openupval
Definition: lstate.h:170
lu_byte numparams
Definition: lobject.h:485
GLclampf f
Definition: glew.h:3024
union Closure cl
Definition: lstate.h:188