From 0175f8d5d18d18e2d8a94db9c5be5c40598aebda Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 27 Jan 2003 13:52:57 -0200 Subject: [PATCH] dump/undump of upvalue names --- ldump.c | 32 +++++++++++++++++++++++--------- lundump.c | 26 +++++++++++++++++++++++--- lundump.h | 8 ++++---- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/ldump.c b/ldump.c index 0a3b9453..f62eb2c1 100644 --- a/ldump.c +++ b/ldump.c @@ -1,5 +1,5 @@ /* -** $Id: ldump.c,v 1.1 2002/10/25 21:31:28 roberto Exp roberto $ +** $Id: ldump.c,v 1.3 2003/01/10 11:08:45 lhf Exp $ ** save bytecodes ** See Copyright Notice in lua.h */ @@ -19,13 +19,12 @@ #define DumpLiteral(s,D) DumpBlock("" s,(sizeof(s))-1,D) typedef struct { - lua_State *L; + lua_State* L; lua_Chunkwriter write; - void *data; + void* data; } DumpState; - -static void DumpBlock(const void *b, size_t size, DumpState* D) +static void DumpBlock(const void* b, size_t size, DumpState* D) { lua_unlock(D->L); (*D->write)(D->L,b,size,D->data); @@ -89,6 +88,18 @@ static void DumpLines(const Proto* f, DumpState* D) DumpVector(f->lineinfo,f->sizelineinfo,sizeof(*f->lineinfo),D); } +static void DumpUpvalues(const Proto* f, DumpState* D) +{ + if (f->upvalues==NULL) + DumpInt(0,D); + else + { + int i,n=f->nupvalues; + DumpInt(n,D); + for (i=0; iupvalues[i],D); + } +} + static void DumpFunction(const Proto* f, const TString* p, DumpState* D); static void DumpConstants(const Proto* f, DumpState* D) @@ -122,12 +133,13 @@ static void DumpFunction(const Proto* f, const TString* p, DumpState* D) { DumpString((f->source==p) ? NULL : f->source,D); DumpInt(f->lineDefined,D); - DumpByte(f->nupvalues,D); + DumpInt(f->nupvalues,D); DumpByte(f->numparams,D); DumpByte(f->is_vararg,D); DumpByte(f->maxstacksize,D); - DumpLocals(f,D); DumpLines(f,D); + DumpLocals(f,D); + DumpUpvalues(f,D); DumpConstants(f,D); DumpCode(f,D); } @@ -148,7 +160,10 @@ static void DumpHeader(DumpState* D) DumpNumber(TEST_NUMBER,D); } -void luaU_dump(lua_State *L, const Proto* Main, lua_Chunkwriter w, void* data) +/* +** dump function as precompiled chunk +*/ +void luaU_dump (lua_State* L, const Proto* Main, lua_Chunkwriter w, void* data) { DumpState D; D.L=L; @@ -157,4 +172,3 @@ void luaU_dump(lua_State *L, const Proto* Main, lua_Chunkwriter w, void* data) DumpHeader(&D); DumpFunction(Main,NULL,&D); } - diff --git a/lundump.c b/lundump.c index 48195faa..27d1c049 100644 --- a/lundump.c +++ b/lundump.c @@ -1,5 +1,5 @@ /* -** $Id: lundump.c,v 1.57 2002/11/14 16:15:53 roberto Exp roberto $ +** $Id: lundump.c,v 1.47 2003/01/10 11:08:45 lhf Exp $ ** load pre-compiled Lua chunks ** See Copyright Notice in lua.h */ @@ -138,6 +138,25 @@ static void LoadLines (LoadState* S, Proto* f) LoadVector(S,f->lineinfo,size,sizeof(*f->lineinfo)); } +static void LoadUpvalues (LoadState* S, Proto* f) +{ + int i,n,noname; + n=LoadInt(S); + noname=(n==0); + if (!noname && n!=f->nupvalues) + luaG_runerror(S->L,"bad nupvalues in %s: read %d; expected %d", + S->name,n,f->nupvalues); + n=f->nupvalues; + 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); +} + static Proto* LoadFunction (LoadState* S, TString* p); static void LoadConstants (LoadState* S, Proto* f) @@ -177,12 +196,13 @@ 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=LoadByte(S); + f->nupvalues=LoadInt(S); f->numparams=LoadByte(S); f->is_vararg=LoadByte(S); f->maxstacksize=LoadByte(S); - LoadLocals(S,f); LoadLines(S,f); + LoadLocals(S,f); + LoadUpvalues(S,f); LoadConstants(S,f); LoadCode(S,f); #ifndef TRUST_BINARIES diff --git a/lundump.h b/lundump.h index 44eb202b..474075a0 100644 --- a/lundump.h +++ b/lundump.h @@ -1,5 +1,5 @@ /* -** $Id: lundump.h,v 1.28 2002/10/09 13:42:01 roberto Exp roberto $ +** $Id: lundump.h,v 1.28 2002/12/13 11:12:35 lhf Exp $ ** load pre-compiled Lua chunks ** See Copyright Notice in lua.h */ @@ -11,13 +11,13 @@ #include "lzio.h" /* load one chunk; from lundump.c */ -Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer *buff); +Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff); /* find byte order; from lundump.c */ int luaU_endianness (void); -/* dump one chunk; from dump.c */ -void luaU_dump (lua_State *L, const Proto* Main, lua_Chunkwriter w, void* data); +/* dump one chunk; from ldump.c */ +void luaU_dump (lua_State* L, const Proto* Main, lua_Chunkwriter w, void* data); /* print one chunk; from print.c */ void luaU_print (const Proto* Main);