1 /* BEGIN CSTYLED */ 2 /* 3 ** $Id: lfunc.c,v 2.30.1.1 2013/04/12 18:48:47 roberto Exp $ 4 ** Auxiliary functions to manipulate prototypes and closures 5 ** See Copyright Notice in lua.h 6 */ 7 8 9 #define lfunc_c 10 #define LUA_CORE 11 12 #include <sys/lua/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 22 Closure *luaF_newCclosure (lua_State *L, int n) { 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 29 Closure *luaF_newLclosure (lua_State *L, int n) { 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 38 UpVal *luaF_newupval (lua_State *L) { 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 46 UpVal *luaF_findupval (lua_State *L, StkId level) { 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 88 void luaF_close (lua_State *L, StkId level) { 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 109 Proto *luaF_newproto (lua_State *L) { 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 134 void luaF_freeproto (lua_State *L, Proto *f) { 135 luaM_freearray(L, f->code, f->sizecode); 136 luaM_freearray(L, f->p, f->sizep); 137 luaM_freearray(L, f->k, f->sizek); 138 luaM_freearray(L, f->lineinfo, f->sizelineinfo); 139 luaM_freearray(L, f->locvars, f->sizelocvars); 140 luaM_freearray(L, f->upvalues, f->sizeupvalues); 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 /* END CSTYLED */ 161