rlm@1: /* rlm@1: ** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ rlm@1: ** save precompiled Lua chunks rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: #include rlm@1: rlm@1: #define ldump_c rlm@1: #define LUA_CORE rlm@1: rlm@1: #include "lua.h" rlm@1: rlm@1: #include "lobject.h" rlm@1: #include "lstate.h" rlm@1: #include "lundump.h" rlm@1: rlm@1: typedef struct { rlm@1: lua_State* L; rlm@1: lua_Writer writer; rlm@1: void* data; rlm@1: int strip; rlm@1: int status; rlm@1: } DumpState; rlm@1: rlm@1: #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) rlm@1: #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) rlm@1: rlm@1: static void DumpBlock(const void* b, size_t size, DumpState* D) rlm@1: { rlm@1: if (D->status==0) rlm@1: { rlm@1: lua_unlock(D->L); rlm@1: D->status=(*D->writer)(D->L,b,size,D->data); rlm@1: lua_lock(D->L); rlm@1: } rlm@1: } rlm@1: rlm@1: static void DumpChar(int y, DumpState* D) rlm@1: { rlm@1: char x=(char)y; rlm@1: DumpVar(x,D); rlm@1: } rlm@1: rlm@1: static void DumpInt(int x, DumpState* D) rlm@1: { rlm@1: DumpVar(x,D); rlm@1: } rlm@1: rlm@1: static void DumpNumber(lua_Number x, DumpState* D) rlm@1: { rlm@1: DumpVar(x,D); rlm@1: } rlm@1: rlm@1: static void DumpVector(const void* b, int n, size_t size, DumpState* D) rlm@1: { rlm@1: DumpInt(n,D); rlm@1: DumpMem(b,n,size,D); rlm@1: } rlm@1: rlm@1: static void DumpString(const TString* s, DumpState* D) rlm@1: { rlm@1: if (s==NULL || getstr(s)==NULL) rlm@1: { rlm@1: size_t size=0; rlm@1: DumpVar(size,D); rlm@1: } rlm@1: else rlm@1: { rlm@1: size_t size=s->tsv.len+1; /* include trailing '\0' */ rlm@1: DumpVar(size,D); rlm@1: DumpBlock(getstr(s),size,D); rlm@1: } rlm@1: } rlm@1: rlm@1: #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) rlm@1: rlm@1: static void DumpFunction(const Proto* f, const TString* p, DumpState* D); rlm@1: rlm@1: static void DumpConstants(const Proto* f, DumpState* D) rlm@1: { rlm@1: int i,n=f->sizek; rlm@1: DumpInt(n,D); rlm@1: for (i=0; ik[i]; rlm@1: DumpChar(ttype(o),D); rlm@1: switch (ttype(o)) rlm@1: { rlm@1: case LUA_TNIL: rlm@1: break; rlm@1: case LUA_TBOOLEAN: rlm@1: DumpChar(bvalue(o),D); rlm@1: break; rlm@1: case LUA_TNUMBER: rlm@1: DumpNumber(nvalue(o),D); rlm@1: break; rlm@1: case LUA_TSTRING: rlm@1: DumpString(rawtsvalue(o),D); rlm@1: break; rlm@1: default: rlm@1: lua_assert(0); /* cannot happen */ rlm@1: break; rlm@1: } rlm@1: } rlm@1: n=f->sizep; rlm@1: DumpInt(n,D); rlm@1: for (i=0; ip[i],f->source,D); rlm@1: } rlm@1: rlm@1: static void DumpDebug(const Proto* f, DumpState* D) rlm@1: { rlm@1: int i,n; rlm@1: n= (D->strip) ? 0 : f->sizelineinfo; rlm@1: DumpVector(f->lineinfo,n,sizeof(int),D); rlm@1: n= (D->strip) ? 0 : f->sizelocvars; rlm@1: DumpInt(n,D); rlm@1: for (i=0; ilocvars[i].varname,D); rlm@1: DumpInt(f->locvars[i].startpc,D); rlm@1: DumpInt(f->locvars[i].endpc,D); rlm@1: } rlm@1: n= (D->strip) ? 0 : f->sizeupvalues; rlm@1: DumpInt(n,D); rlm@1: for (i=0; iupvalues[i],D); rlm@1: } rlm@1: rlm@1: static void DumpFunction(const Proto* f, const TString* p, DumpState* D) rlm@1: { rlm@1: DumpString((f->source==p || D->strip) ? NULL : f->source,D); rlm@1: DumpInt(f->linedefined,D); rlm@1: DumpInt(f->lastlinedefined,D); rlm@1: DumpChar(f->nups,D); rlm@1: DumpChar(f->numparams,D); rlm@1: DumpChar(f->is_vararg,D); rlm@1: DumpChar(f->maxstacksize,D); rlm@1: DumpCode(f,D); rlm@1: DumpConstants(f,D); rlm@1: DumpDebug(f,D); rlm@1: } rlm@1: rlm@1: static void DumpHeader(DumpState* D) rlm@1: { rlm@1: char h[LUAC_HEADERSIZE]; rlm@1: luaU_header(h); rlm@1: DumpBlock(h,LUAC_HEADERSIZE,D); rlm@1: } rlm@1: rlm@1: /* rlm@1: ** dump Lua function as precompiled chunk rlm@1: */ rlm@1: int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) rlm@1: { rlm@1: DumpState D; rlm@1: D.L=L; rlm@1: D.writer=w; rlm@1: D.data=data; rlm@1: D.strip=strip; rlm@1: D.status=0; rlm@1: DumpHeader(&D); rlm@1: DumpFunction(f,NULL,&D); rlm@1: return D.status; rlm@1: }