Dash:
[t2-trunk.git] / misc / luabash / lua / lundump.c
blob7fc635eeb7bebb9ba08ca0151ce24bf0c0acb910
1 /*
2 ** $Id: lundump.c,v 1.60 2006/02/16 15:53:49 lhf 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 #else
33 #define IF(c,s) if (c) error(S,s)
35 static void error(LoadState* S, const char* why)
37 luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
38 luaD_throw(S->L,LUA_ERRSYNTAX);
40 #endif
42 #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
43 #define LoadByte(S) (lu_byte)LoadChar(S)
44 #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
45 #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
47 static void LoadBlock(LoadState* S, void* b, size_t size)
49 size_t r=luaZ_read(S->Z,b,size);
50 IF (r!=0, "unexpected end");
53 static int LoadChar(LoadState* S)
55 char x;
56 LoadVar(S,x);
57 return x;
60 static int LoadInt(LoadState* S)
62 int x;
63 LoadVar(S,x);
64 IF (x<0, "bad integer");
65 return x;
68 static lua_Number LoadNumber(LoadState* S)
70 lua_Number x;
71 LoadVar(S,x);
72 return x;
75 static TString* LoadString(LoadState* S)
77 size_t size;
78 LoadVar(S,size);
79 if (size==0)
80 return NULL;
81 else
83 char* s=luaZ_openspace(S->L,S->b,size);
84 LoadBlock(S,s,size);
85 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
89 static void LoadCode(LoadState* S, Proto* f)
91 int n=LoadInt(S);
92 f->code=luaM_newvector(S->L,n,Instruction);
93 f->sizecode=n;
94 LoadVector(S,f->code,n,sizeof(Instruction));
97 static Proto* LoadFunction(LoadState* S, TString* p);
99 static void LoadConstants(LoadState* S, Proto* f)
101 int i,n;
102 n=LoadInt(S);
103 f->k=luaM_newvector(S->L,n,TValue);
104 f->sizek=n;
105 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
106 for (i=0; i<n; i++)
108 TValue* o=&f->k[i];
109 int t=LoadChar(S);
110 switch (t)
112 case LUA_TNIL:
113 setnilvalue(o);
114 break;
115 case LUA_TBOOLEAN:
116 setbvalue(o,LoadChar(S));
117 break;
118 case LUA_TNUMBER:
119 setnvalue(o,LoadNumber(S));
120 break;
121 case LUA_TSTRING:
122 setsvalue2n(S->L,o,LoadString(S));
123 break;
124 default:
125 IF (1, "bad constant");
126 break;
129 n=LoadInt(S);
130 f->p=luaM_newvector(S->L,n,Proto*);
131 f->sizep=n;
132 for (i=0; i<n; i++) f->p[i]=NULL;
133 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
136 static void LoadDebug(LoadState* S, Proto* f)
138 int i,n;
139 n=LoadInt(S);
140 f->lineinfo=luaM_newvector(S->L,n,int);
141 f->sizelineinfo=n;
142 LoadVector(S,f->lineinfo,n,sizeof(int));
143 n=LoadInt(S);
144 f->locvars=luaM_newvector(S->L,n,LocVar);
145 f->sizelocvars=n;
146 for (i=0; i<n; i++) f->locvars[i].varname=NULL;
147 for (i=0; i<n; i++)
149 f->locvars[i].varname=LoadString(S);
150 f->locvars[i].startpc=LoadInt(S);
151 f->locvars[i].endpc=LoadInt(S);
153 n=LoadInt(S);
154 f->upvalues=luaM_newvector(S->L,n,TString*);
155 f->sizeupvalues=n;
156 for (i=0; i<n; i++) f->upvalues[i]=NULL;
157 for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
160 static Proto* LoadFunction(LoadState* S, TString* p)
162 Proto* f=luaF_newproto(S->L);
163 setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
164 f->source=LoadString(S); if (f->source==NULL) f->source=p;
165 f->linedefined=LoadInt(S);
166 f->lastlinedefined=LoadInt(S);
167 f->nups=LoadByte(S);
168 f->numparams=LoadByte(S);
169 f->is_vararg=LoadByte(S);
170 f->maxstacksize=LoadByte(S);
171 LoadCode(S,f);
172 LoadConstants(S,f);
173 LoadDebug(S,f);
174 IF (!luaG_checkcode(f), "bad code");
175 S->L->top--;
176 return f;
179 static void LoadHeader(LoadState* S)
181 char h[LUAC_HEADERSIZE];
182 char s[LUAC_HEADERSIZE];
183 luaU_header(h);
184 LoadBlock(S,s,LUAC_HEADERSIZE);
185 IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
189 ** load precompiled chunk
191 Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
193 LoadState S;
194 if (*name=='@' || *name=='=')
195 S.name=name+1;
196 else if (*name==LUA_SIGNATURE[0])
197 S.name="binary string";
198 else
199 S.name=name;
200 S.L=L;
201 S.Z=Z;
202 S.b=buff;
203 LoadHeader(&S);
204 return LoadFunction(&S,luaS_newliteral(L,"=?"));
208 * make header
210 void luaU_header (char* h)
212 int x=1;
213 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
214 h+=sizeof(LUA_SIGNATURE)-1;
215 *h++=(char)LUAC_VERSION;
216 *h++=(char)LUAC_FORMAT;
217 *h++=(char)*(char*)&x; /* endianness */
218 *h++=(char)sizeof(int);
219 *h++=(char)sizeof(size_t);
220 *h++=(char)sizeof(Instruction);
221 *h++=(char)sizeof(lua_Number);
222 *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */