Release 5.1.3
[lua.git] / src / lundump.c
blob731c064553e24818eadb60e0771de46be2f06aee
1 /*
2 ** $Id: lundump.c,v 2.7.1.2 2008/01/18 16:39:11 roberto Exp $
3 ** load precompiled Lua chunks
4 ** See Copyright Notice in lua.h
5 */
7 #include <string.h>
9 #define lundump_c
10 #define LUA_CORE
12 #include "lua.h"
14 #include "ldebug.h"
15 #include "ldo.h"
16 #include "lfunc.h"
17 #include "lmem.h"
18 #include "lobject.h"
19 #include "lstring.h"
20 #include "lundump.h"
21 #include "lzio.h"
23 typedef struct {
24 lua_State* L;
25 ZIO* Z;
26 Mbuffer* b;
27 const char* name;
28 } LoadState;
30 #ifdef LUAC_TRUST_BINARIES
31 #define IF(c,s)
32 #define error(S,s)
33 #else
34 #define IF(c,s) if (c) error(S,s)
36 static void error(LoadState* S, const char* why)
38 luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
39 luaD_throw(S->L,LUA_ERRSYNTAX);
41 #endif
43 #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
44 #define LoadByte(S) (lu_byte)LoadChar(S)
45 #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
46 #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
48 static void LoadBlock(LoadState* S, void* b, size_t size)
50 size_t r=luaZ_read(S->Z,b,size);
51 UNUSED(r);
52 IF (r!=0, "unexpected end");
55 static int LoadChar(LoadState* S)
57 char x;
58 LoadVar(S,x);
59 return x;
62 static int LoadInt(LoadState* S)
64 int x;
65 LoadVar(S,x);
66 IF (x<0, "bad integer");
67 return x;
70 static lua_Number LoadNumber(LoadState* S)
72 lua_Number x;
73 LoadVar(S,x);
74 return x;
77 static TString* LoadString(LoadState* S)
79 size_t size;
80 LoadVar(S,size);
81 if (size==0)
82 return NULL;
83 else
85 char* s=luaZ_openspace(S->L,S->b,size);
86 LoadBlock(S,s,size);
87 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
91 static void LoadCode(LoadState* S, Proto* f)
93 int n=LoadInt(S);
94 f->code=luaM_newvector(S->L,n,Instruction);
95 f->sizecode=n;
96 LoadVector(S,f->code,n,sizeof(Instruction));
99 static Proto* LoadFunction(LoadState* S, TString* p);
101 static void LoadConstants(LoadState* S, Proto* f)
103 int i,n;
104 n=LoadInt(S);
105 f->k=luaM_newvector(S->L,n,TValue);
106 f->sizek=n;
107 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
108 for (i=0; i<n; i++)
110 TValue* o=&f->k[i];
111 int t=LoadChar(S);
112 switch (t)
114 case LUA_TNIL:
115 setnilvalue(o);
116 break;
117 case LUA_TBOOLEAN:
118 setbvalue(o,LoadChar(S));
119 break;
120 case LUA_TNUMBER:
121 setnvalue(o,LoadNumber(S));
122 break;
123 case LUA_TSTRING:
124 setsvalue2n(S->L,o,LoadString(S));
125 break;
126 default:
127 error(S,"bad constant");
128 break;
131 n=LoadInt(S);
132 f->p=luaM_newvector(S->L,n,Proto*);
133 f->sizep=n;
134 for (i=0; i<n; i++) f->p[i]=NULL;
135 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
138 static void LoadDebug(LoadState* S, Proto* f)
140 int i,n;
141 n=LoadInt(S);
142 f->lineinfo=luaM_newvector(S->L,n,int);
143 f->sizelineinfo=n;
144 LoadVector(S,f->lineinfo,n,sizeof(int));
145 n=LoadInt(S);
146 f->locvars=luaM_newvector(S->L,n,LocVar);
147 f->sizelocvars=n;
148 for (i=0; i<n; i++) f->locvars[i].varname=NULL;
149 for (i=0; i<n; i++)
151 f->locvars[i].varname=LoadString(S);
152 f->locvars[i].startpc=LoadInt(S);
153 f->locvars[i].endpc=LoadInt(S);
155 n=LoadInt(S);
156 f->upvalues=luaM_newvector(S->L,n,TString*);
157 f->sizeupvalues=n;
158 for (i=0; i<n; i++) f->upvalues[i]=NULL;
159 for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
162 static Proto* LoadFunction(LoadState* S, TString* p)
164 Proto* f=luaF_newproto(S->L);
165 setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
166 f->source=LoadString(S); if (f->source==NULL) f->source=p;
167 f->linedefined=LoadInt(S);
168 f->lastlinedefined=LoadInt(S);
169 f->nups=LoadByte(S);
170 f->numparams=LoadByte(S);
171 f->is_vararg=LoadByte(S);
172 f->maxstacksize=LoadByte(S);
173 LoadCode(S,f);
174 LoadConstants(S,f);
175 LoadDebug(S,f);
176 IF (!luaG_checkcode(f), "bad code");
177 S->L->top--;
178 return f;
181 static void LoadHeader(LoadState* S)
183 char h[LUAC_HEADERSIZE];
184 char s[LUAC_HEADERSIZE];
185 luaU_header(h);
186 LoadBlock(S,s,LUAC_HEADERSIZE);
187 IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
191 ** load precompiled chunk
193 Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
195 LoadState S;
196 if (*name=='@' || *name=='=')
197 S.name=name+1;
198 else if (*name==LUA_SIGNATURE[0])
199 S.name="binary string";
200 else
201 S.name=name;
202 S.L=L;
203 S.Z=Z;
204 S.b=buff;
205 LoadHeader(&S);
206 return LoadFunction(&S,luaS_newliteral(L,"=?"));
210 * make header
212 void luaU_header (char* h)
214 int x=1;
215 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
216 h+=sizeof(LUA_SIGNATURE)-1;
217 *h++=(char)LUAC_VERSION;
218 *h++=(char)LUAC_FORMAT;
219 *h++=(char)*(char*)&x; /* endianness */
220 *h++=(char)sizeof(int);
221 *h++=(char)sizeof(size_t);
222 *h++=(char)sizeof(Instruction);
223 *h++=(char)sizeof(lua_Number);
224 *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */