lua/lfunc.c

147 lines
3.5 KiB
C
Raw Normal View History

1997-09-16 23:25:59 +04:00
/*
** $Id: lfunc.c,v 2.40 2014/02/15 13:12:01 roberto Exp roberto $
1998-06-19 20:14:09 +04:00
** Auxiliary functions to manipulate prototypes and closures
1997-09-16 23:25:59 +04:00
** See Copyright Notice in lua.h
*/
2003-10-02 23:21:09 +04:00
#include <stddef.h>
1997-09-16 23:25:59 +04:00
2002-12-04 20:38:31 +03:00
#define lfunc_c
#define LUA_CORE
2002-12-04 20:38:31 +03:00
#include "lua.h"
1997-09-16 23:25:59 +04:00
#include "lfunc.h"
#include "lgc.h"
1997-09-16 23:25:59 +04:00
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
1997-09-16 23:25:59 +04:00
2010-03-26 23:58:11 +03:00
Closure *luaF_newCclosure (lua_State *L, int n) {
Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n))->cl;
c->c.nupvalues = cast_byte(n);
1997-09-16 23:25:59 +04:00
return c;
}
Closure *luaF_newLclosure (lua_State *L, int n) {
Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n))->cl;
c->l.p = NULL;
c->l.nupvalues = cast_byte(n);
while (n--) c->l.upvals[n] = NULL;
return c;
}
/*
** fill a closure with new closed upvalues
*/
2013-08-27 22:53:35 +04:00
void luaF_initupvals (lua_State *L, LClosure *cl) {
int i;
for (i = 0; i < cl->nupvalues; i++) {
UpVal *uv = luaM_new(L, UpVal);
uv->refcount = 1;
uv->v = &uv->u.value; /* make it closed */
setnilvalue(uv->v);
cl->upvals[i] = uv;
}
}
UpVal *luaF_findupval (lua_State *L, StkId level) {
2013-08-27 22:53:35 +04:00
UpVal **pp = &L->openupval;
2002-11-13 14:32:26 +03:00
UpVal *p;
UpVal *uv;
lua_assert(isintwups(L) || L->openupval == NULL);
2013-08-27 22:53:35 +04:00
while (*pp != NULL && (p = *pp)->v >= level) {
lua_assert(upisopen(p));
if (p->v == level) /* found a corresponding upvalue? */
return p; /* return it */
2014-02-15 17:12:01 +04:00
pp = &p->u.open.next;
}
/* not found: create a new upvalue */
2013-08-27 22:53:35 +04:00
uv = luaM_new(L, UpVal);
uv->refcount = 0;
uv->u.open.next = *pp; /* link it to list of open upvalues */
uv->u.open.touched = 1;
2013-08-27 22:53:35 +04:00
*pp = uv;
uv->v = level; /* current value lives in the stack */
if (!isintwups(L)) { /* thread not in list of threads with upvalues? */
L->twups = G(L)->twups; /* link it to the list */
G(L)->twups = L;
}
return uv;
}
void luaF_close (lua_State *L, StkId level) {
UpVal *uv;
2013-08-27 22:53:35 +04:00
while (L->openupval != NULL && (uv = L->openupval)->v >= level) {
lua_assert(upisopen(uv));
2014-02-15 17:12:01 +04:00
L->openupval = uv->u.open.next; /* remove from `open' list */
2013-08-27 22:53:35 +04:00
if (uv->refcount == 0) /* no references? */
luaM_free(L, uv); /* free upvalue */
2004-03-16 00:04:33 +03:00
else {
2013-08-27 22:53:35 +04:00
setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */
uv->v = &uv->u.value; /* now current value lives here */
luaC_upvalbarrier(L, uv);
2004-03-16 00:04:33 +03:00
}
}
}
2000-03-10 21:37:44 +03:00
Proto *luaF_newproto (lua_State *L) {
Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto))->p;
f->k = NULL;
f->sizek = 0;
2001-06-28 18:57:17 +04:00
f->p = NULL;
f->sizep = 0;
1997-09-16 23:25:59 +04:00
f->code = NULL;
2010-06-04 17:25:10 +04:00
f->cache = NULL;
f->sizecode = 0;
f->lineinfo = NULL;
f->sizelineinfo = 0;
f->upvalues = NULL;
f->sizeupvalues = 0;
2000-10-10 23:52:58 +04:00
f->numparams = 0;
f->is_vararg = 0;
f->maxstacksize = 0;
f->locvars = NULL;
f->sizelocvars = 0;
f->linedefined = 0;
f->lastlinedefined = 0;
f->source = NULL;
1997-09-16 23:25:59 +04:00
return f;
}
2000-03-10 21:37:44 +03:00
void luaF_freeproto (lua_State *L, Proto *f) {
luaM_freearray(L, f->code, f->sizecode);
luaM_freearray(L, f->p, f->sizep);
luaM_freearray(L, f->k, f->sizek);
luaM_freearray(L, f->lineinfo, f->sizelineinfo);
luaM_freearray(L, f->locvars, f->sizelocvars);
luaM_freearray(L, f->upvalues, f->sizeupvalues);
2004-11-24 22:20:21 +03:00
luaM_free(L, f);
1997-09-16 23:25:59 +04:00
}
/*
1999-12-27 20:33:22 +03:00
** Look for n-th local variable at line `line' in function `func'.
1997-09-16 23:25:59 +04:00
** Returns NULL if not found.
*/
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
int i;
for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
if (pc < f->locvars[i].endpc) { /* is variable active? */
local_number--;
if (local_number == 0)
return getstr(f->locvars[i].varname);
1997-09-16 23:25:59 +04:00
}
}
return NULL; /* not found */
1997-09-16 23:25:59 +04:00
}