Mercurial > vba-clojure
diff src/lua/lfunc.c @ 11:27763b933818
raise lua sources up one level
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 11:07:39 -0600 |
parents | src/lua/src/lfunc.c@f9f4f1b99eed |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/lua/lfunc.c Sat Mar 03 11:07:39 2012 -0600 1.3 @@ -0,0 +1,174 @@ 1.4 +/* 1.5 +** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $ 1.6 +** Auxiliary functions to manipulate prototypes and closures 1.7 +** See Copyright Notice in lua.h 1.8 +*/ 1.9 + 1.10 + 1.11 +#include <stddef.h> 1.12 + 1.13 +#define lfunc_c 1.14 +#define LUA_CORE 1.15 + 1.16 +#include "lua.h" 1.17 + 1.18 +#include "lfunc.h" 1.19 +#include "lgc.h" 1.20 +#include "lmem.h" 1.21 +#include "lobject.h" 1.22 +#include "lstate.h" 1.23 + 1.24 + 1.25 + 1.26 +Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) { 1.27 + Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); 1.28 + luaC_link(L, obj2gco(c), LUA_TFUNCTION); 1.29 + c->c.isC = 1; 1.30 + c->c.env = e; 1.31 + c->c.nupvalues = cast_byte(nelems); 1.32 + return c; 1.33 +} 1.34 + 1.35 + 1.36 +Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) { 1.37 + Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); 1.38 + luaC_link(L, obj2gco(c), LUA_TFUNCTION); 1.39 + c->l.isC = 0; 1.40 + c->l.env = e; 1.41 + c->l.nupvalues = cast_byte(nelems); 1.42 + while (nelems--) c->l.upvals[nelems] = NULL; 1.43 + return c; 1.44 +} 1.45 + 1.46 + 1.47 +UpVal *luaF_newupval (lua_State *L) { 1.48 + UpVal *uv = luaM_new(L, UpVal); 1.49 + luaC_link(L, obj2gco(uv), LUA_TUPVAL); 1.50 + uv->v = &uv->u.value; 1.51 + setnilvalue(uv->v); 1.52 + return uv; 1.53 +} 1.54 + 1.55 + 1.56 +UpVal *luaF_findupval (lua_State *L, StkId level) { 1.57 + global_State *g = G(L); 1.58 + GCObject **pp = &L->openupval; 1.59 + UpVal *p; 1.60 + UpVal *uv; 1.61 + while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) { 1.62 + lua_assert(p->v != &p->u.value); 1.63 + if (p->v == level) { /* found a corresponding upvalue? */ 1.64 + if (isdead(g, obj2gco(p))) /* is it dead? */ 1.65 + changewhite(obj2gco(p)); /* ressurect it */ 1.66 + return p; 1.67 + } 1.68 + pp = &p->next; 1.69 + } 1.70 + uv = luaM_new(L, UpVal); /* not found: create a new one */ 1.71 + uv->tt = LUA_TUPVAL; 1.72 + uv->marked = luaC_white(g); 1.73 + uv->v = level; /* current value lives in the stack */ 1.74 + uv->next = *pp; /* chain it in the proper position */ 1.75 + *pp = obj2gco(uv); 1.76 + uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ 1.77 + uv->u.l.next = g->uvhead.u.l.next; 1.78 + uv->u.l.next->u.l.prev = uv; 1.79 + g->uvhead.u.l.next = uv; 1.80 + lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 1.81 + return uv; 1.82 +} 1.83 + 1.84 + 1.85 +static void unlinkupval (UpVal *uv) { 1.86 + lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 1.87 + uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ 1.88 + uv->u.l.prev->u.l.next = uv->u.l.next; 1.89 +} 1.90 + 1.91 + 1.92 +void luaF_freeupval (lua_State *L, UpVal *uv) { 1.93 + if (uv->v != &uv->u.value) /* is it open? */ 1.94 + unlinkupval(uv); /* remove from open list */ 1.95 + luaM_free(L, uv); /* free upvalue */ 1.96 +} 1.97 + 1.98 + 1.99 +void luaF_close (lua_State *L, StkId level) { 1.100 + UpVal *uv; 1.101 + global_State *g = G(L); 1.102 + while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) { 1.103 + GCObject *o = obj2gco(uv); 1.104 + lua_assert(!isblack(o) && uv->v != &uv->u.value); 1.105 + L->openupval = uv->next; /* remove from `open' list */ 1.106 + if (isdead(g, o)) 1.107 + luaF_freeupval(L, uv); /* free upvalue */ 1.108 + else { 1.109 + unlinkupval(uv); 1.110 + setobj(L, &uv->u.value, uv->v); 1.111 + uv->v = &uv->u.value; /* now current value lives here */ 1.112 + luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */ 1.113 + } 1.114 + } 1.115 +} 1.116 + 1.117 + 1.118 +Proto *luaF_newproto (lua_State *L) { 1.119 + Proto *f = luaM_new(L, Proto); 1.120 + luaC_link(L, obj2gco(f), LUA_TPROTO); 1.121 + f->k = NULL; 1.122 + f->sizek = 0; 1.123 + f->p = NULL; 1.124 + f->sizep = 0; 1.125 + f->code = NULL; 1.126 + f->sizecode = 0; 1.127 + f->sizelineinfo = 0; 1.128 + f->sizeupvalues = 0; 1.129 + f->nups = 0; 1.130 + f->upvalues = NULL; 1.131 + f->numparams = 0; 1.132 + f->is_vararg = 0; 1.133 + f->maxstacksize = 0; 1.134 + f->lineinfo = NULL; 1.135 + f->sizelocvars = 0; 1.136 + f->locvars = NULL; 1.137 + f->linedefined = 0; 1.138 + f->lastlinedefined = 0; 1.139 + f->source = NULL; 1.140 + return f; 1.141 +} 1.142 + 1.143 + 1.144 +void luaF_freeproto (lua_State *L, Proto *f) { 1.145 + luaM_freearray(L, f->code, f->sizecode, Instruction); 1.146 + luaM_freearray(L, f->p, f->sizep, Proto *); 1.147 + luaM_freearray(L, f->k, f->sizek, TValue); 1.148 + luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); 1.149 + luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); 1.150 + luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); 1.151 + luaM_free(L, f); 1.152 +} 1.153 + 1.154 + 1.155 +void luaF_freeclosure (lua_State *L, Closure *c) { 1.156 + int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : 1.157 + sizeLclosure(c->l.nupvalues); 1.158 + luaM_freemem(L, c, size); 1.159 +} 1.160 + 1.161 + 1.162 +/* 1.163 +** Look for n-th local variable at line `line' in function `func'. 1.164 +** Returns NULL if not found. 1.165 +*/ 1.166 +const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 1.167 + int i; 1.168 + for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) { 1.169 + if (pc < f->locvars[i].endpc) { /* is variable active? */ 1.170 + local_number--; 1.171 + if (local_number == 0) 1.172 + return getstr(f->locvars[i].varname); 1.173 + } 1.174 + } 1.175 + return NULL; /* not found */ 1.176 +} 1.177 +