diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-12 10:37:19 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-12 10:37:19 -0300 |
| commit | 221b5be7b78dc401d4fac49df534f7ff68c04d58 (patch) | |
| tree | 1eea31118a5e93a9ea1dca7c45d85a92c2726bb7 | |
| parent | b6791f5fa552cbf299a754c1c750f8422c6fb991 (diff) | |
| download | lua-221b5be7b78dc401d4fac49df534f7ff68c04d58.tar.gz lua-221b5be7b78dc401d4fac49df534f7ff68c04d58.tar.bz2 lua-221b5be7b78dc401d4fac49df534f7ff68c04d58.zip | |
use of `LoadState' struct (instead of several separate arguments)
| -rw-r--r-- | lundump.c | 211 |
1 files changed, 109 insertions, 102 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lundump.c,v 1.50 2002/06/17 13:51:01 roberto Exp roberto $ | 2 | ** $Id: lundump.c,v 1.43 2002/08/07 00:36:03 lhf Exp $ |
| 3 | ** load pre-compiled Lua chunks | 3 | ** load pre-compiled Lua chunks |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -16,216 +16,212 @@ | |||
| 16 | 16 | ||
| 17 | #define LoadByte (lu_byte) ezgetc | 17 | #define LoadByte (lu_byte) ezgetc |
| 18 | 18 | ||
| 19 | static const char* ZNAME (ZIO* Z) | 19 | typedef struct { |
| 20 | { | 20 | lua_State* L; |
| 21 | const char* s=zname(Z); | 21 | ZIO* Z; |
| 22 | if (*s=='@' || *s=='=') | 22 | int swap; |
| 23 | return s+1; | 23 | const char* name; |
| 24 | else if (*s==LUA_SIGNATURE[0]) | 24 | } LoadState; |
| 25 | return "binary string"; | ||
| 26 | else | ||
| 27 | return s; | ||
| 28 | } | ||
| 29 | 25 | ||
| 30 | static void unexpectedEOZ (lua_State* L, ZIO* Z) | 26 | static void unexpectedEOZ (LoadState* S) |
| 31 | { | 27 | { |
| 32 | luaG_runerror(L,"unexpected end of file in %s",ZNAME(Z)); | 28 | luaG_runerror(S->L,"unexpected end of file in %s",S->name); |
| 33 | } | 29 | } |
| 34 | 30 | ||
| 35 | static int ezgetc (lua_State* L, ZIO* Z) | 31 | static int ezgetc (LoadState* S) |
| 36 | { | 32 | { |
| 37 | int c=zgetc(Z); | 33 | int c=zgetc(S->Z); |
| 38 | if (c==EOZ) unexpectedEOZ(L,Z); | 34 | if (c==EOZ) unexpectedEOZ(S); |
| 39 | return c; | 35 | return c; |
| 40 | } | 36 | } |
| 41 | 37 | ||
| 42 | static void ezread (lua_State* L, ZIO* Z, void* b, int n) | 38 | static void ezread (LoadState* S, void* b, int n) |
| 43 | { | 39 | { |
| 44 | int r=luaZ_read(Z,b,n); | 40 | int r=luaZ_read(S->Z,b,n); |
| 45 | if (r!=0) unexpectedEOZ(L,Z); | 41 | if (r!=0) unexpectedEOZ(S); |
| 46 | } | 42 | } |
| 47 | 43 | ||
| 48 | static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap) | 44 | static void LoadBlock (LoadState* S, void* b, size_t size) |
| 49 | { | 45 | { |
| 50 | if (swap) | 46 | if (S->swap) |
| 51 | { | 47 | { |
| 52 | char *p=(char*) b+size-1; | 48 | char *p=(char*) b+size-1; |
| 53 | int n=size; | 49 | int n=size; |
| 54 | while (n--) *p--=(char)ezgetc(L,Z); | 50 | while (n--) *p--=(char)ezgetc(S); |
| 55 | } | 51 | } |
| 56 | else | 52 | else |
| 57 | ezread(L,Z,b,size); | 53 | ezread(S,b,size); |
| 58 | } | 54 | } |
| 59 | 55 | ||
| 60 | static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int swap) | 56 | static void LoadVector (LoadState* S, void* b, int m, size_t size) |
| 61 | { | 57 | { |
| 62 | if (swap) | 58 | if (S->swap) |
| 63 | { | 59 | { |
| 64 | char *q=(char*) b; | 60 | char *q=(char*) b; |
| 65 | while (m--) | 61 | while (m--) |
| 66 | { | 62 | { |
| 67 | char *p=q+size-1; | 63 | char *p=q+size-1; |
| 68 | int n=size; | 64 | int n=size; |
| 69 | while (n--) *p--=(char)ezgetc(L,Z); | 65 | while (n--) *p--=(char)ezgetc(S); |
| 70 | q+=size; | 66 | q+=size; |
| 71 | } | 67 | } |
| 72 | } | 68 | } |
| 73 | else | 69 | else |
| 74 | ezread(L,Z,b,m*size); | 70 | ezread(S,b,m*size); |
| 75 | } | 71 | } |
| 76 | 72 | ||
| 77 | static int LoadInt (lua_State* L, ZIO* Z, int swap) | 73 | static int LoadInt (LoadState* S) |
| 78 | { | 74 | { |
| 79 | int x; | 75 | int x; |
| 80 | LoadBlock(L,&x,sizeof(x),Z,swap); | 76 | LoadBlock(S,&x,sizeof(x)); |
| 81 | if (x<0) luaG_runerror(L,"bad integer in %s",ZNAME(Z)); | 77 | if (x<0) luaG_runerror(S->L,"bad integer in %s",S->name); |
| 82 | return x; | 78 | return x; |
| 83 | } | 79 | } |
| 84 | 80 | ||
| 85 | static size_t LoadSize (lua_State* L, ZIO* Z, int swap) | 81 | static size_t LoadSize (LoadState* S) |
| 86 | { | 82 | { |
| 87 | size_t x; | 83 | size_t x; |
| 88 | LoadBlock(L,&x,sizeof(x),Z,swap); | 84 | LoadBlock(S,&x,sizeof(x)); |
| 89 | return x; | 85 | return x; |
| 90 | } | 86 | } |
| 91 | 87 | ||
| 92 | static lua_Number LoadNumber (lua_State* L, ZIO* Z, int swap) | 88 | static lua_Number LoadNumber (LoadState* S) |
| 93 | { | 89 | { |
| 94 | lua_Number x; | 90 | lua_Number x; |
| 95 | LoadBlock(L,&x,sizeof(x),Z,swap); | 91 | LoadBlock(S,&x,sizeof(x)); |
| 96 | return x; | 92 | return x; |
| 97 | } | 93 | } |
| 98 | 94 | ||
| 99 | static TString* LoadString (lua_State* L, ZIO* Z, int swap) | 95 | static TString* LoadString (LoadState* S) |
| 100 | { | 96 | { |
| 101 | size_t size=LoadSize(L,Z,swap); | 97 | size_t size=LoadSize(S); |
| 102 | if (size==0) | 98 | if (size==0) |
| 103 | return NULL; | 99 | return NULL; |
| 104 | else | 100 | else |
| 105 | { | 101 | { |
| 106 | char* s=luaO_openspace(L,size,char); | 102 | char* s=luaO_openspace(S->L,size,char); |
| 107 | LoadBlock(L,s,size,Z,0); | 103 | ezread(S,s,size); |
| 108 | return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */ | 104 | return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ |
| 109 | } | 105 | } |
| 110 | } | 106 | } |
| 111 | 107 | ||
| 112 | static void LoadCode (lua_State* L, Proto* f, ZIO* Z, int swap) | 108 | static void LoadCode (LoadState* S, Proto* f) |
| 113 | { | 109 | { |
| 114 | int size=LoadInt(L,Z,swap); | 110 | int size=LoadInt(S); |
| 115 | f->code=luaM_newvector(L,size,Instruction); | 111 | f->code=luaM_newvector(S->L,size,Instruction); |
| 116 | f->sizecode=size; | 112 | f->sizecode=size; |
| 117 | LoadVector(L,f->code,size,sizeof(*f->code),Z,swap); | 113 | LoadVector(S,f->code,size,sizeof(*f->code)); |
| 118 | } | 114 | } |
| 119 | 115 | ||
| 120 | static void LoadLocals (lua_State* L, Proto* f, ZIO* Z, int swap) | 116 | static void LoadLocals (LoadState* S, Proto* f) |
| 121 | { | 117 | { |
| 122 | int i,n; | 118 | int i,n; |
| 123 | n=LoadInt(L,Z,swap); | 119 | n=LoadInt(S); |
| 124 | f->locvars=luaM_newvector(L,n,LocVar); | 120 | f->locvars=luaM_newvector(S->L,n,LocVar); |
| 125 | f->sizelocvars=n; | 121 | f->sizelocvars=n; |
| 126 | for (i=0; i<n; i++) | 122 | for (i=0; i<n; i++) |
| 127 | { | 123 | { |
| 128 | f->locvars[i].varname=LoadString(L,Z,swap); | 124 | f->locvars[i].varname=LoadString(S); |
| 129 | f->locvars[i].startpc=LoadInt(L,Z,swap); | 125 | f->locvars[i].startpc=LoadInt(S); |
| 130 | f->locvars[i].endpc=LoadInt(L,Z,swap); | 126 | f->locvars[i].endpc=LoadInt(S); |
| 131 | } | 127 | } |
| 132 | } | 128 | } |
| 133 | 129 | ||
| 134 | static void LoadLines (lua_State* L, Proto* f, ZIO* Z, int swap) | 130 | static void LoadLines (LoadState* S, Proto* f) |
| 135 | { | 131 | { |
| 136 | int n; | 132 | int n; |
| 137 | n=LoadInt(L,Z,swap); | 133 | n=LoadInt(S); |
| 138 | f->lineinfo=luaM_newvector(L,n,int); | 134 | f->lineinfo=luaM_newvector(S->L,n,int); |
| 139 | LoadVector(L,f->lineinfo,n,sizeof(*f->lineinfo),Z,swap); | 135 | LoadVector(S,f->lineinfo,n,sizeof(*f->lineinfo)); |
| 140 | } | 136 | } |
| 141 | 137 | ||
| 142 | static Proto* LoadFunction (lua_State* L, TString* p, ZIO* Z, int swap); | 138 | static Proto* LoadFunction (LoadState* S, TString* p); |
| 143 | 139 | ||
| 144 | static void LoadConstants (lua_State* L, Proto* f, ZIO* Z, int swap) | 140 | static void LoadConstants (LoadState* S, Proto* f) |
| 145 | { | 141 | { |
| 146 | int i,n; | 142 | int i,n; |
| 147 | n=LoadInt(L,Z,swap); | 143 | n=LoadInt(S); |
| 148 | f->k=luaM_newvector(L,n,TObject); | 144 | f->k=luaM_newvector(S->L,n,TObject); |
| 149 | f->sizek=n; | 145 | f->sizek=n; |
| 150 | for (i=0; i<n; i++) | 146 | for (i=0; i<n; i++) |
| 151 | { | 147 | { |
| 152 | TObject* o=&f->k[i]; | 148 | TObject* o=&f->k[i]; |
| 153 | int t=LoadByte(L,Z); | 149 | int t=LoadByte(S); |
| 154 | switch (t) | 150 | switch (t) |
| 155 | { | 151 | { |
| 156 | case LUA_TNUMBER: | 152 | case LUA_TNUMBER: |
| 157 | setnvalue(o,LoadNumber(L,Z,swap)); | 153 | setnvalue(o,LoadNumber(S)); |
| 158 | break; | 154 | break; |
| 159 | case LUA_TSTRING: | 155 | case LUA_TSTRING: |
| 160 | setsvalue(o,LoadString(L,Z,swap)); | 156 | setsvalue(o,LoadString(S)); |
| 161 | break; | 157 | break; |
| 162 | case LUA_TNIL: | 158 | case LUA_TNIL: |
| 163 | setnilvalue(o); | 159 | setnilvalue(o); |
| 164 | break; | 160 | break; |
| 165 | default: | 161 | default: |
| 166 | luaG_runerror(L,"bad constant type (%d) in %s",t,ZNAME(Z)); | 162 | luaG_runerror(S->L,"bad constant type (%d) in %s",t,S->name); |
| 167 | break; | 163 | break; |
| 168 | } | 164 | } |
| 169 | } | 165 | } |
| 170 | n=LoadInt(L,Z,swap); | 166 | n=LoadInt(S); |
| 171 | f->p=luaM_newvector(L,n,Proto*); | 167 | f->p=luaM_newvector(S->L,n,Proto*); |
| 172 | f->sizep=n; | 168 | f->sizep=n; |
| 173 | for (i=0; i<n; i++) f->p[i]=LoadFunction(L,f->source,Z,swap); | 169 | for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source); |
| 174 | } | 170 | } |
| 175 | 171 | ||
| 176 | static Proto* LoadFunction (lua_State* L, TString* p, ZIO* Z, int swap) | 172 | static Proto* LoadFunction (LoadState* S, TString* p) |
| 177 | { | 173 | { |
| 178 | Proto* f=luaF_newproto(L); | 174 | Proto* f=luaF_newproto(S->L); |
| 179 | f->source=LoadString(L,Z,swap); if (f->source==NULL) f->source=p; | 175 | f->source=LoadString(S); if (f->source==NULL) f->source=p; |
| 180 | f->lineDefined=LoadInt(L,Z,swap); | 176 | f->lineDefined=LoadInt(S); |
| 181 | f->nupvalues=LoadByte(L,Z); | 177 | f->nupvalues=LoadByte(S); |
| 182 | f->numparams=LoadByte(L,Z); | 178 | f->numparams=LoadByte(S); |
| 183 | f->is_vararg=LoadByte(L,Z); | 179 | f->is_vararg=LoadByte(S); |
| 184 | f->maxstacksize=LoadByte(L,Z); | 180 | f->maxstacksize=LoadByte(S); |
| 185 | LoadLocals(L,f,Z,swap); | 181 | LoadLocals(S,f); |
| 186 | LoadLines(L,f,Z,swap); | 182 | LoadLines(S,f); |
| 187 | LoadConstants(L,f,Z,swap); | 183 | LoadConstants(S,f); |
| 188 | LoadCode(L,f,Z,swap); | 184 | LoadCode(S,f); |
| 189 | #ifndef TRUST_BINARIES | 185 | #ifndef TRUST_BINARIES |
| 190 | if (!luaG_checkcode(f)) luaG_runerror(L,"bad code in %s",ZNAME(Z)); | 186 | if (!luaG_checkcode(f)) luaG_runerror(S->L,"bad code in %s",S->name); |
| 191 | #endif | 187 | #endif |
| 192 | return f; | 188 | return f; |
| 193 | } | 189 | } |
| 194 | 190 | ||
| 195 | static void LoadSignature (lua_State* L, ZIO* Z) | 191 | static void LoadSignature (LoadState* S) |
| 196 | { | 192 | { |
| 197 | const char* s=LUA_SIGNATURE; | 193 | const char* s=LUA_SIGNATURE; |
| 198 | while (*s!=0 && ezgetc(L,Z)==*s) | 194 | while (*s!=0 && ezgetc(S)==*s) |
| 199 | ++s; | 195 | ++s; |
| 200 | if (*s!=0) luaG_runerror(L,"bad signature in %s",ZNAME(Z)); | 196 | if (*s!=0) luaG_runerror(S->L,"bad signature in %s",S->name); |
| 201 | } | 197 | } |
| 202 | 198 | ||
| 203 | static void TestSize (lua_State* L, int s, const char* what, ZIO* Z) | 199 | static void TestSize (LoadState* S, int s, const char* what) |
| 204 | { | 200 | { |
| 205 | int r=LoadByte(L,Z); | 201 | int r=LoadByte(S); |
| 206 | if (r!=s) | 202 | if (r!=s) |
| 207 | luaG_runerror(L,"virtual machine mismatch in %s: " | 203 | luaG_runerror(S->L,"virtual machine mismatch in %s: " |
| 208 | "size of %s is %d but read %d",ZNAME(Z),what,s,r); | 204 | "size of %s is %d but read %d",S->name,what,s,r); |
| 209 | } | 205 | } |
| 210 | 206 | ||
| 211 | #define TESTSIZE(s,w) TestSize(L,s,w,Z) | 207 | #define TESTSIZE(s,w) TestSize(S,s,w) |
| 212 | #define V(v) v/16,v%16 | 208 | #define V(v) v/16,v%16 |
| 213 | 209 | ||
| 214 | static int LoadHeader (lua_State* L, ZIO* Z) | 210 | static void LoadHeader (LoadState* S) |
| 215 | { | 211 | { |
| 216 | int version,swap; | 212 | int version; |
| 217 | lua_Number x=0,tx=TEST_NUMBER; | 213 | lua_Number x=0,tx=TEST_NUMBER; |
| 218 | LoadSignature(L,Z); | 214 | LoadSignature(S); |
| 219 | version=LoadByte(L,Z); | 215 | version=LoadByte(S); |
| 220 | if (version>VERSION) | 216 | if (version>VERSION) |
| 221 | luaG_runerror(L,"%s too new: " | 217 | luaG_runerror(S->L,"%s too new: " |
| 222 | "read version %d.%d; expected at most %d.%d", | 218 | "read version %d.%d; expected at most %d.%d", |
| 223 | ZNAME(Z),V(version),V(VERSION)); | 219 | S->name,V(version),V(VERSION)); |
| 224 | if (version<VERSION0) /* check last major change */ | 220 | if (version<VERSION0) /* check last major change */ |
| 225 | luaG_runerror(L,"%s too old: " | 221 | luaG_runerror(S->L,"%s too old: " |
| 226 | "read version %d.%d; expected at least %d.%d", | 222 | "read version %d.%d; expected at least %d.%d", |
| 227 | ZNAME(Z),V(version),V(VERSION0)); | 223 | S->name,V(version),V(VERSION0)); |
| 228 | swap=(luaU_endianness()!=LoadByte(L,Z)); /* need to swap bytes? */ | 224 | S->swap=(luaU_endianness()!=LoadByte(S)); /* need to swap bytes? */ |
| 229 | TESTSIZE(sizeof(int),"int"); | 225 | TESTSIZE(sizeof(int),"int"); |
| 230 | TESTSIZE(sizeof(size_t), "size_t"); | 226 | TESTSIZE(sizeof(size_t), "size_t"); |
| 231 | TESTSIZE(sizeof(Instruction), "Instruction"); | 227 | TESTSIZE(sizeof(Instruction), "Instruction"); |
| @@ -234,16 +230,16 @@ static int LoadHeader (lua_State* L, ZIO* Z) | |||
| 234 | TESTSIZE(SIZE_B, "B"); | 230 | TESTSIZE(SIZE_B, "B"); |
| 235 | TESTSIZE(SIZE_C, "C"); | 231 | TESTSIZE(SIZE_C, "C"); |
| 236 | TESTSIZE(sizeof(lua_Number), "number"); | 232 | TESTSIZE(sizeof(lua_Number), "number"); |
| 237 | x=LoadNumber(L,Z,swap); | 233 | x=LoadNumber(S); |
| 238 | if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */ | 234 | if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */ |
| 239 | luaG_runerror(L,"unknown number format in %s: read %f; expected %f", | 235 | luaG_runerror(S->L,"unknown number format in %s: read %f; expected %f", |
| 240 | ZNAME(Z),x,tx); | 236 | S->name,x,tx); |
| 241 | return swap; | ||
| 242 | } | 237 | } |
| 243 | 238 | ||
| 244 | static Proto* LoadChunk (lua_State* L, ZIO* Z) | 239 | static Proto* LoadChunk (LoadState* S) |
| 245 | { | 240 | { |
| 246 | return LoadFunction(L,NULL,Z,LoadHeader(L,Z)); | 241 | LoadHeader(S); |
| 242 | return LoadFunction(S,NULL); | ||
| 247 | } | 243 | } |
| 248 | 244 | ||
| 249 | /* | 245 | /* |
| @@ -251,9 +247,20 @@ static Proto* LoadChunk (lua_State* L, ZIO* Z) | |||
| 251 | */ | 247 | */ |
| 252 | Proto* luaU_undump (lua_State* L, ZIO* Z) | 248 | Proto* luaU_undump (lua_State* L, ZIO* Z) |
| 253 | { | 249 | { |
| 254 | Proto* f=LoadChunk(L,Z); | 250 | LoadState S; |
| 251 | Proto* f; | ||
| 252 | const char* s=zname(Z); | ||
| 253 | if (*s=='@' || *s=='=') | ||
| 254 | S.name=s+1; | ||
| 255 | else if (*s==LUA_SIGNATURE[0]) | ||
| 256 | S.name="binary string"; | ||
| 257 | else | ||
| 258 | S.name=s; | ||
| 259 | S.L=L; | ||
| 260 | S.Z=Z; | ||
| 261 | f=LoadChunk(&S); | ||
| 255 | if (zgetc(Z)!=EOZ) | 262 | if (zgetc(Z)!=EOZ) |
| 256 | luaG_runerror(L,"%s apparently contains more than one chunk",ZNAME(Z)); | 263 | luaG_runerror(L,"%s apparently contains more than one chunk",S.name); |
| 257 | return f; | 264 | return f; |
| 258 | } | 265 | } |
| 259 | 266 | ||
