diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-10-25 12:33:38 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-10-25 12:33:38 -0200 |
commit | 1475cb59bf16c6af7ecd937e13da0ca0f451a191 (patch) | |
tree | 5f74b1bbd198b71dbfc752669f65bd958771a9e1 | |
parent | 9e8e60dd5f2223aaabd8239b9152167a8b630b4a (diff) | |
download | lua-1475cb59bf16c6af7ecd937e13da0ca0f451a191.tar.gz lua-1475cb59bf16c6af7ecd937e13da0ca0f451a191.tar.bz2 lua-1475cb59bf16c6af7ecd937e13da0ca0f451a191.zip |
version from lhf
-rw-r--r-- | ldump.c | 15 | ||||
-rw-r--r-- | lundump.c | 40 |
2 files changed, 20 insertions, 35 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldump.c,v 2.12 2009/09/30 15:38:37 roberto Exp roberto $ | 2 | ** $Id: ldump.c,v 1.17 2010/10/13 21:04:52 lhf Exp $ |
3 | ** save precompiled Lua chunks | 3 | ** save precompiled Lua chunks |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -75,7 +75,7 @@ static void DumpString(const TString* s, DumpState* D) | |||
75 | 75 | ||
76 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) | 76 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) |
77 | 77 | ||
78 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D); | 78 | static void DumpFunction(const Proto* f, DumpState* D); |
79 | 79 | ||
80 | static void DumpConstants(const Proto* f, DumpState* D) | 80 | static void DumpConstants(const Proto* f, DumpState* D) |
81 | { | 81 | { |
@@ -98,14 +98,11 @@ static void DumpConstants(const Proto* f, DumpState* D) | |||
98 | case LUA_TSTRING: | 98 | case LUA_TSTRING: |
99 | DumpString(rawtsvalue(o),D); | 99 | DumpString(rawtsvalue(o),D); |
100 | break; | 100 | break; |
101 | default: | ||
102 | lua_assert(0); /* cannot happen */ | ||
103 | break; | ||
104 | } | 101 | } |
105 | } | 102 | } |
106 | n=f->sizep; | 103 | n=f->sizep; |
107 | DumpInt(n,D); | 104 | DumpInt(n,D); |
108 | for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D); | 105 | for (i=0; i<n; i++) DumpFunction(f->p[i],D); |
109 | } | 106 | } |
110 | 107 | ||
111 | static void DumpUpvalues(const Proto* f, DumpState* D) | 108 | static void DumpUpvalues(const Proto* f, DumpState* D) |
@@ -122,6 +119,7 @@ static void DumpUpvalues(const Proto* f, DumpState* D) | |||
122 | static void DumpDebug(const Proto* f, DumpState* D) | 119 | static void DumpDebug(const Proto* f, DumpState* D) |
123 | { | 120 | { |
124 | int i,n; | 121 | int i,n; |
122 | DumpString((D->strip) ? NULL : f->source,D); | ||
125 | n= (D->strip) ? 0 : f->sizelineinfo; | 123 | n= (D->strip) ? 0 : f->sizelineinfo; |
126 | DumpVector(f->lineinfo,n,sizeof(int),D); | 124 | DumpVector(f->lineinfo,n,sizeof(int),D); |
127 | n= (D->strip) ? 0 : f->sizelocvars; | 125 | n= (D->strip) ? 0 : f->sizelocvars; |
@@ -137,9 +135,8 @@ static void DumpDebug(const Proto* f, DumpState* D) | |||
137 | for (i=0; i<n; i++) DumpString(f->upvalues[i].name,D); | 135 | for (i=0; i<n; i++) DumpString(f->upvalues[i].name,D); |
138 | } | 136 | } |
139 | 137 | ||
140 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D) | 138 | static void DumpFunction(const Proto* f, DumpState* D) |
141 | { | 139 | { |
142 | DumpString((f->source==p || D->strip) ? NULL : f->source,D); | ||
143 | DumpInt(f->linedefined,D); | 140 | DumpInt(f->linedefined,D); |
144 | DumpInt(f->lastlinedefined,D); | 141 | DumpInt(f->lastlinedefined,D); |
145 | DumpChar(f->numparams,D); | 142 | DumpChar(f->numparams,D); |
@@ -170,6 +167,6 @@ int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip | |||
170 | D.strip=strip; | 167 | D.strip=strip; |
171 | D.status=0; | 168 | D.status=0; |
172 | DumpHeader(&D); | 169 | DumpHeader(&D); |
173 | DumpFunction(f,NULL,&D); | 170 | DumpFunction(f,&D); |
174 | return D.status; | 171 | return D.status; |
175 | } | 172 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lundump.c,v 2.12 2009/09/30 15:38:37 roberto Exp roberto $ | 2 | ** $Id: lundump.c,v 1.67 2010/10/13 21:04:52 lhf Exp $ |
3 | ** load precompiled Lua chunks | 3 | ** load precompiled Lua chunks |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -27,27 +27,20 @@ typedef struct { | |||
27 | const char* name; | 27 | const char* name; |
28 | } LoadState; | 28 | } LoadState; |
29 | 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) | 30 | static void error(LoadState* S, const char* why) |
36 | { | 31 | { |
37 | luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); | 32 | luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why); |
38 | luaD_throw(S->L,LUA_ERRSYNTAX); | 33 | luaD_throw(S->L,LUA_ERRSYNTAX); |
39 | } | 34 | } |
40 | #endif | ||
41 | 35 | ||
42 | #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) | 36 | #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) |
43 | #define LoadByte(S) (lu_byte)LoadChar(S) | 37 | #define LoadByte(S) (lu_byte)LoadChar(S) |
44 | #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) | 38 | #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) |
45 | #define LoadVector(S,b,n,size) LoadMem(S,b,n,size) | 39 | #define LoadVector(S,b,n,size) LoadMem(S,b,n,size) |
46 | 40 | ||
47 | static void LoadBlock(LoadState* S, void* b, size_t size) | 41 | static void LoadBlock(LoadState* S, void* b, size_t size) |
48 | { | 42 | { |
49 | size_t r=luaZ_read(S->Z,b,size); | 43 | if (luaZ_read(S->Z,b,size)!=0) error(S,"corrupted"); |
50 | IF (r!=0, "unexpected end"); | ||
51 | } | 44 | } |
52 | 45 | ||
53 | static int LoadChar(LoadState* S) | 46 | static int LoadChar(LoadState* S) |
@@ -61,7 +54,6 @@ static int LoadInt(LoadState* S) | |||
61 | { | 54 | { |
62 | int x; | 55 | int x; |
63 | LoadVar(S,x); | 56 | LoadVar(S,x); |
64 | IF (x<0, "bad integer"); | ||
65 | return x; | 57 | return x; |
66 | } | 58 | } |
67 | 59 | ||
@@ -94,7 +86,7 @@ static void LoadCode(LoadState* S, Proto* f) | |||
94 | LoadVector(S,f->code,n,sizeof(Instruction)); | 86 | LoadVector(S,f->code,n,sizeof(Instruction)); |
95 | } | 87 | } |
96 | 88 | ||
97 | static Proto* LoadFunction(LoadState* S, TString* p); | 89 | static Proto* LoadFunction(LoadState* S); |
98 | 90 | ||
99 | static void LoadConstants(LoadState* S, Proto* f) | 91 | static void LoadConstants(LoadState* S, Proto* f) |
100 | { | 92 | { |
@@ -113,7 +105,7 @@ static void LoadConstants(LoadState* S, Proto* f) | |||
113 | setnilvalue(o); | 105 | setnilvalue(o); |
114 | break; | 106 | break; |
115 | case LUA_TBOOLEAN: | 107 | case LUA_TBOOLEAN: |
116 | setbvalue(o,LoadChar(S)!=0); | 108 | setbvalue(o,LoadChar(S)); |
117 | break; | 109 | break; |
118 | case LUA_TNUMBER: | 110 | case LUA_TNUMBER: |
119 | setnvalue(o,LoadNumber(S)); | 111 | setnvalue(o,LoadNumber(S)); |
@@ -121,16 +113,13 @@ static void LoadConstants(LoadState* S, Proto* f) | |||
121 | case LUA_TSTRING: | 113 | case LUA_TSTRING: |
122 | setsvalue2n(S->L,o,LoadString(S)); | 114 | setsvalue2n(S->L,o,LoadString(S)); |
123 | break; | 115 | break; |
124 | default: | ||
125 | IF (1, "bad constant"); | ||
126 | break; | ||
127 | } | 116 | } |
128 | } | 117 | } |
129 | n=LoadInt(S); | 118 | n=LoadInt(S); |
130 | f->p=luaM_newvector(S->L,n,Proto*); | 119 | f->p=luaM_newvector(S->L,n,Proto*); |
131 | f->sizep=n; | 120 | f->sizep=n; |
132 | for (i=0; i<n; i++) f->p[i]=NULL; | 121 | for (i=0; i<n; i++) f->p[i]=NULL; |
133 | for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source); | 122 | for (i=0; i<n; i++) f->p[i]=LoadFunction(S); |
134 | } | 123 | } |
135 | 124 | ||
136 | static void LoadUpvalues(LoadState* S, Proto* f) | 125 | static void LoadUpvalues(LoadState* S, Proto* f) |
@@ -150,6 +139,7 @@ static void LoadUpvalues(LoadState* S, Proto* f) | |||
150 | static void LoadDebug(LoadState* S, Proto* f) | 139 | static void LoadDebug(LoadState* S, Proto* f) |
151 | { | 140 | { |
152 | int i,n; | 141 | int i,n; |
142 | f->source=LoadString(S); | ||
153 | n=LoadInt(S); | 143 | n=LoadInt(S); |
154 | f->lineinfo=luaM_newvector(S->L,n,int); | 144 | f->lineinfo=luaM_newvector(S->L,n,int); |
155 | f->sizelineinfo=n; | 145 | f->sizelineinfo=n; |
@@ -168,13 +158,10 @@ static void LoadDebug(LoadState* S, Proto* f) | |||
168 | for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S); | 158 | for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S); |
169 | } | 159 | } |
170 | 160 | ||
171 | static Proto* LoadFunction(LoadState* S, TString* p) | 161 | static Proto* LoadFunction(LoadState* S) |
172 | { | 162 | { |
173 | Proto* f; | 163 | Proto* f=luaF_newproto(S->L); |
174 | if (++G(S->L)->nCcalls > LUAI_MAXCCALLS) error(S, "function nest too deep"); | ||
175 | f=luaF_newproto(S->L); | ||
176 | setptvalue2s(S->L,S->L->top,f); incr_top(S->L); | 164 | setptvalue2s(S->L,S->L->top,f); incr_top(S->L); |
177 | f->source=LoadString(S); if (f->source==NULL) f->source=p; | ||
178 | f->linedefined=LoadInt(S); | 165 | f->linedefined=LoadInt(S); |
179 | f->lastlinedefined=LoadInt(S); | 166 | f->lastlinedefined=LoadInt(S); |
180 | f->numparams=LoadByte(S); | 167 | f->numparams=LoadByte(S); |
@@ -185,7 +172,6 @@ static Proto* LoadFunction(LoadState* S, TString* p) | |||
185 | LoadUpvalues(S,f); | 172 | LoadUpvalues(S,f); |
186 | LoadDebug(S,f); | 173 | LoadDebug(S,f); |
187 | S->L->top--; | 174 | S->L->top--; |
188 | G(S->L)->nCcalls--; | ||
189 | return f; | 175 | return f; |
190 | } | 176 | } |
191 | 177 | ||
@@ -195,7 +181,7 @@ static void LoadHeader(LoadState* S) | |||
195 | char s[LUAC_HEADERSIZE]; | 181 | char s[LUAC_HEADERSIZE]; |
196 | luaU_header(h); | 182 | luaU_header(h); |
197 | LoadBlock(S,s,LUAC_HEADERSIZE); | 183 | LoadBlock(S,s,LUAC_HEADERSIZE); |
198 | IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); | 184 | if (memcmp(h,s,LUAC_HEADERSIZE)!=0) error(S,"incompatible"); |
199 | } | 185 | } |
200 | 186 | ||
201 | /* | 187 | /* |
@@ -214,11 +200,13 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) | |||
214 | S.Z=Z; | 200 | S.Z=Z; |
215 | S.b=buff; | 201 | S.b=buff; |
216 | LoadHeader(&S); | 202 | LoadHeader(&S); |
217 | return LoadFunction(&S,luaS_newliteral(L,"=?")); | 203 | return LoadFunction(&S); |
218 | } | 204 | } |
219 | 205 | ||
220 | /* | 206 | /* |
221 | * make header | 207 | * make header |
208 | * if you make any changes in the header or in LUA_SIGNATURE, | ||
209 | * be sure to update LUAC_HEADERSIZE accordingly in lundump.h. | ||
222 | */ | 210 | */ |
223 | void luaU_header (char* h) | 211 | void luaU_header (char* h) |
224 | { | 212 | { |