From 7a40cdbda0a171da223094208dd3c6e3eca13922 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 11 Feb 2003 08:46:24 -0200 Subject: [PATCH] different variables for number of upvalues and size of upvalue array (makes code clearer) --- lapi.c | 8 +++++--- ldebug.c | 6 +++--- ldump.c | 11 +++-------- lfunc.c | 7 ++++--- lgc.c | 6 +++--- lobject.h | 5 +++-- lparser.c | 27 ++++++++++++--------------- lparser.h | 3 +-- lundump.c | 21 +++++++-------------- lvm.c | 4 ++-- 10 files changed, 43 insertions(+), 55 deletions(-) diff --git a/lapi.c b/lapi.c index 998bac0a..b413ada7 100644 --- a/lapi.c +++ b/lapi.c @@ -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]); } } diff --git a/ldebug.c b/ldebug.c index bf58d0a8..754efe1f 100644 --- a/ldebug.c +++ b/ldebug.c @@ -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]); diff --git a/ldump.c b/ldump.c index f62eb2c1..a34aeed3 100644 --- a/ldump.c +++ b/ldump.c @@ -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; iupvalues[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); diff --git a/lfunc.c b/lfunc.c index e55b59d9..2ce2f047 100644 --- a/lfunc.c +++ b/lfunc.c @@ -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); } diff --git a/lgc.c b/lgc.c index 626756a7..01e8032f 100644 --- a/lgc.c +++ b/lgc.c @@ -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; inupvalues; i++) /* mark upvalue names */ + for (i=0; isizeupvalues; i++) /* mark upvalue names */ stringmark(f->upvalues[i]); for (i=0; isizep; 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; il.nupvalues; i++) { /* mark its upvalues */ diff --git a/lobject.h b/lobject.h index 6b00b4b6..90533240 100644 --- a/lobject.h +++ b/lobject.h @@ -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; diff --git a/lparser.c b/lparser.c index d804e430..fb98ebc8 100644 --- a/lparser.c +++ b/lparser.c @@ -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; inu; i++) { + Proto *f = fs->f; + for (i=0; inups; 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; inu; i++) { + for (i=0; if->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), " 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; } diff --git a/lparser.h b/lparser.h index b426c800..b5aa740a 100644 --- a/lparser.h +++ b/lparser.h @@ -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 */ diff --git a/lundump.c b/lundump.c index 27d1c049..a35d2e9c 100644 --- a/lundump.c +++ b/lundump.c @@ -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; iupvalues[i]=name; - } - else - for (i=0; iupvalues[i]=LoadString(S); + f->sizeupvalues = n; + for (i=0; iupvalues[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); diff --git a/lvm.c b/lvm.c index 5d4b0445..31e7b083 100644 --- a/lvm.c +++ b/lvm.c @@ -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