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