diff options
author | pixel <pixel> | 2007-07-27 10:05:52 +0000 |
---|---|---|
committer | pixel <pixel> | 2007-07-27 10:05:52 +0000 |
commit | 6c1ab7da376ae7f78a075dda5c5be0dabce20931 (patch) | |
tree | bc44e447c450c96140cf6b63c39a37afa8ba61fb /lib/lua/src/ldump.c | |
parent | a68385b1043a2041390f65ce5cf9ed434af5e95d (diff) |
Upgrading to Lua-5.1.2
Diffstat (limited to 'lib/lua/src/ldump.c')
-rw-r--r-- | lib/lua/src/ldump.c | 164 |
1 files changed, 79 insertions, 85 deletions
diff --git a/lib/lua/src/ldump.c b/lib/lua/src/ldump.c index 2bcfd5d..f7dd971 100644 --- a/lib/lua/src/ldump.c +++ b/lib/lua/src/ldump.c @@ -1,170 +1,164 @@ /* -** $Id: ldump.c,v 1.4 2004-11-27 21:46:07 pixel Exp $ -** save bytecodes +** $Id: ldump.c,v 1.5 2007-07-27 10:05:53 pixel Exp $ +** save precompiled Lua chunks ** See Copyright Notice in lua.h */ #include <stddef.h> #define ldump_c +#define LUA_CORE #include "lua.h" #include "lobject.h" -#include "lopcodes.h" #include "lstate.h" #include "lundump.h" -#define DumpVector(b,n,size,D) DumpBlock(b,(n)*(size),D) -#define DumpLiteral(s,D) DumpBlock("" s,(sizeof(s))-1,D) - typedef struct { lua_State* L; - lua_Chunkwriter write; + lua_Writer writer; void* data; + int strip; + int status; } DumpState; +#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) +#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) + static void DumpBlock(const void* b, size_t size, DumpState* D) { - lua_unlock(D->L); - (*D->write)(D->L,b,size,D->data); - lua_lock(D->L); + if (D->status==0) + { + lua_unlock(D->L); + D->status=(*D->writer)(D->L,b,size,D->data); + lua_lock(D->L); + } } -static void DumpByte(int y, DumpState* D) +static void DumpChar(int y, DumpState* D) { char x=(char)y; - DumpBlock(&x,sizeof(x),D); + DumpVar(x,D); } static void DumpInt(int x, DumpState* D) { - DumpBlock(&x,sizeof(x),D); + DumpVar(x,D); } -static void DumpSize(size_t x, DumpState* D) +static void DumpNumber(lua_Number x, DumpState* D) { - DumpBlock(&x,sizeof(x),D); + DumpVar(x,D); } -static void DumpNumber(lua_Number x, DumpState* D) +static void DumpVector(const void* b, int n, size_t size, DumpState* D) { - DumpBlock(&x,sizeof(x),D); + DumpInt(n,D); + DumpMem(b,n,size,D); } -static void DumpString(TString* s, DumpState* D) +static void DumpString(const TString* s, DumpState* D) { if (s==NULL || getstr(s)==NULL) - DumpSize(0,D); + { + size_t size=0; + DumpVar(size,D); + } else { size_t size=s->tsv.len+1; /* include trailing '\0' */ - DumpSize(size,D); + DumpVar(size,D); DumpBlock(getstr(s),size,D); } } -static void DumpCode(const Proto* f, DumpState* D) -{ - DumpInt(f->sizecode,D); - DumpVector(f->code,f->sizecode,sizeof(*f->code),D); -} - -static void DumpLocals(const Proto* f, DumpState* D) -{ - int i,n=f->sizelocvars; - DumpInt(n,D); - for (i=0; i<n; i++) - { - DumpString(f->locvars[i].varname,D); - DumpInt(f->locvars[i].startpc,D); - DumpInt(f->locvars[i].endpc,D); - } -} - -static void DumpLines(const Proto* f, DumpState* D) -{ - DumpInt(f->sizelineinfo,D); - DumpVector(f->lineinfo,f->sizelineinfo,sizeof(*f->lineinfo),D); -} - -static void DumpUpvalues(const Proto* f, DumpState* D) -{ - int i,n=f->sizeupvalues; - DumpInt(n,D); - for (i=0; i<n; i++) DumpString(f->upvalues[i],D); -} +#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) static void DumpFunction(const Proto* f, const TString* p, DumpState* D); static void DumpConstants(const Proto* f, DumpState* D) { - int i,n; - DumpInt(n=f->sizek,D); + int i,n=f->sizek; + DumpInt(n,D); for (i=0; i<n; i++) { - const TObject* o=&f->k[i]; - DumpByte(ttype(o),D); + const TValue* o=&f->k[i]; + DumpChar(ttype(o),D); switch (ttype(o)) { + case LUA_TNIL: + break; + case LUA_TBOOLEAN: + DumpChar(bvalue(o),D); + break; case LUA_TNUMBER: DumpNumber(nvalue(o),D); break; case LUA_TSTRING: - DumpString(tsvalue(o),D); - break; - case LUA_TNIL: + DumpString(rawtsvalue(o),D); break; default: lua_assert(0); /* cannot happen */ break; } } - DumpInt(n=f->sizep,D); + n=f->sizep; + DumpInt(n,D); for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D); } +static void DumpDebug(const Proto* f, DumpState* D) +{ + int i,n; + n= (D->strip) ? 0 : f->sizelineinfo; + DumpVector(f->lineinfo,n,sizeof(int),D); + n= (D->strip) ? 0 : f->sizelocvars; + DumpInt(n,D); + for (i=0; i<n; i++) + { + DumpString(f->locvars[i].varname,D); + DumpInt(f->locvars[i].startpc,D); + DumpInt(f->locvars[i].endpc,D); + } + n= (D->strip) ? 0 : 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) { - DumpString((f->source==p) ? NULL : f->source,D); - DumpInt(f->lineDefined,D); - DumpByte(f->nups,D); - DumpByte(f->numparams,D); - DumpByte(f->is_vararg,D); - DumpByte(f->maxstacksize,D); - DumpLines(f,D); - DumpLocals(f,D); - DumpUpvalues(f,D); - DumpConstants(f,D); + DumpString((f->source==p || D->strip) ? NULL : f->source,D); + DumpInt(f->linedefined,D); + DumpInt(f->lastlinedefined,D); + DumpChar(f->nups,D); + DumpChar(f->numparams,D); + DumpChar(f->is_vararg,D); + DumpChar(f->maxstacksize,D); DumpCode(f,D); + DumpConstants(f,D); + DumpDebug(f,D); } static void DumpHeader(DumpState* D) { - DumpLiteral(LUA_SIGNATURE,D); - DumpByte(VERSION,D); - DumpByte(luaU_endianness(),D); - DumpByte(sizeof(int),D); - DumpByte(sizeof(size_t),D); - DumpByte(sizeof(Instruction),D); - DumpByte(SIZE_OP,D); - DumpByte(SIZE_A,D); - DumpByte(SIZE_B,D); - DumpByte(SIZE_C,D); - DumpByte(sizeof(lua_Number),D); - DumpNumber(TEST_NUMBER,D); + char h[LUAC_HEADERSIZE]; + luaU_header(h); + DumpBlock(h,LUAC_HEADERSIZE,D); } /* -** dump function as precompiled chunk +** dump Lua function as precompiled chunk */ -void luaU_dump (lua_State* L, const Proto* Main, lua_Chunkwriter w, void* data) +int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) { DumpState D; D.L=L; - D.write=w; + D.writer=w; D.data=data; + D.strip=strip; + D.status=0; DumpHeader(&D); - DumpFunction(Main,NULL,&D); + DumpFunction(f,NULL,&D); + return D.status; } - |