- Main Page
- Related Pages
- Modules
- Data Structures
- Files

# lua/lundump.c

Go to the documentation of this file.

    00001 /*
    00002 ** $Id: lundump.c,v 1.60 2006/02/16 15:53:49 lhf Exp $
    00003 ** load precompiled Lua chunks
    00004 ** See Copyright Notice in lua.h
    00005 */
    00006
    00007 #include <string.h>
    00008
    00009 #define lundump_c
    00010 #define LUA_CORE
    00011
    00012 #include "lua.h"
    00013
    00014 #include "ldebug.h"
    00015 #include "ldo.h"
    00016 #include "lfunc.h"
    00017 #include "lmem.h"
    00018 #include "lobject.h"
    00019 #include "lstring.h"
    00020 #include "lundump.h"
    00021 #include "lzio.h"
    00022
    00023 typedef struct {
    00024  lua_State* L;
    00025  ZIO* Z;
    00026  Mbuffer* b;
    00027  const char* name;
    00028 } LoadState;
    00029
    00030 #ifdef LUAC_TRUST_BINARIES
    00031 #define IF(c,s)
    00032 #else
    00033 #define IF(c,s)         if (c) error(S,s)
    00034
    00035 static void error(LoadState* S, const char* why)
    00036 {
    00037  luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
    00038  luaD_throw(S->L,LUA_ERRSYNTAX);
    00039 }
    00040 #endif
    00041
    00042 #define LoadMem(S,b,n,size)     LoadBlock(S,b,(n)*(size))
    00043 #define LoadByte(S)             (lu_byte)LoadChar(S)
    00044 #define LoadVar(S,x)            LoadMem(S,&x,1,sizeof(x))
    00045 #define LoadVector(S,b,n,size)  LoadMem(S,b,n,size)
    00046
    00047 static void LoadBlock(LoadState* S, void* b, size_t size)
    00048 {
    00049  size_t r=luaZ_read(S->Z,b,size);
    00050  IF (r!=0, "unexpected end");
    00051 }
    00052
    00053 static int LoadChar(LoadState* S)
    00054 {
    00055  char x;
    00056  LoadVar(S,x);
    00057  return x;
    00058 }
    00059
    00060 static int LoadInt(LoadState* S)
    00061 {
    00062  int x;
    00063  LoadVar(S,x);
    00064  IF (x<0, "bad integer");
    00065  return x;
    00066 }
    00067
    00068 static lua_Number LoadNumber(LoadState* S)
    00069 {
    00070  lua_Number x;
    00071  LoadVar(S,x);
    00072  return x;
    00073 }
    00074
    00075 static TString* LoadString(LoadState* S)
    00076 {
    00077  size_t size;
    00078  LoadVar(S,size);
    00079  if (size==0)
    00080   return NULL;
    00081  else
    00082  {
    00083   char* s=luaZ_openspace(S->L,S->b,size);
    00084   LoadBlock(S,s,size);
    00085   return luaS_newlstr(S->L,s,size-1);           /* remove trailing '\0' */
    00086  }
    00087 }
    00088
    00089 static void LoadCode(LoadState* S, Proto* f)
    00090 {
    00091  int n=LoadInt(S);
    00092  f->code=luaM_newvector(S->L,n,Instruction);
    00093  f->sizecode=n;
    00094  LoadVector(S,f->code,n,sizeof(Instruction));
    00095 }
    00096
    00097 static Proto* LoadFunction(LoadState* S, TString* p);
    00098
    00099 static void LoadConstants(LoadState* S, Proto* f)
    00100 {
    00101  int i,n;
    00102  n=LoadInt(S);
    00103  f->k=luaM_newvector(S->L,n,TValue);
    00104  f->sizek=n;
    00105  for (i=0; i<n; i++) setnilvalue(&f->k[i]);
    00106  for (i=0; i<n; i++)
    00107  {
    00108   TValue* o=&f->k[i];
    00109   int t=LoadChar(S);
    00110   switch (t)
    00111   {
    00112    case LUA_TNIL:
    00113         setnilvalue(o);
    00114         break;
    00115    case LUA_TBOOLEAN:
    00116         setbvalue(o,LoadChar(S));
    00117         break;
    00118    case LUA_TNUMBER:
    00119         setnvalue(o,LoadNumber(S));
    00120         break;
    00121    case LUA_TSTRING:
    00122         setsvalue2n(S->L,o,LoadString(S));
    00123         break;
    00124    default:
    00125         IF (1, "bad constant");
    00126         break;
    00127   }
    00128  }
    00129  n=LoadInt(S);
    00130  f->p=luaM_newvector(S->L,n,Proto*);
    00131  f->sizep=n;
    00132  for (i=0; i<n; i++) f->p[i]=NULL;
    00133  for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
    00134 }
    00135
    00136 static void LoadDebug(LoadState* S, Proto* f)
    00137 {
    00138  int i,n;
    00139  n=LoadInt(S);
    00140  f->lineinfo=luaM_newvector(S->L,n,int);
    00141  f->sizelineinfo=n;
    00142  LoadVector(S,f->lineinfo,n,sizeof(int));
    00143  n=LoadInt(S);
    00144  f->locvars=luaM_newvector(S->L,n,LocVar);
    00145  f->sizelocvars=n;
    00146  for (i=0; i<n; i++) f->locvars[i].varname=NULL;
    00147  for (i=0; i<n; i++)
    00148  {
    00149   f->locvars[i].varname=LoadString(S);
    00150   f->locvars[i].startpc=LoadInt(S);
    00151   f->locvars[i].endpc=LoadInt(S);
    00152  }
    00153  n=LoadInt(S);
    00154  f->upvalues=luaM_newvector(S->L,n,TString*);
    00155  f->sizeupvalues=n;
    00156  for (i=0; i<n; i++) f->upvalues[i]=NULL;
    00157  for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
    00158 }
    00159
    00160 static Proto* LoadFunction(LoadState* S, TString* p)
    00161 {
    00162  Proto* f=luaF_newproto(S->L);
    00163  setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
    00164  f->source=LoadString(S); if (f->source==NULL) f->source=p;
    00165  f->linedefined=LoadInt(S);
    00166  f->lastlinedefined=LoadInt(S);
    00167  f->nups=LoadByte(S);
    00168  f->numparams=LoadByte(S);
    00169  f->is_vararg=LoadByte(S);
    00170  f->maxstacksize=LoadByte(S);
    00171  LoadCode(S,f);
    00172  LoadConstants(S,f);
    00173  LoadDebug(S,f);
    00174  IF (!luaG_checkcode(f), "bad code");
    00175  S->L->top--;
    00176  return f;
    00177 }
    00178
    00179 static void LoadHeader(LoadState* S)
    00180 {
    00181  char h[LUAC_HEADERSIZE];
    00182  char s[LUAC_HEADERSIZE];
    00183  luaU_header(h);
    00184  LoadBlock(S,s,LUAC_HEADERSIZE);
    00185  IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
    00186 }
    00187
    00188 /*
    00189 ** load precompiled chunk
    00190 */
    00191 Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
    00192 {
    00193  LoadState S;
    00194  if (*name=='@' || *name=='=')
    00195   S.name=name+1;
    00196  else if (*name==LUA_SIGNATURE[0])
    00197   S.name="binary string";
    00198  else
    00199   S.name=name;
    00200  S.L=L;
    00201  S.Z=Z;
    00202  S.b=buff;
    00203  LoadHeader(&S);
    00204  return LoadFunction(&S,luaS_newliteral(L,"=?"));
    00205 }
    00206
    00207 /*
    00208 * make header
    00209 */
    00210 void luaU_header (char* h)
    00211 {
    00212  int x=1;
    00213  memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
    00214  h+=sizeof(LUA_SIGNATURE)-1;
    00215  *h++=(char)LUAC_VERSION;
    00216  *h++=(char)LUAC_FORMAT;
    00217  *h++=(char)*(char*)&x;                         /* endianness */
    00218  *h++=(char)sizeof(int);
    00219  *h++=(char)sizeof(size_t);
    00220  *h++=(char)sizeof(Instruction);
    00221  *h++=(char)sizeof(lua_Number);
    00222  *h++=(char)(((lua_Number)0.5)==0);             /* is lua_Number integral? */
    00223 }