Mercurial > vba-linux
diff src/lua/lundump.c @ 11:27763b933818
raise lua sources up one level
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 11:07:39 -0600 |
parents | src/lua/src/lundump.c@f9f4f1b99eed |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/lua/lundump.c Sat Mar 03 11:07:39 2012 -0600 1.3 @@ -0,0 +1,227 @@ 1.4 +/* 1.5 +** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $ 1.6 +** load precompiled Lua chunks 1.7 +** See Copyright Notice in lua.h 1.8 +*/ 1.9 + 1.10 +#include <string.h> 1.11 + 1.12 +#define lundump_c 1.13 +#define LUA_CORE 1.14 + 1.15 +#include "lua.h" 1.16 + 1.17 +#include "ldebug.h" 1.18 +#include "ldo.h" 1.19 +#include "lfunc.h" 1.20 +#include "lmem.h" 1.21 +#include "lobject.h" 1.22 +#include "lstring.h" 1.23 +#include "lundump.h" 1.24 +#include "lzio.h" 1.25 + 1.26 +typedef struct { 1.27 + lua_State* L; 1.28 + ZIO* Z; 1.29 + Mbuffer* b; 1.30 + const char* name; 1.31 +} LoadState; 1.32 + 1.33 +#ifdef LUAC_TRUST_BINARIES 1.34 +#define IF(c,s) 1.35 +#define error(S,s) 1.36 +#else 1.37 +#define IF(c,s) if (c) error(S,s) 1.38 + 1.39 +static void error(LoadState* S, const char* why) 1.40 +{ 1.41 + luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); 1.42 + luaD_throw(S->L,LUA_ERRSYNTAX); 1.43 +} 1.44 +#endif 1.45 + 1.46 +#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) 1.47 +#define LoadByte(S) (lu_byte)LoadChar(S) 1.48 +#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) 1.49 +#define LoadVector(S,b,n,size) LoadMem(S,b,n,size) 1.50 + 1.51 +static void LoadBlock(LoadState* S, void* b, size_t size) 1.52 +{ 1.53 + size_t r=luaZ_read(S->Z,b,size); 1.54 + IF (r!=0, "unexpected end"); 1.55 +} 1.56 + 1.57 +static int LoadChar(LoadState* S) 1.58 +{ 1.59 + char x; 1.60 + LoadVar(S,x); 1.61 + return x; 1.62 +} 1.63 + 1.64 +static int LoadInt(LoadState* S) 1.65 +{ 1.66 + int x; 1.67 + LoadVar(S,x); 1.68 + IF (x<0, "bad integer"); 1.69 + return x; 1.70 +} 1.71 + 1.72 +static lua_Number LoadNumber(LoadState* S) 1.73 +{ 1.74 + lua_Number x; 1.75 + LoadVar(S,x); 1.76 + return x; 1.77 +} 1.78 + 1.79 +static TString* LoadString(LoadState* S) 1.80 +{ 1.81 + size_t size; 1.82 + LoadVar(S,size); 1.83 + if (size==0) 1.84 + return NULL; 1.85 + else 1.86 + { 1.87 + char* s=luaZ_openspace(S->L,S->b,size); 1.88 + LoadBlock(S,s,size); 1.89 + return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ 1.90 + } 1.91 +} 1.92 + 1.93 +static void LoadCode(LoadState* S, Proto* f) 1.94 +{ 1.95 + int n=LoadInt(S); 1.96 + f->code=luaM_newvector(S->L,n,Instruction); 1.97 + f->sizecode=n; 1.98 + LoadVector(S,f->code,n,sizeof(Instruction)); 1.99 +} 1.100 + 1.101 +static Proto* LoadFunction(LoadState* S, TString* p); 1.102 + 1.103 +static void LoadConstants(LoadState* S, Proto* f) 1.104 +{ 1.105 + int i,n; 1.106 + n=LoadInt(S); 1.107 + f->k=luaM_newvector(S->L,n,TValue); 1.108 + f->sizek=n; 1.109 + for (i=0; i<n; i++) setnilvalue(&f->k[i]); 1.110 + for (i=0; i<n; i++) 1.111 + { 1.112 + TValue* o=&f->k[i]; 1.113 + int t=LoadChar(S); 1.114 + switch (t) 1.115 + { 1.116 + case LUA_TNIL: 1.117 + setnilvalue(o); 1.118 + break; 1.119 + case LUA_TBOOLEAN: 1.120 + setbvalue(o,LoadChar(S)!=0); 1.121 + break; 1.122 + case LUA_TNUMBER: 1.123 + setnvalue(o,LoadNumber(S)); 1.124 + break; 1.125 + case LUA_TSTRING: 1.126 + setsvalue2n(S->L,o,LoadString(S)); 1.127 + break; 1.128 + default: 1.129 + error(S,"bad constant"); 1.130 + break; 1.131 + } 1.132 + } 1.133 + n=LoadInt(S); 1.134 + f->p=luaM_newvector(S->L,n,Proto*); 1.135 + f->sizep=n; 1.136 + for (i=0; i<n; i++) f->p[i]=NULL; 1.137 + for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source); 1.138 +} 1.139 + 1.140 +static void LoadDebug(LoadState* S, Proto* f) 1.141 +{ 1.142 + int i,n; 1.143 + n=LoadInt(S); 1.144 + f->lineinfo=luaM_newvector(S->L,n,int); 1.145 + f->sizelineinfo=n; 1.146 + LoadVector(S,f->lineinfo,n,sizeof(int)); 1.147 + n=LoadInt(S); 1.148 + f->locvars=luaM_newvector(S->L,n,LocVar); 1.149 + f->sizelocvars=n; 1.150 + for (i=0; i<n; i++) f->locvars[i].varname=NULL; 1.151 + for (i=0; i<n; i++) 1.152 + { 1.153 + f->locvars[i].varname=LoadString(S); 1.154 + f->locvars[i].startpc=LoadInt(S); 1.155 + f->locvars[i].endpc=LoadInt(S); 1.156 + } 1.157 + n=LoadInt(S); 1.158 + f->upvalues=luaM_newvector(S->L,n,TString*); 1.159 + f->sizeupvalues=n; 1.160 + for (i=0; i<n; i++) f->upvalues[i]=NULL; 1.161 + for (i=0; i<n; i++) f->upvalues[i]=LoadString(S); 1.162 +} 1.163 + 1.164 +static Proto* LoadFunction(LoadState* S, TString* p) 1.165 +{ 1.166 + Proto* f; 1.167 + if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); 1.168 + f=luaF_newproto(S->L); 1.169 + setptvalue2s(S->L,S->L->top,f); incr_top(S->L); 1.170 + f->source=LoadString(S); if (f->source==NULL) f->source=p; 1.171 + f->linedefined=LoadInt(S); 1.172 + f->lastlinedefined=LoadInt(S); 1.173 + f->nups=LoadByte(S); 1.174 + f->numparams=LoadByte(S); 1.175 + f->is_vararg=LoadByte(S); 1.176 + f->maxstacksize=LoadByte(S); 1.177 + LoadCode(S,f); 1.178 + LoadConstants(S,f); 1.179 + LoadDebug(S,f); 1.180 + IF (!luaG_checkcode(f), "bad code"); 1.181 + S->L->top--; 1.182 + S->L->nCcalls--; 1.183 + return f; 1.184 +} 1.185 + 1.186 +static void LoadHeader(LoadState* S) 1.187 +{ 1.188 + char h[LUAC_HEADERSIZE]; 1.189 + char s[LUAC_HEADERSIZE]; 1.190 + luaU_header(h); 1.191 + LoadBlock(S,s,LUAC_HEADERSIZE); 1.192 + IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); 1.193 +} 1.194 + 1.195 +/* 1.196 +** load precompiled chunk 1.197 +*/ 1.198 +Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) 1.199 +{ 1.200 + LoadState S; 1.201 + if (*name=='@' || *name=='=') 1.202 + S.name=name+1; 1.203 + else if (*name==LUA_SIGNATURE[0]) 1.204 + S.name="binary string"; 1.205 + else 1.206 + S.name=name; 1.207 + S.L=L; 1.208 + S.Z=Z; 1.209 + S.b=buff; 1.210 + LoadHeader(&S); 1.211 + return LoadFunction(&S,luaS_newliteral(L,"=?")); 1.212 +} 1.213 + 1.214 +/* 1.215 +* make header 1.216 +*/ 1.217 +void luaU_header (char* h) 1.218 +{ 1.219 + int x=1; 1.220 + memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1); 1.221 + h+=sizeof(LUA_SIGNATURE)-1; 1.222 + *h++=(char)LUAC_VERSION; 1.223 + *h++=(char)LUAC_FORMAT; 1.224 + *h++=(char)*(char*)&x; /* endianness */ 1.225 + *h++=(char)sizeof(int); 1.226 + *h++=(char)sizeof(size_t); 1.227 + *h++=(char)sizeof(Instruction); 1.228 + *h++=(char)sizeof(lua_Number); 1.229 + *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */ 1.230 +}