diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2007-12-27 11:02:25 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2007-12-27 11:02:25 -0200 |
commit | f5ae26ec6c2eb69f03f316e59c1e1977c52c7c23 (patch) | |
tree | b4c761dac7648caea2cc20f8ebc2dd5b27a3fa1e /lundump.c | |
parent | 98194db4295726069137d13b8d24fca8cbf892b6 (diff) | |
download | lua-f5ae26ec6c2eb69f03f316e59c1e1977c52c7c23.tar.gz lua-f5ae26ec6c2eb69f03f316e59c1e1977c52c7c23.tar.bz2 lua-f5ae26ec6c2eb69f03f316e59c1e1977c52c7c23.zip |
official branch for Lua 5.1
Diffstat (limited to 'lundump.c')
-rw-r--r-- | lundump.c | 223 |
1 files changed, 0 insertions, 223 deletions
diff --git a/lundump.c b/lundump.c deleted file mode 100644 index 7fc635ee..00000000 --- a/lundump.c +++ /dev/null | |||
@@ -1,223 +0,0 @@ | |||
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 | */ | ||
6 | |||
7 | #include <string.h> | ||
8 | |||
9 | #define lundump_c | ||
10 | #define LUA_CORE | ||
11 | |||
12 | #include "lua.h" | ||
13 | |||
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" | ||
22 | |||
23 | typedef struct { | ||
24 | lua_State* L; | ||
25 | ZIO* Z; | ||
26 | Mbuffer* b; | ||
27 | const char* name; | ||
28 | } LoadState; | ||
29 | |||
30 | #ifdef LUAC_TRUST_BINARIES | ||
31 | #define IF(c,s) | ||
32 | #else | ||
33 | #define IF(c,s) if (c) error(S,s) | ||
34 | |||
35 | static void error(LoadState* S, const char* why) | ||
36 | { | ||
37 | luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); | ||
38 | luaD_throw(S->L,LUA_ERRSYNTAX); | ||
39 | } | ||
40 | #endif | ||
41 | |||
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) | ||
46 | |||
47 | static void LoadBlock(LoadState* S, void* b, size_t size) | ||
48 | { | ||
49 | size_t r=luaZ_read(S->Z,b,size); | ||
50 | IF (r!=0, "unexpected end"); | ||
51 | } | ||
52 | |||
53 | static int LoadChar(LoadState* S) | ||
54 | { | ||
55 | char x; | ||
56 | LoadVar(S,x); | ||
57 | return x; | ||
58 | } | ||
59 | |||
60 | static int LoadInt(LoadState* S) | ||
61 | { | ||
62 | int x; | ||
63 | LoadVar(S,x); | ||
64 | IF (x<0, "bad integer"); | ||
65 | return x; | ||
66 | } | ||
67 | |||
68 | static lua_Number LoadNumber(LoadState* S) | ||
69 | { | ||
70 | lua_Number x; | ||
71 | LoadVar(S,x); | ||
72 | return x; | ||
73 | } | ||
74 | |||
75 | static TString* LoadString(LoadState* S) | ||
76 | { | ||
77 | size_t size; | ||
78 | LoadVar(S,size); | ||
79 | if (size==0) | ||
80 | return NULL; | ||
81 | else | ||
82 | { | ||
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' */ | ||
86 | } | ||
87 | } | ||
88 | |||
89 | static void LoadCode(LoadState* S, Proto* f) | ||
90 | { | ||
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)); | ||
95 | } | ||
96 | |||
97 | static Proto* LoadFunction(LoadState* S, TString* p); | ||
98 | |||
99 | static void LoadConstants(LoadState* S, Proto* f) | ||
100 | { | ||
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++) | ||
107 | { | ||
108 | TValue* o=&f->k[i]; | ||
109 | int t=LoadChar(S); | ||
110 | switch (t) | ||
111 | { | ||
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; | ||
127 | } | ||
128 | } | ||
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); | ||
134 | } | ||
135 | |||
136 | static void LoadDebug(LoadState* S, Proto* f) | ||
137 | { | ||
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++) | ||
148 | { | ||
149 | f->locvars[i].varname=LoadString(S); | ||
150 | f->locvars[i].startpc=LoadInt(S); | ||
151 | f->locvars[i].endpc=LoadInt(S); | ||
152 | } | ||
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); | ||
158 | } | ||
159 | |||
160 | static Proto* LoadFunction(LoadState* S, TString* p) | ||
161 | { | ||
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; | ||
177 | } | ||
178 | |||
179 | static void LoadHeader(LoadState* S) | ||
180 | { | ||
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"); | ||
186 | } | ||
187 | |||
188 | /* | ||
189 | ** load precompiled chunk | ||
190 | */ | ||
191 | Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) | ||
192 | { | ||
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,"=?")); | ||
205 | } | ||
206 | |||
207 | /* | ||
208 | * make header | ||
209 | */ | ||
210 | void luaU_header (char* h) | ||
211 | { | ||
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? */ | ||
223 | } | ||