source: trunk/src/lua/lundump.c @ 2542

Last change on this file since 2542 was 2542, checked in by sam, 10 years ago

lua: compilation fixes for non-quite-POSIX platforms.

File size: 5.6 KB
Line 
1/*
2** $Id: lundump.c,v 2.22 2012/05/08 13:53:33 roberto Exp $
3** load precompiled Lua chunks
4** See Copyright Notice in lua.h
5*/
6
7#if defined HAVE_CONFIG_H // LOL BEGIN
8#   include "config.h"
9#endif // LOL END
10
11#include <string.h>
12
13#define lundump_c
14#define LUA_CORE
15
16#include "lua.h"
17
18#include "ldebug.h"
19#include "ldo.h"
20#include "lfunc.h"
21#include "lmem.h"
22#include "lobject.h"
23#include "lstring.h"
24#include "lundump.h"
25#include "lzio.h"
26
27typedef struct {
28 lua_State* L;
29 ZIO* Z;
30 Mbuffer* b;
31 const char* name;
32} LoadState;
33
34static l_noret error(LoadState* S, const char* why)
35{
36 luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
37 luaD_throw(S->L,LUA_ERRSYNTAX);
38}
39
40#define LoadMem(S,b,n,size)     LoadBlock(S,b,(n)*(size))
41#define LoadByte(S)             (lu_byte)LoadChar(S)
42#define LoadVar(S,x)            LoadMem(S,&x,1,sizeof(x))
43#define LoadVector(S,b,n,size)  LoadMem(S,b,n,size)
44
45#if !defined(luai_verifycode)
46#define luai_verifycode(L,b,f)  /* empty */
47#endif
48
49static void LoadBlock(LoadState* S, void* b, size_t size)
50{
51 if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated");
52}
53
54static int LoadChar(LoadState* S)
55{
56 char x;
57 LoadVar(S,x);
58 return x;
59}
60
61static int LoadInt(LoadState* S)
62{
63 int x;
64 LoadVar(S,x);
65 if (x<0) error(S,"corrupted");
66 return x;
67}
68
69static lua_Number LoadNumber(LoadState* S)
70{
71 lua_Number x;
72 LoadVar(S,x);
73 return x;
74}
75
76static TString* LoadString(LoadState* S)
77{
78 size_t size;
79 LoadVar(S,size);
80 if (size==0)
81  return NULL;
82 else
83 {
84  char* s=luaZ_openspace(S->L,S->b,size);
85  LoadBlock(S,s,size*sizeof(char));
86  return luaS_newlstr(S->L,s,size-1);           /* remove trailing '\0' */
87 }
88}
89
90static void LoadCode(LoadState* S, Proto* f)
91{
92 int n=LoadInt(S);
93 f->code=luaM_newvector(S->L,n,Instruction);
94 f->sizecode=n;
95 LoadVector(S,f->code,n,sizeof(Instruction));
96}
97
98static void LoadFunction(LoadState* S, Proto* f);
99
100static void LoadConstants(LoadState* S, Proto* f)
101{
102 int i,n;
103 n=LoadInt(S);
104 f->k=luaM_newvector(S->L,n,TValue);
105 f->sizek=n;
106 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
107 for (i=0; i<n; i++)
108 {
109  TValue* o=&f->k[i];
110  int t=LoadChar(S);
111  switch (t)
112  {
113   case LUA_TNIL:
114        setnilvalue(o);
115        break;
116   case LUA_TBOOLEAN:
117        setbvalue(o,LoadChar(S));
118        break;
119   case LUA_TNUMBER:
120        setnvalue(o,LoadNumber(S));
121        break;
122   case LUA_TSTRING:
123        setsvalue2n(S->L,o,LoadString(S));
124        break;
125    default: lua_assert(0);
126  }
127 }
128 n=LoadInt(S);
129 f->p=luaM_newvector(S->L,n,Proto*);
130 f->sizep=n;
131 for (i=0; i<n; i++) f->p[i]=NULL;
132 for (i=0; i<n; i++)
133 {
134  f->p[i]=luaF_newproto(S->L);
135  LoadFunction(S,f->p[i]);
136 }
137}
138
139static void LoadUpvalues(LoadState* S, Proto* f)
140{
141 int i,n;
142 n=LoadInt(S);
143 f->upvalues=luaM_newvector(S->L,n,Upvaldesc);
144 f->sizeupvalues=n;
145 for (i=0; i<n; i++) f->upvalues[i].name=NULL;
146 for (i=0; i<n; i++)
147 {
148  f->upvalues[i].instack=LoadByte(S);
149  f->upvalues[i].idx=LoadByte(S);
150 }
151}
152
153static void LoadDebug(LoadState* S, Proto* f)
154{
155 int i,n;
156 f->source=LoadString(S);
157 n=LoadInt(S);
158 f->lineinfo=luaM_newvector(S->L,n,int);
159 f->sizelineinfo=n;
160 LoadVector(S,f->lineinfo,n,sizeof(int));
161 n=LoadInt(S);
162 f->locvars=luaM_newvector(S->L,n,LocVar);
163 f->sizelocvars=n;
164 for (i=0; i<n; i++) f->locvars[i].varname=NULL;
165 for (i=0; i<n; i++)
166 {
167  f->locvars[i].varname=LoadString(S);
168  f->locvars[i].startpc=LoadInt(S);
169  f->locvars[i].endpc=LoadInt(S);
170 }
171 n=LoadInt(S);
172 for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
173}
174
175static void LoadFunction(LoadState* S, Proto* f)
176{
177 f->linedefined=LoadInt(S);
178 f->lastlinedefined=LoadInt(S);
179 f->numparams=LoadByte(S);
180 f->is_vararg=LoadByte(S);
181 f->maxstacksize=LoadByte(S);
182 LoadCode(S,f);
183 LoadConstants(S,f);
184 LoadUpvalues(S,f);
185 LoadDebug(S,f);
186}
187
188/* the code below must be consistent with the code in luaU_header */
189#define N0      LUAC_HEADERSIZE
190#define N1      (sizeof(LUA_SIGNATURE)-sizeof(char))
191#define N2      N1+2
192#define N3      N2+6
193
194static void LoadHeader(LoadState* S)
195{
196 lu_byte h[LUAC_HEADERSIZE];
197 lu_byte s[LUAC_HEADERSIZE];
198 luaU_header(h);
199 memcpy(s,h,sizeof(char));                      /* first char already read */
200 LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char));
201 if (memcmp(h,s,N0)==0) return;
202 if (memcmp(h,s,N1)!=0) error(S,"not a");
203 if (memcmp(h,s,N2)!=0) error(S,"version mismatch in");
204 if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted");
205}
206
207/*
208** load precompiled chunk
209*/
210Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
211{
212 LoadState S;
213 Closure* cl;
214 if (*name=='@' || *name=='=')
215  S.name=name+1;
216 else if (*name==LUA_SIGNATURE[0])
217  S.name="binary string";
218 else
219  S.name=name;
220 S.L=L;
221 S.Z=Z;
222 S.b=buff;
223 LoadHeader(&S);
224 cl=luaF_newLclosure(L,1);
225 setclLvalue(L,L->top,cl); incr_top(L);
226 cl->l.p=luaF_newproto(L);
227 LoadFunction(&S,cl->l.p);
228 if (cl->l.p->sizeupvalues != 1)
229 {
230  Proto* p=cl->l.p;
231  cl=luaF_newLclosure(L,cl->l.p->sizeupvalues);
232  cl->l.p=p;
233  setclLvalue(L,L->top-1,cl);
234 }
235 luai_verifycode(L,buff,cl->l.p);
236 return cl;
237}
238
239#define MYINT(s)        (s[0]-'0')
240#define VERSION         MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
241#define FORMAT          0               /* this is the official format */
242
243/*
244* make header for precompiled chunks
245* if you change the code below be sure to update LoadHeader and FORMAT above
246* and LUAC_HEADERSIZE in lundump.h
247*/
248void luaU_header (lu_byte* h)
249{
250 int x=1;
251 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char));
252 h+=sizeof(LUA_SIGNATURE)-sizeof(char);
253 *h++=cast_byte(VERSION);
254 *h++=cast_byte(FORMAT);
255 *h++=cast_byte(*(char*)&x);                    /* endianness */
256 *h++=cast_byte(sizeof(int));
257 *h++=cast_byte(sizeof(size_t));
258 *h++=cast_byte(sizeof(Instruction));
259 *h++=cast_byte(sizeof(lua_Number));
260 *h++=cast_byte(((lua_Number)0.5)==0);          /* is lua_Number integral? */
261 memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char));
262}
Note: See TracBrowser for help on using the repository browser.