rlm@1: /* rlm@1: ** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $ rlm@1: ** Auxiliary functions to manipulate prototypes and closures rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: rlm@1: #include rlm@1: rlm@1: #define lfunc_c rlm@1: #define LUA_CORE rlm@1: rlm@1: #include "lua.h" rlm@1: rlm@1: #include "lfunc.h" rlm@1: #include "lgc.h" rlm@1: #include "lmem.h" rlm@1: #include "lobject.h" rlm@1: #include "lstate.h" rlm@1: rlm@1: rlm@1: rlm@1: Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) { rlm@1: Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); rlm@1: luaC_link(L, obj2gco(c), LUA_TFUNCTION); rlm@1: c->c.isC = 1; rlm@1: c->c.env = e; rlm@1: c->c.nupvalues = cast_byte(nelems); rlm@1: return c; rlm@1: } rlm@1: rlm@1: rlm@1: Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) { rlm@1: Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); rlm@1: luaC_link(L, obj2gco(c), LUA_TFUNCTION); rlm@1: c->l.isC = 0; rlm@1: c->l.env = e; rlm@1: c->l.nupvalues = cast_byte(nelems); rlm@1: while (nelems--) c->l.upvals[nelems] = NULL; rlm@1: return c; rlm@1: } rlm@1: rlm@1: rlm@1: UpVal *luaF_newupval (lua_State *L) { rlm@1: UpVal *uv = luaM_new(L, UpVal); rlm@1: luaC_link(L, obj2gco(uv), LUA_TUPVAL); rlm@1: uv->v = &uv->u.value; rlm@1: setnilvalue(uv->v); rlm@1: return uv; rlm@1: } rlm@1: rlm@1: rlm@1: UpVal *luaF_findupval (lua_State *L, StkId level) { rlm@1: global_State *g = G(L); rlm@1: GCObject **pp = &L->openupval; rlm@1: UpVal *p; rlm@1: UpVal *uv; rlm@1: while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) { rlm@1: lua_assert(p->v != &p->u.value); rlm@1: if (p->v == level) { /* found a corresponding upvalue? */ rlm@1: if (isdead(g, obj2gco(p))) /* is it dead? */ rlm@1: changewhite(obj2gco(p)); /* ressurect it */ rlm@1: return p; rlm@1: } rlm@1: pp = &p->next; rlm@1: } rlm@1: uv = luaM_new(L, UpVal); /* not found: create a new one */ rlm@1: uv->tt = LUA_TUPVAL; rlm@1: uv->marked = luaC_white(g); rlm@1: uv->v = level; /* current value lives in the stack */ rlm@1: uv->next = *pp; /* chain it in the proper position */ rlm@1: *pp = obj2gco(uv); rlm@1: uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ rlm@1: uv->u.l.next = g->uvhead.u.l.next; rlm@1: uv->u.l.next->u.l.prev = uv; rlm@1: g->uvhead.u.l.next = uv; rlm@1: lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); rlm@1: return uv; rlm@1: } rlm@1: rlm@1: rlm@1: static void unlinkupval (UpVal *uv) { rlm@1: lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); rlm@1: uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ rlm@1: uv->u.l.prev->u.l.next = uv->u.l.next; rlm@1: } rlm@1: rlm@1: rlm@1: void luaF_freeupval (lua_State *L, UpVal *uv) { rlm@1: if (uv->v != &uv->u.value) /* is it open? */ rlm@1: unlinkupval(uv); /* remove from open list */ rlm@1: luaM_free(L, uv); /* free upvalue */ rlm@1: } rlm@1: rlm@1: rlm@1: void luaF_close (lua_State *L, StkId level) { rlm@1: UpVal *uv; rlm@1: global_State *g = G(L); rlm@1: while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) { rlm@1: GCObject *o = obj2gco(uv); rlm@1: lua_assert(!isblack(o) && uv->v != &uv->u.value); rlm@1: L->openupval = uv->next; /* remove from `open' list */ rlm@1: if (isdead(g, o)) rlm@1: luaF_freeupval(L, uv); /* free upvalue */ rlm@1: else { rlm@1: unlinkupval(uv); rlm@1: setobj(L, &uv->u.value, uv->v); rlm@1: uv->v = &uv->u.value; /* now current value lives here */ rlm@1: luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */ rlm@1: } rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: Proto *luaF_newproto (lua_State *L) { rlm@1: Proto *f = luaM_new(L, Proto); rlm@1: luaC_link(L, obj2gco(f), LUA_TPROTO); rlm@1: f->k = NULL; rlm@1: f->sizek = 0; rlm@1: f->p = NULL; rlm@1: f->sizep = 0; rlm@1: f->code = NULL; rlm@1: f->sizecode = 0; rlm@1: f->sizelineinfo = 0; rlm@1: f->sizeupvalues = 0; rlm@1: f->nups = 0; rlm@1: f->upvalues = NULL; rlm@1: f->numparams = 0; rlm@1: f->is_vararg = 0; rlm@1: f->maxstacksize = 0; rlm@1: f->lineinfo = NULL; rlm@1: f->sizelocvars = 0; rlm@1: f->locvars = NULL; rlm@1: f->linedefined = 0; rlm@1: f->lastlinedefined = 0; rlm@1: f->source = NULL; rlm@1: return f; rlm@1: } rlm@1: rlm@1: rlm@1: void luaF_freeproto (lua_State *L, Proto *f) { rlm@1: luaM_freearray(L, f->code, f->sizecode, Instruction); rlm@1: luaM_freearray(L, f->p, f->sizep, Proto *); rlm@1: luaM_freearray(L, f->k, f->sizek, TValue); rlm@1: luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); rlm@1: luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); rlm@1: luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); rlm@1: luaM_free(L, f); rlm@1: } rlm@1: rlm@1: rlm@1: void luaF_freeclosure (lua_State *L, Closure *c) { rlm@1: int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : rlm@1: sizeLclosure(c->l.nupvalues); rlm@1: luaM_freemem(L, c, size); rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** Look for n-th local variable at line `line' in function `func'. rlm@1: ** Returns NULL if not found. rlm@1: */ rlm@1: const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { rlm@1: int i; rlm@1: for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { rlm@1: if (pc < f->locvars[i].endpc) { /* is variable active? */ rlm@1: local_number--; rlm@1: if (local_number == 0) rlm@1: return getstr(f->locvars[i].varname); rlm@1: } rlm@1: } rlm@1: return NULL; /* not found */ rlm@1: } rlm@1: