2011-08-08 15:19:44 -06:00
|
|
|
/*
|
2016-07-26 21:23:26 -06:00
|
|
|
** $Id: lfunc.c,v 2.45 2014/11/02 19:19:04 roberto Exp $
|
2011-08-08 15:19:44 -06:00
|
|
|
** Auxiliary functions to manipulate prototypes and closures
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define lfunc_c
|
|
|
|
#define LUA_CORE
|
|
|
|
|
2016-07-26 21:23:26 -06:00
|
|
|
#include "lprefix.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2011-08-08 15:19:44 -06:00
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lgc.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.h"
|
|
|
|
#include "lstate.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-26 21:23:26 -06:00
|
|
|
CClosure *luaF_newCclosure (lua_State *L, int n) {
|
|
|
|
GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n));
|
|
|
|
CClosure *c = gco2ccl(o);
|
|
|
|
c->nupvalues = cast_byte(n);
|
2011-08-08 15:19:44 -06:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-26 21:23:26 -06:00
|
|
|
LClosure *luaF_newLclosure (lua_State *L, int n) {
|
|
|
|
GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n));
|
|
|
|
LClosure *c = gco2lcl(o);
|
|
|
|
c->p = NULL;
|
|
|
|
c->nupvalues = cast_byte(n);
|
|
|
|
while (n--) c->upvals[n] = NULL;
|
2011-08-08 15:19:44 -06:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2016-07-26 21:23:26 -06:00
|
|
|
/*
|
|
|
|
** fill a closure with new closed upvalues
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
2011-08-08 15:19:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UpVal *luaF_findupval (lua_State *L, StkId level) {
|
2016-07-26 21:23:26 -06:00
|
|
|
UpVal **pp = &L->openupval;
|
2011-08-08 15:19:44 -06:00
|
|
|
UpVal *p;
|
|
|
|
UpVal *uv;
|
2016-07-26 21:23:26 -06:00
|
|
|
lua_assert(isintwups(L) || L->openupval == NULL);
|
|
|
|
while (*pp != NULL && (p = *pp)->v >= level) {
|
|
|
|
lua_assert(upisopen(p));
|
|
|
|
if (p->v == level) /* found a corresponding upvalue? */
|
|
|
|
return p; /* return it */
|
|
|
|
pp = &p->u.open.next;
|
2011-08-08 15:19:44 -06:00
|
|
|
}
|
2016-07-26 21:23:26 -06:00
|
|
|
/* not found: create a new upvalue */
|
|
|
|
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;
|
|
|
|
*pp = uv;
|
2011-08-08 15:19:44 -06:00
|
|
|
uv->v = level; /* current value lives in the stack */
|
2016-07-26 21:23:26 -06:00
|
|
|
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;
|
|
|
|
}
|
2011-08-08 15:19:44 -06:00
|
|
|
return uv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaF_close (lua_State *L, StkId level) {
|
|
|
|
UpVal *uv;
|
2016-07-26 21:23:26 -06:00
|
|
|
while (L->openupval != NULL && (uv = L->openupval)->v >= level) {
|
|
|
|
lua_assert(upisopen(uv));
|
|
|
|
L->openupval = uv->u.open.next; /* remove from 'open' list */
|
|
|
|
if (uv->refcount == 0) /* no references? */
|
|
|
|
luaM_free(L, uv); /* free upvalue */
|
2011-08-08 15:19:44 -06:00
|
|
|
else {
|
2012-03-31 02:11:43 -06:00
|
|
|
setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */
|
2011-08-08 15:19:44 -06:00
|
|
|
uv->v = &uv->u.value; /* now current value lives here */
|
2016-07-26 21:23:26 -06:00
|
|
|
luaC_upvalbarrier(L, uv);
|
2011-08-08 15:19:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Proto *luaF_newproto (lua_State *L) {
|
2016-07-26 21:23:26 -06:00
|
|
|
GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto));
|
|
|
|
Proto *f = gco2p(o);
|
2011-08-08 15:19:44 -06:00
|
|
|
f->k = NULL;
|
|
|
|
f->sizek = 0;
|
|
|
|
f->p = NULL;
|
|
|
|
f->sizep = 0;
|
|
|
|
f->code = NULL;
|
2012-03-31 02:11:43 -06:00
|
|
|
f->cache = NULL;
|
2011-08-08 15:19:44 -06:00
|
|
|
f->sizecode = 0;
|
2012-03-31 02:11:43 -06:00
|
|
|
f->lineinfo = NULL;
|
2011-08-08 15:19:44 -06:00
|
|
|
f->sizelineinfo = 0;
|
|
|
|
f->upvalues = NULL;
|
2012-03-31 02:11:43 -06:00
|
|
|
f->sizeupvalues = 0;
|
2011-08-08 15:19:44 -06:00
|
|
|
f->numparams = 0;
|
|
|
|
f->is_vararg = 0;
|
|
|
|
f->maxstacksize = 0;
|
|
|
|
f->locvars = NULL;
|
2012-03-31 02:11:43 -06:00
|
|
|
f->sizelocvars = 0;
|
2011-08-08 15:19:44 -06:00
|
|
|
f->linedefined = 0;
|
|
|
|
f->lastlinedefined = 0;
|
|
|
|
f->source = NULL;
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaF_freeproto (lua_State *L, Proto *f) {
|
2012-03-31 02:11:43 -06:00
|
|
|
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);
|
2011-08-08 15:19:44 -06:00
|
|
|
luaM_free(L, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2016-07-26 21:23:26 -06:00
|
|
|
** Look for n-th local variable at line 'line' in function 'func'.
|
2011-08-08 15:19:44 -06: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL; /* not found */
|
|
|
|
}
|
|
|
|
|