1 /*
2 ** $Id$
3 ** Auxiliary functions to manipulate prototypes and closures
4 ** See Copyright Notice in lua.h
5 */
6
7
8 #define lfunc_c
9 #define LUA_CORE
10
11 #include "lua.h"
12
13 #include "lfunc.h"
14 #include "lgc.h"
15 #include "lmem.h"
16 #include "lobject.h"
17 #include "lstate.h"
18
19
20
luaF_newCclosure(lua_State * L,int nelems,Table * e)21 Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) {
22 Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
23 luaC_link(L, obj2gco(c), LUA_TFUNCTION);
24 c->c.isC = 1;
25 c->c.env = e;
26 c->c.nupvalues = cast_byte(nelems);
27 return c;
28 }
29
30
luaF_newLclosure(lua_State * L,int nelems,Table * e)31 Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) {
32 Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
33 luaC_link(L, obj2gco(c), LUA_TFUNCTION);
34 c->l.isC = 0;
35 c->l.env = e;
36 c->l.nupvalues = cast_byte(nelems);
37 while (nelems--) c->l.upvals[nelems] = NULL;
38 return c;
39 }
40
41
luaF_newupval(lua_State * L)42 UpVal *luaF_newupval (lua_State *L) {
43 UpVal *uv = luaM_new(L, UpVal);
44 luaC_link(L, obj2gco(uv), LUA_TUPVAL);
45 uv->v = &uv->u.value;
46 setnilvalue(uv->v);
47 return uv;
48 }
49
50
luaF_findupval(lua_State * L,StkId level)51 UpVal *luaF_findupval (lua_State *L, StkId level) {
52 global_State *g = G(L);
53 GCObject **pp = &L->openupval;
54 UpVal *p;
55 UpVal *uv;
56 while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) {
57 lua_assert(p->v != &p->u.value);
58 if (p->v == level) { /* found a corresponding upvalue? */
59 if (isdead(g, obj2gco(p))) /* is it dead? */
60 changewhite(obj2gco(p)); /* ressurect it */
61 return p;
62 }
63 pp = &p->next;
64 }
65 uv = luaM_new(L, UpVal); /* not found: create a new one */
66 uv->tt = LUA_TUPVAL;
67 uv->marked = luaC_white(g);
68 uv->v = level; /* current value lives in the stack */
69 uv->next = *pp; /* chain it in the proper position */
70 *pp = obj2gco(uv);
71 uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */
72 uv->u.l.next = g->uvhead.u.l.next;
73 uv->u.l.next->u.l.prev = uv;
74 g->uvhead.u.l.next = uv;
75 lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
76 return uv;
77 }
78
79
unlinkupval(UpVal * uv)80 static void unlinkupval (UpVal *uv) {
81 lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
82 uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */
83 uv->u.l.prev->u.l.next = uv->u.l.next;
84 }
85
86
luaF_freeupval(lua_State * L,UpVal * uv)87 void luaF_freeupval (lua_State *L, UpVal *uv) {
88 if (uv->v != &uv->u.value) /* is it open? */
89 unlinkupval(uv); /* remove from open list */
90 luaM_free(L, uv); /* free upvalue */
91 }
92
93
luaF_close(lua_State * L,StkId level)94 void luaF_close (lua_State *L, StkId level) {
95 UpVal *uv;
96 global_State *g = G(L);
97 while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) {
98 GCObject *o = obj2gco(uv);
99 lua_assert(!isblack(o) && uv->v != &uv->u.value);
100 L->openupval = uv->next; /* remove from `open' list */
101 if (isdead(g, o))
102 luaF_freeupval(L, uv); /* free upvalue */
103 else {
104 unlinkupval(uv);
105 setobj(L, &uv->u.value, uv->v);
106 uv->v = &uv->u.value; /* now current value lives here */
107 luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */
108 }
109 }
110 }
111
112
luaF_newproto(lua_State * L)113 Proto *luaF_newproto (lua_State *L) {
114 Proto *f = luaM_new(L, Proto);
115 luaC_link(L, obj2gco(f), LUA_TPROTO);
116 f->k = NULL;
117 f->sizek = 0;
118 f->p = NULL;
119 f->sizep = 0;
120 f->code = NULL;
121 f->sizecode = 0;
122 f->sizelineinfo = 0;
123 f->sizeupvalues = 0;
124 f->nups = 0;
125 f->upvalues = NULL;
126 f->numparams = 0;
127 f->is_vararg = 0;
128 f->maxstacksize = 0;
129 f->lineinfo = NULL;
130 f->sizelocvars = 0;
131 f->locvars = NULL;
132 f->linedefined = 0;
133 f->lastlinedefined = 0;
134 f->source = NULL;
135 return f;
136 }
137
138
luaF_freeproto(lua_State * L,Proto * f)139 void luaF_freeproto (lua_State *L, Proto *f) {
140 luaM_freearray(L, f->code, f->sizecode, Instruction);
141 luaM_freearray(L, f->p, f->sizep, Proto *);
142 luaM_freearray(L, f->k, f->sizek, TValue);
143 luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
144 luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
145 luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *);
146 luaM_free(L, f);
147 }
148
149
luaF_freeclosure(lua_State * L,Closure * c)150 void luaF_freeclosure (lua_State *L, Closure *c) {
151 int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
152 sizeLclosure(c->l.nupvalues);
153 luaM_freemem(L, c, size);
154 }
155
156
157 /*
158 ** Look for n-th local variable at line `line' in function `func'.
159 ** Returns NULL if not found.
160 */
luaF_getlocalname(const Proto * f,int local_number,int pc)161 const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
162 int i;
163 for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
164 if (pc < f->locvars[i].endpc) { /* is variable active? */
165 local_number--;
166 if (local_number == 0)
167 return getstr(f->locvars[i].varname);
168 }
169 }
170 return NULL; /* not found */
171 }
172