different variables for number of upvalues and size of upvalue array

(makes code clearer)
This commit is contained in:
Roberto Ierusalimschy 2003-02-11 08:46:24 -02:00
parent 3cdeacbbfb
commit 7a40cdbda0
10 changed files with 43 additions and 55 deletions

8
lapi.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 1.226 2002/12/04 17:38:31 roberto Exp roberto $
** $Id: lapi.c,v 1.227 2002/12/19 11:11:55 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@ -872,14 +872,16 @@ static const char *aux_upvalue (lua_State *L, int funcindex, int n,
StkId fi = luaA_index(L, funcindex);
if (!ttisfunction(fi)) return NULL;
f = clvalue(fi);
if (n > f->l.nupvalues) return NULL;
if (f->c.isC) {
if (n > f->c.nupvalues) return NULL;
*val = &f->c.upvalue[n-1];
return "";
}
else {
Proto *p = f->l.p;
if (n > p->sizeupvalues) return NULL;
*val = f->l.upvals[n-1]->v;
return getstr(f->l.p->upvalues[n-1]);
return getstr(p->upvalues[n-1]);
}
}

View File

@ -1,5 +1,5 @@
/*
** $Id: ldebug.c,v 1.142 2002/12/06 17:15:35 roberto Exp roberto $
** $Id: ldebug.c,v 1.143 2002/12/11 12:34:22 roberto Exp roberto $
** Debug Interface
** See Copyright Notice in lua.h
*/
@ -345,7 +345,7 @@ static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) {
}
case OP_GETUPVAL:
case OP_SETUPVAL: {
check(b < pt->nupvalues);
check(b < pt->nups);
break;
}
case OP_GETGLOBAL:
@ -404,7 +404,7 @@ static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) {
case OP_CLOSURE: {
int nup;
check(b < pt->sizep);
nup = pt->p[b]->nupvalues;
nup = pt->p[b]->nups;
check(pc + nup < pt->sizecode);
for (; nup>0; nup--) {
OpCode op1 = GET_OPCODE(pt->code[pc+nup]);

11
ldump.c
View File

@ -1,5 +1,5 @@
/*
** $Id: ldump.c,v 1.3 2003/01/10 11:08:45 lhf Exp $
** $Id: ldump.c,v 1.3 2003/01/27 15:52:57 roberto Exp roberto $
** save bytecodes
** See Copyright Notice in lua.h
*/
@ -90,14 +90,9 @@ static void DumpLines(const Proto* f, DumpState* D)
static void DumpUpvalues(const Proto* f, DumpState* D)
{
if (f->upvalues==NULL)
DumpInt(0,D);
else
{
int i,n=f->nupvalues;
int i,n=f->sizeupvalues;
DumpInt(n,D);
for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
}
}
static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
@ -133,7 +128,7 @@ static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
{
DumpString((f->source==p) ? NULL : f->source,D);
DumpInt(f->lineDefined,D);
DumpInt(f->nupvalues,D);
DumpByte(f->nups,D);
DumpByte(f->numparams,D);
DumpByte(f->is_vararg,D);
DumpByte(f->maxstacksize,D);

View File

@ -1,5 +1,5 @@
/*
** $Id: lfunc.c,v 1.64 2002/12/04 17:38:31 roberto Exp roberto $
** $Id: lfunc.c,v 1.65 2002/12/19 11:11:55 roberto Exp roberto $
** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h
*/
@ -84,7 +84,8 @@ Proto *luaF_newproto (lua_State *L) {
f->code = NULL;
f->sizecode = 0;
f->sizelineinfo = 0;
f->nupvalues = 0;
f->sizeupvalues = 0;
f->nups = 0;
f->upvalues = NULL;
f->numparams = 0;
f->is_vararg = 0;
@ -104,7 +105,7 @@ void luaF_freeproto (lua_State *L, Proto *f) {
luaM_freearray(L, f->k, f->sizek, TObject);
luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
luaM_freearray(L, f->upvalues, f->nupvalues, TString *);
luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *);
luaM_freelem(L, f);
}

6
lgc.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lgc.c,v 1.167 2002/12/19 11:11:55 roberto Exp roberto $
** $Id: lgc.c,v 1.168 2003/02/10 17:32:50 roberto Exp roberto $
** Garbage Collector
** See Copyright Notice in lua.h
*/
@ -190,7 +190,7 @@ static void traverseproto (GCState *st, Proto *f) {
if (ttisstring(f->k+i))
stringmark(tsvalue(f->k+i));
}
for (i=0; i<f->nupvalues; i++) /* mark upvalue names */
for (i=0; i<f->sizeupvalues; i++) /* mark upvalue names */
stringmark(f->upvalues[i]);
for (i=0; i<f->sizep; i++) /* mark nested protos */
markvalue(st, f->p[i]);
@ -209,7 +209,7 @@ static void traverseclosure (GCState *st, Closure *cl) {
}
else {
int i;
lua_assert(cl->l.nupvalues == cl->l.p->nupvalues);
lua_assert(cl->l.nupvalues == cl->l.p->nups);
markvalue(st, hvalue(&cl->l.g));
markvalue(st, cl->l.p);
for (i=0; i<cl->l.nupvalues; i++) { /* mark its upvalues */

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.h,v 1.155 2002/11/14 16:16:21 roberto Exp roberto $
** $Id: lobject.h,v 1.156 2002/12/19 11:11:55 roberto Exp roberto $
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/
@ -216,7 +216,7 @@ typedef struct Proto {
struct LocVar *locvars; /* information about local variables */
TString **upvalues; /* upvalue names */
TString *source;
int nupvalues; /* (also size of `upvals') */
int sizeupvalues;
int sizek; /* size of `k' */
int sizecode;
int sizelineinfo;
@ -224,6 +224,7 @@ typedef struct Proto {
int sizelocvars;
int lineDefined;
GCObject *gclist;
lu_byte nups; /* number of upvalues */
lu_byte numparams;
lu_byte is_vararg;
lu_byte maxstacksize;

View File

@ -1,5 +1,5 @@
/*
** $Id: lparser.c,v 1.202 2002/12/11 12:34:22 roberto Exp roberto $
** $Id: lparser.c,v 1.203 2002/12/19 11:11:55 roberto Exp roberto $
** Lua Parser
** See Copyright Notice in lua.h
*/
@ -181,19 +181,20 @@ static void create_local (LexState *ls, const char *name) {
static int indexupvalue (FuncState *fs, TString *name, expdesc *v) {
int i;
for (i=0; i<fs->nu; i++) {
Proto *f = fs->f;
for (i=0; i<f->nups; i++) {
if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->info) {
lua_assert(fs->f->upvalues[i] == name);
return i;
}
}
/* new one */
luaX_checklimit(fs->ls, fs->nu + 1, MAXUPVALUES, "upvalues");
luaM_growvector(fs->L, fs->f->upvalues, fs->nu, fs->f->nupvalues,
luaX_checklimit(fs->ls, f->nups + 1, MAXUPVALUES, "upvalues");
luaM_growvector(fs->L, fs->f->upvalues, f->nups, fs->f->sizeupvalues,
TString *, MAX_INT, "");
fs->f->upvalues[fs->nu] = name;
fs->upvalues[fs->nu] = *v;
return fs->nu++;
fs->f->upvalues[f->nups] = name;
fs->upvalues[f->nups] = *v;
return f->nups++;
}
@ -307,7 +308,7 @@ static void pushclosure (LexState *ls, FuncState *func, expdesc *v) {
MAXARG_Bx, "constant table overflow");
f->p[fs->np++] = func->f;
init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
for (i=0; i<func->nu; i++) {
for (i=0; i<func->f->nups; i++) {
OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0);
}
@ -326,17 +327,13 @@ static void open_func (LexState *ls, FuncState *fs) {
fs->jpc = NO_JUMP;
fs->freereg = 0;
fs->nk = 0;
fs->nu = 0;
fs->h = luaH_new(ls->L, 0, 0);
fs->np = 0;
fs->nlocvars = 0;
fs->nactvar = 0;
fs->bl = NULL;
f->code = NULL;
f->source = ls->source;
f->maxstacksize = 2; /* registers 0/1 are always valid */
f->numparams = 0; /* default for main chunk */
f->is_vararg = 0; /* default for main chunk */
}
@ -356,8 +353,8 @@ static void close_func (LexState *ls) {
f->sizep = fs->np;
luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
f->sizelocvars = fs->nlocvars;
luaM_reallocvector(L, f->upvalues, f->nupvalues, fs->nu, TString *);
f->nupvalues = fs->nu;
luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *);
f->sizeupvalues = f->nups;
lua_assert(luaG_checkcode(f));
lua_assert(fs->bl == NULL);
ls->fs = fs->prev;
@ -376,7 +373,7 @@ Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff) {
check_condition(&lexstate, (lexstate.t.token == TK_EOS), "<eof> expected");
close_func(&lexstate);
lua_assert(funcstate.prev == NULL);
lua_assert(funcstate.nu == 0);
lua_assert(funcstate.f->nups == 0);
lua_assert(lexstate.nestlevel == 0);
return funcstate.f;
}

View File

@ -1,5 +1,5 @@
/*
** $Id: lparser.h,v 1.45 2002/10/08 18:46:08 roberto Exp roberto $
** $Id: lparser.h,v 1.46 2002/12/19 11:11:55 roberto Exp roberto $
** Lua Parser
** See Copyright Notice in lua.h
*/
@ -58,7 +58,6 @@ typedef struct FuncState {
int freereg; /* first free register */
int nk; /* number of elements in `k' */
int np; /* number of elements in `p' */
int nu; /* number of elements in `upvalues' */
int nlocvars; /* number of elements in `locvars' */
int nactvar; /* number of active local variables */
expdesc upvalues[MAXUPVALUES]; /* upvalues */

View File

@ -1,5 +1,5 @@
/*
** $Id: lundump.c,v 1.47 2003/01/10 11:08:45 lhf Exp $
** $Id: lundump.c,v 1.59 2003/01/27 15:52:57 roberto Exp roberto $
** load pre-compiled Lua chunks
** See Copyright Notice in lua.h
*/
@ -140,21 +140,14 @@ static void LoadLines (LoadState* S, Proto* f)
static void LoadUpvalues (LoadState* S, Proto* f)
{
int i,n,noname;
int i,n;
n=LoadInt(S);
noname=(n==0);
if (!noname && n!=f->nupvalues)
if (n!=0 && n!=f->nups)
luaG_runerror(S->L,"bad nupvalues in %s: read %d; expected %d",
S->name,n,f->nupvalues);
n=f->nupvalues;
S->name,n,f->nups);
f->upvalues=luaM_newvector(S->L,n,TString*);
if (noname)
{
TString* name=luaS_newliteral(S->L,"(no name)");
for (i=0; i<n; i++) f->upvalues[i]=name;
}
else
for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
f->sizeupvalues = n;
for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
}
static Proto* LoadFunction (LoadState* S, TString* p);
@ -196,7 +189,7 @@ static Proto* LoadFunction (LoadState* S, TString* p)
Proto* f=luaF_newproto(S->L);
f->source=LoadString(S); if (f->source==NULL) f->source=p;
f->lineDefined=LoadInt(S);
f->nupvalues=LoadInt(S);
f->nups=LoadByte(S);
f->numparams=LoadByte(S);
f->is_vararg=LoadByte(S);
f->maxstacksize=LoadByte(S);

4
lvm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 1.273 2002/12/11 12:34:22 roberto Exp roberto $
** $Id: lvm.c,v 1.274 2003/01/27 15:12:52 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -754,7 +754,7 @@ StkId luaV_execute (lua_State *L) {
Closure *ncl;
int nup, j;
p = cl->p->p[GETARG_Bx(i)];
nup = p->nupvalues;
nup = p->nups;
ncl = luaF_newLclosure(L, nup, &cl->g);
ncl->l.p = p;
for (j=0; j<nup; j++, pc++) {