diff options
-rw-r--r-- | ldump.c | 278 | ||||
-rw-r--r-- | lundump.c | 429 |
2 files changed, 363 insertions, 344 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldump.c,v 2.23 2014/02/28 16:13:01 roberto Exp roberto $ | 2 | ** $Id: ldump.c,v 2.24 2014/03/01 15:18:44 roberto Exp roberto $ |
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 | */ |
@@ -15,176 +15,180 @@ | |||
15 | #include "lstate.h" | 15 | #include "lstate.h" |
16 | #include "lundump.h" | 16 | #include "lundump.h" |
17 | 17 | ||
18 | |||
18 | typedef struct { | 19 | typedef struct { |
19 | lua_State* L; | 20 | lua_State *L; |
20 | lua_Writer writer; | 21 | lua_Writer writer; |
21 | void* data; | 22 | void *data; |
22 | int strip; | 23 | int strip; |
23 | int status; | 24 | int status; |
24 | } DumpState; | 25 | } DumpState; |
25 | 26 | ||
26 | 27 | ||
27 | #define DumpVar(x,D) DumpBlock(&x,sizeof(x),D) | 28 | #define DumpVar(x,D) DumpBlock(&x,sizeof(x),D) |
28 | #define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D) | 29 | #define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D) |
29 | 30 | ||
30 | static void DumpBlock(const void* b, size_t size, DumpState* D) | 31 | |
31 | { | 32 | static void DumpBlock (const void *b, size_t size, DumpState *D) { |
32 | if (D->status==0) | 33 | if (D->status == 0) { |
33 | { | 34 | lua_unlock(D->L); |
34 | lua_unlock(D->L); | 35 | D->status = (*D->writer)(D->L, b, size, D->data); |
35 | D->status=(*D->writer)(D->L,b,size,D->data); | 36 | lua_lock(D->L); |
36 | lua_lock(D->L); | 37 | } |
37 | } | ||
38 | } | 38 | } |
39 | 39 | ||
40 | static void DumpByte(int y, DumpState* D) | 40 | |
41 | { | 41 | static void DumpByte (int y, DumpState *D) { |
42 | lu_byte x=(lu_byte)y; | 42 | lu_byte x = (lu_byte)y; |
43 | DumpVar(x,D); | 43 | DumpVar(x, D); |
44 | } | 44 | } |
45 | 45 | ||
46 | static void DumpInt(int x, DumpState* D) | 46 | |
47 | { | 47 | static void DumpInt (int x, DumpState *D) { |
48 | DumpVar(x,D); | 48 | DumpVar(x, D); |
49 | } | 49 | } |
50 | 50 | ||
51 | static void DumpNumber(lua_Number x, DumpState* D) | 51 | |
52 | { | 52 | static void DumpNumber (lua_Number x, DumpState *D) { |
53 | DumpVar(x,D); | 53 | DumpVar(x, D); |
54 | } | 54 | } |
55 | 55 | ||
56 | static void DumpInteger(lua_Integer x, DumpState* D) | 56 | |
57 | { | 57 | static void DumpInteger (lua_Integer x, DumpState *D) { |
58 | DumpVar(x,D); | 58 | DumpVar(x, D); |
59 | } | 59 | } |
60 | 60 | ||
61 | static void DumpString(const TString* s, DumpState* D) | 61 | |
62 | { | 62 | static void DumpString (const TString *s, DumpState *D) { |
63 | if (s==NULL) DumpByte(0,D); | 63 | if (s == NULL) |
64 | else { | 64 | DumpByte(0, D); |
65 | size_t size = s->tsv.len+1; /* include trailing '\0' */ | 65 | else { |
66 | if (size < 0xFF) DumpByte(size,D); | 66 | size_t size = s->tsv.len + 1; /* include trailing '\0' */ |
67 | else | 67 | if (size < 0xFF) |
68 | { | 68 | DumpByte(size, D); |
69 | DumpByte(0xFF,D); | 69 | else { |
70 | DumpVar(size,D); | 70 | DumpByte(0xFF, D); |
71 | DumpVar(size, D); | ||
72 | } | ||
73 | DumpVector(getstr(s), size - 1, D); /* no need to save '\0' */ | ||
71 | } | 74 | } |
72 | DumpVector(getstr(s),size-1,D); /* no need to save '\0' */ | ||
73 | } | ||
74 | } | 75 | } |
75 | 76 | ||
76 | static void DumpCode(const Proto* f, DumpState* D) | 77 | |
77 | { | 78 | static void DumpCode (const Proto *f, DumpState *D) { |
78 | DumpInt(f->sizecode,D); | 79 | DumpInt(f->sizecode, D); |
79 | DumpVector(f->code,f->sizecode,D); | 80 | DumpVector(f->code, f->sizecode, D); |
80 | } | 81 | } |
81 | 82 | ||
82 | static void DumpFunction(const Proto* f, DumpState* D); | 83 | |
83 | 84 | static void DumpFunction(const Proto *f, DumpState *D); | |
84 | static void DumpConstants(const Proto* f, DumpState* D) | 85 | |
85 | { | 86 | static void DumpConstants (const Proto *f, DumpState *D) { |
86 | int i,n=f->sizek; | 87 | int i; |
87 | DumpInt(n,D); | 88 | int n = f->sizek; |
88 | for (i=0; i<n; i++) | 89 | DumpInt(n, D); |
89 | { | 90 | for (i = 0; i < n; i++) { |
90 | const TValue* o=&f->k[i]; | 91 | const TValue *o = &f->k[i]; |
91 | DumpByte(ttype(o),D); | 92 | DumpByte(ttype(o), D); |
92 | switch (ttype(o)) | 93 | switch (ttype(o)) { |
93 | { | 94 | case LUA_TNIL: |
94 | case LUA_TNIL: | 95 | break; |
95 | break; | 96 | case LUA_TBOOLEAN: |
96 | case LUA_TBOOLEAN: | 97 | DumpByte(bvalue(o), D); |
97 | DumpByte(bvalue(o),D); | 98 | break; |
98 | break; | 99 | case LUA_TNUMFLT: |
99 | case LUA_TNUMFLT: | 100 | DumpNumber(fltvalue(o), D); |
100 | DumpNumber(fltvalue(o),D); | 101 | break; |
101 | break; | 102 | case LUA_TNUMINT: |
102 | case LUA_TNUMINT: | 103 | DumpInteger(ivalue(o), D); |
103 | DumpInteger(ivalue(o),D); | 104 | break; |
104 | break; | 105 | case LUA_TSHRSTR: |
105 | case LUA_TSHRSTR: case LUA_TLNGSTR: | 106 | case LUA_TLNGSTR: |
106 | DumpString(rawtsvalue(o),D); | 107 | DumpString(rawtsvalue(o), D); |
107 | break; | 108 | break; |
108 | default: lua_assert(0); | 109 | default: |
110 | lua_assert(0); | ||
111 | } | ||
109 | } | 112 | } |
110 | } | 113 | n = f->sizep; |
111 | n=f->sizep; | 114 | DumpInt(n, D); |
112 | DumpInt(n,D); | 115 | for (i = 0; i < n; i++) |
113 | for (i=0; i<n; i++) DumpFunction(f->p[i],D); | 116 | DumpFunction(f->p[i], D); |
114 | } | 117 | } |
115 | 118 | ||
116 | static void DumpUpvalues(const Proto* f, DumpState* D) | 119 | |
117 | { | 120 | static void DumpUpvalues (const Proto *f, DumpState *D) { |
118 | int i,n=f->sizeupvalues; | 121 | int i, n = f->sizeupvalues; |
119 | DumpInt(n,D); | 122 | DumpInt(n, D); |
120 | for (i=0; i<n; i++) | 123 | for (i = 0; i < n; i++) { |
121 | { | 124 | DumpByte(f->upvalues[i].instack, D); |
122 | DumpByte(f->upvalues[i].instack,D); | 125 | DumpByte(f->upvalues[i].idx, D); |
123 | DumpByte(f->upvalues[i].idx,D); | 126 | } |
124 | } | ||
125 | } | 127 | } |
126 | 128 | ||
127 | static void DumpDebug(const Proto* f, DumpState* D) | 129 | |
128 | { | 130 | static void DumpDebug (const Proto *f, DumpState *D) { |
129 | int i,n; | 131 | int i, n; |
130 | DumpString((D->strip) ? NULL : f->source,D); | 132 | DumpString((D->strip) ? NULL : f->source, D); |
131 | n= (D->strip) ? 0 : f->sizelineinfo; | 133 | n = (D->strip) ? 0 : f->sizelineinfo; |
132 | DumpInt(n,D); | 134 | DumpInt(n, D); |
133 | DumpVector(f->lineinfo,n,D); | 135 | DumpVector(f->lineinfo, n, D); |
134 | n= (D->strip) ? 0 : f->sizelocvars; | 136 | n = (D->strip) ? 0 : f->sizelocvars; |
135 | DumpInt(n,D); | 137 | DumpInt(n, D); |
136 | for (i=0; i<n; i++) | 138 | for (i = 0; i < n; i++) { |
137 | { | 139 | DumpString(f->locvars[i].varname, D); |
138 | DumpString(f->locvars[i].varname,D); | 140 | DumpInt(f->locvars[i].startpc, D); |
139 | DumpInt(f->locvars[i].startpc,D); | 141 | DumpInt(f->locvars[i].endpc, D); |
140 | DumpInt(f->locvars[i].endpc,D); | 142 | } |
141 | } | 143 | n = (D->strip) ? 0 : f->sizeupvalues; |
142 | n= (D->strip) ? 0 : f->sizeupvalues; | 144 | DumpInt(n, D); |
143 | DumpInt(n,D); | 145 | for (i = 0; i < n; i++) |
144 | for (i=0; i<n; i++) DumpString(f->upvalues[i].name,D); | 146 | DumpString(f->upvalues[i].name, D); |
145 | } | 147 | } |
146 | 148 | ||
147 | static void DumpFunction(const Proto* f, DumpState* D) | 149 | |
148 | { | 150 | static void DumpFunction (const Proto *f, DumpState *D) { |
149 | DumpInt(f->linedefined,D); | 151 | DumpInt(f->linedefined, D); |
150 | DumpInt(f->lastlinedefined,D); | 152 | DumpInt(f->lastlinedefined, D); |
151 | DumpByte(f->numparams,D); | 153 | DumpByte(f->numparams, D); |
152 | DumpByte(f->is_vararg,D); | 154 | DumpByte(f->is_vararg, D); |
153 | DumpByte(f->maxstacksize,D); | 155 | DumpByte(f->maxstacksize, D); |
154 | DumpCode(f,D); | 156 | DumpCode(f, D); |
155 | DumpConstants(f,D); | 157 | DumpConstants(f, D); |
156 | DumpUpvalues(f,D); | 158 | DumpUpvalues(f, D); |
157 | DumpDebug(f,D); | 159 | DumpDebug(f, D); |
158 | } | 160 | } |
159 | 161 | ||
160 | static void DumpHeader(DumpState* D) | 162 | |
161 | { | 163 | static void DumpHeader (DumpState *D) { |
162 | DumpBlock(LUA_SIGNATURE,sizeof(LUA_SIGNATURE),D); | 164 | DumpBlock(LUA_SIGNATURE, sizeof(LUA_SIGNATURE), D); |
163 | DumpBlock(LUAC_DATA,sizeof(LUAC_DATA),D); | 165 | DumpBlock(LUAC_DATA, sizeof(LUAC_DATA), D); |
164 | DumpByte(LUAC_VERSION,D); | 166 | DumpByte(LUAC_VERSION, D); |
165 | DumpByte(LUAC_FORMAT,D); | 167 | DumpByte(LUAC_FORMAT, D); |
166 | DumpByte(sizeof(int),D); | 168 | DumpByte(sizeof(int), D); |
167 | DumpByte(sizeof(size_t),D); | 169 | DumpByte(sizeof(size_t), D); |
168 | DumpByte(sizeof(Instruction),D); | 170 | DumpByte(sizeof(Instruction), D); |
169 | DumpByte(sizeof(lua_Integer),D); | 171 | DumpByte(sizeof(lua_Integer), D); |
170 | DumpByte(sizeof(lua_Number),D); | 172 | DumpByte(sizeof(lua_Number), D); |
171 | DumpInteger(LUAC_INT,D); | 173 | DumpInteger(LUAC_INT, D); |
172 | DumpNumber(LUAC_NUM,D); | 174 | DumpNumber(LUAC_NUM, D); |
173 | } | 175 | } |
174 | 176 | ||
177 | |||
175 | /* | 178 | /* |
176 | ** dump Lua function as precompiled chunk | 179 | ** dump Lua function as precompiled chunk |
177 | */ | 180 | */ |
178 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) | 181 | int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, |
179 | { | 182 | int strip) { |
180 | DumpState D; | 183 | DumpState D; |
181 | D.L=L; | 184 | D.L = L; |
182 | D.writer=w; | 185 | D.writer = w; |
183 | D.data=data; | 186 | D.data = data; |
184 | D.strip=strip; | 187 | D.strip = strip; |
185 | D.status=0; | 188 | D.status = 0; |
186 | DumpHeader(&D); | 189 | DumpHeader(&D); |
187 | DumpByte(f->sizeupvalues,&D); | 190 | DumpByte(f->sizeupvalues, &D); |
188 | DumpFunction(f,&D); | 191 | DumpFunction(f, &D); |
189 | return D.status; | 192 | return D.status; |
190 | } | 193 | } |
194 | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lundump.c,v 2.29 2014/02/28 16:13:01 roberto Exp roberto $ | 2 | ** $Id: lundump.c,v 2.30 2014/03/01 15:18:44 roberto Exp roberto $ |
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 | */ |
@@ -20,227 +20,242 @@ | |||
20 | #include "lundump.h" | 20 | #include "lundump.h" |
21 | #include "lzio.h" | 21 | #include "lzio.h" |
22 | 22 | ||
23 | typedef struct { | ||
24 | lua_State* L; | ||
25 | ZIO* Z; | ||
26 | Mbuffer* b; | ||
27 | const char* name; | ||
28 | } LoadState; | ||
29 | |||
30 | static l_noret error(LoadState* S, const char* why) | ||
31 | { | ||
32 | luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why); | ||
33 | luaD_throw(S->L,LUA_ERRSYNTAX); | ||
34 | } | ||
35 | 23 | ||
36 | #define LoadVar(S,x) LoadBlock(S,&x,sizeof(x)) | 24 | #define LoadVar(S,x) LoadBlock(S,&x,sizeof(x)) |
37 | #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0])) | 25 | #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0])) |
38 | 26 | ||
39 | #if !defined(luai_verifycode) | 27 | #if !defined(luai_verifycode) |
40 | #define luai_verifycode(L,b,f) /* empty */ | 28 | #define luai_verifycode(L,b,f) /* empty */ |
41 | #endif | 29 | #endif |
42 | 30 | ||
43 | static void LoadBlock(LoadState* S, void* b, size_t size) | 31 | |
44 | { | 32 | typedef struct { |
45 | if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated"); | 33 | lua_State *L; |
46 | } | 34 | ZIO *Z; |
47 | 35 | Mbuffer *b; | |
48 | static lu_byte LoadByte(LoadState* S) | 36 | const char *name; |
49 | { | 37 | } LoadState; |
50 | lu_byte x; | 38 | |
51 | LoadVar(S,x); | 39 | |
52 | return x; | 40 | static l_noret error(LoadState *S, const char *why) { |
53 | } | 41 | luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); |
54 | 42 | luaD_throw(S->L, LUA_ERRSYNTAX); | |
55 | static int LoadInt(LoadState* S) | 43 | } |
56 | { | 44 | |
57 | int x; | 45 | |
58 | LoadVar(S,x); | 46 | static void LoadBlock (LoadState *S, void *b, size_t size) { |
59 | if (x<0) error(S,"corrupted"); | 47 | if (luaZ_read(S->Z, b, size) != 0) |
60 | return x; | 48 | error(S, "truncated"); |
61 | } | 49 | } |
62 | 50 | ||
63 | static lua_Number LoadNumber(LoadState* S) | 51 | |
64 | { | 52 | static lu_byte LoadByte (LoadState *S) { |
65 | lua_Number x; | 53 | lu_byte x; |
66 | LoadVar(S,x); | 54 | LoadVar(S, x); |
67 | return x; | 55 | return x; |
68 | } | 56 | } |
69 | 57 | ||
70 | static lua_Integer LoadInteger(LoadState* S) | 58 | |
71 | { | 59 | static int LoadInt (LoadState *S) { |
72 | lua_Integer x; | 60 | int x; |
73 | LoadVar(S,x); | 61 | LoadVar(S, x); |
74 | return x; | 62 | if (x < 0) |
75 | } | 63 | error(S, "corrupted"); |
76 | 64 | return x; | |
77 | static TString* LoadString(LoadState* S) | 65 | } |
78 | { | 66 | |
79 | size_t size = LoadByte(S); | 67 | |
80 | if (size == 0xFF) LoadVar(S,size); | 68 | static lua_Number LoadNumber (LoadState *S) { |
81 | if (size==0) | 69 | lua_Number x; |
82 | return NULL; | 70 | LoadVar(S, x); |
83 | else | 71 | return x; |
84 | { | 72 | } |
85 | char* s=luaZ_openspace(S->L,S->b,--size); | 73 | |
86 | LoadVector(S,s,size); | 74 | |
87 | return luaS_newlstr(S->L,s,size); | 75 | static lua_Integer LoadInteger (LoadState *S) { |
88 | } | 76 | lua_Integer x; |
89 | } | 77 | LoadVar(S, x); |
90 | 78 | return x; | |
91 | static void LoadCode(LoadState* S, Proto* f) | 79 | } |
92 | { | 80 | |
93 | int n=LoadInt(S); | 81 | |
94 | f->code=luaM_newvector(S->L,n,Instruction); | 82 | static TString *LoadString (LoadState *S) { |
95 | f->sizecode=n; | 83 | size_t size = LoadByte(S); |
96 | LoadVector(S,f->code,n); | 84 | if (size == 0xFF) |
97 | } | 85 | LoadVar(S, size); |
98 | 86 | if (size == 0) | |
99 | static void LoadFunction(LoadState* S, Proto* f); | 87 | return NULL; |
100 | 88 | else { | |
101 | static void LoadConstants(LoadState* S, Proto* f) | 89 | char *s = luaZ_openspace(S->L, S->b, --size); |
102 | { | 90 | LoadVector(S, s, size); |
103 | int i,n; | 91 | return luaS_newlstr(S->L, s, size); |
104 | n=LoadInt(S); | ||
105 | f->k=luaM_newvector(S->L,n,TValue); | ||
106 | f->sizek=n; | ||
107 | for (i=0; i<n; i++) setnilvalue(&f->k[i]); | ||
108 | for (i=0; i<n; i++) | ||
109 | { | ||
110 | TValue* o=&f->k[i]; | ||
111 | int t=LoadByte(S); | ||
112 | switch (t) | ||
113 | { | ||
114 | case LUA_TNIL: | ||
115 | setnilvalue(o); | ||
116 | break; | ||
117 | case LUA_TBOOLEAN: | ||
118 | setbvalue(o,LoadByte(S)); | ||
119 | break; | ||
120 | case LUA_TNUMFLT: | ||
121 | setnvalue(o,LoadNumber(S)); | ||
122 | break; | ||
123 | case LUA_TNUMINT: | ||
124 | setivalue(o,LoadInteger(S)); | ||
125 | break; | ||
126 | case LUA_TSHRSTR: case LUA_TLNGSTR: | ||
127 | setsvalue2n(S->L,o,LoadString(S)); | ||
128 | break; | ||
129 | default: lua_assert(0); | ||
130 | } | 92 | } |
131 | } | ||
132 | n=LoadInt(S); | ||
133 | f->p=luaM_newvector(S->L,n,Proto*); | ||
134 | f->sizep=n; | ||
135 | for (i=0; i<n; i++) f->p[i]=NULL; | ||
136 | for (i=0; i<n; i++) | ||
137 | { | ||
138 | f->p[i]=luaF_newproto(S->L); | ||
139 | LoadFunction(S,f->p[i]); | ||
140 | } | ||
141 | } | ||
142 | |||
143 | static void LoadUpvalues(LoadState* S, Proto* f) | ||
144 | { | ||
145 | int i,n; | ||
146 | n=LoadInt(S); | ||
147 | f->upvalues=luaM_newvector(S->L,n,Upvaldesc); | ||
148 | f->sizeupvalues=n; | ||
149 | for (i=0; i<n; i++) f->upvalues[i].name=NULL; | ||
150 | for (i=0; i<n; i++) | ||
151 | { | ||
152 | f->upvalues[i].instack=LoadByte(S); | ||
153 | f->upvalues[i].idx=LoadByte(S); | ||
154 | } | ||
155 | } | ||
156 | |||
157 | static void LoadDebug(LoadState* S, Proto* f) | ||
158 | { | ||
159 | int i,n; | ||
160 | f->source=LoadString(S); | ||
161 | n=LoadInt(S); | ||
162 | f->lineinfo=luaM_newvector(S->L,n,int); | ||
163 | f->sizelineinfo=n; | ||
164 | LoadVector(S,f->lineinfo,n); | ||
165 | n=LoadInt(S); | ||
166 | f->locvars=luaM_newvector(S->L,n,LocVar); | ||
167 | f->sizelocvars=n; | ||
168 | for (i=0; i<n; i++) f->locvars[i].varname=NULL; | ||
169 | for (i=0; i<n; i++) | ||
170 | { | ||
171 | f->locvars[i].varname=LoadString(S); | ||
172 | f->locvars[i].startpc=LoadInt(S); | ||
173 | f->locvars[i].endpc=LoadInt(S); | ||
174 | } | ||
175 | n=LoadInt(S); | ||
176 | for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S); | ||
177 | } | ||
178 | |||
179 | static void LoadFunction(LoadState* S, Proto* f) | ||
180 | { | ||
181 | f->linedefined=LoadInt(S); | ||
182 | f->lastlinedefined=LoadInt(S); | ||
183 | f->numparams=LoadByte(S); | ||
184 | f->is_vararg=LoadByte(S); | ||
185 | f->maxstacksize=LoadByte(S); | ||
186 | LoadCode(S,f); | ||
187 | LoadConstants(S,f); | ||
188 | LoadUpvalues(S,f); | ||
189 | LoadDebug(S,f); | ||
190 | } | ||
191 | |||
192 | static void checkstring(LoadState *S, const char *s, const char *msg) | ||
193 | { | ||
194 | char buff[sizeof(LUA_SIGNATURE)+sizeof(LUAC_DATA)]; /* larger than each */ | ||
195 | LoadVector(S,buff,strlen(s)+1); | ||
196 | if (strcmp(s,buff)!=0) error(S,msg); | ||
197 | } | ||
198 | |||
199 | static void fchecksize(LoadState *S, size_t size, const char *tname) | ||
200 | { | ||
201 | if (LoadByte(S) != size) | ||
202 | error(S,luaO_pushfstring(S->L,"%s size mismatch in",tname)); | ||
203 | } | 93 | } |
204 | 94 | ||
95 | |||
96 | static void LoadCode (LoadState *S, Proto *f) { | ||
97 | int n = LoadInt(S); | ||
98 | f->code = luaM_newvector(S->L, n, Instruction); | ||
99 | f->sizecode = n; | ||
100 | LoadVector(S, f->code, n); | ||
101 | } | ||
102 | |||
103 | |||
104 | static void LoadFunction(LoadState *S, Proto *f); | ||
105 | |||
106 | |||
107 | static void LoadConstants (LoadState *S, Proto *f) { | ||
108 | int i, n; | ||
109 | n = LoadInt(S); | ||
110 | f->k = luaM_newvector(S->L, n, TValue); | ||
111 | f->sizek = n; | ||
112 | for (i = 0; i < n; i++) | ||
113 | setnilvalue(&f->k[i]); | ||
114 | for (i = 0; i < n; i++) { | ||
115 | TValue *o = &f->k[i]; | ||
116 | int t = LoadByte(S); | ||
117 | switch (t) { | ||
118 | case LUA_TNIL: | ||
119 | setnilvalue(o); | ||
120 | break; | ||
121 | case LUA_TBOOLEAN: | ||
122 | setbvalue(o, LoadByte(S)); | ||
123 | break; | ||
124 | case LUA_TNUMFLT: | ||
125 | setnvalue(o, LoadNumber(S)); | ||
126 | break; | ||
127 | case LUA_TNUMINT: | ||
128 | setivalue(o, LoadInteger(S)); | ||
129 | break; | ||
130 | case LUA_TSHRSTR: | ||
131 | case LUA_TLNGSTR: | ||
132 | setsvalue2n(S->L, o, LoadString(S)); | ||
133 | break; | ||
134 | default: | ||
135 | lua_assert(0); | ||
136 | } | ||
137 | } | ||
138 | n = LoadInt(S); | ||
139 | f->p = luaM_newvector(S->L, n, Proto *); | ||
140 | f->sizep = n; | ||
141 | for (i = 0; i < n; i++) | ||
142 | f->p[i] = NULL; | ||
143 | for (i = 0; i < n; i++) { | ||
144 | f->p[i] = luaF_newproto(S->L); | ||
145 | LoadFunction(S, f->p[i]); | ||
146 | } | ||
147 | } | ||
148 | |||
149 | |||
150 | static void LoadUpvalues (LoadState *S, Proto *f) { | ||
151 | int i, n; | ||
152 | n = LoadInt(S); | ||
153 | f->upvalues = luaM_newvector(S->L, n, Upvaldesc); | ||
154 | f->sizeupvalues = n; | ||
155 | for (i = 0; i < n; i++) | ||
156 | f->upvalues[i].name = NULL; | ||
157 | for (i = 0; i < n; i++) { | ||
158 | f->upvalues[i].instack = LoadByte(S); | ||
159 | f->upvalues[i].idx = LoadByte(S); | ||
160 | } | ||
161 | } | ||
162 | |||
163 | |||
164 | static void LoadDebug (LoadState *S, Proto *f) { | ||
165 | int i, n; | ||
166 | f->source = LoadString(S); | ||
167 | n = LoadInt(S); | ||
168 | f->lineinfo = luaM_newvector(S->L, n, int); | ||
169 | f->sizelineinfo = n; | ||
170 | LoadVector(S, f->lineinfo, n); | ||
171 | n = LoadInt(S); | ||
172 | f->locvars = luaM_newvector(S->L, n, LocVar); | ||
173 | f->sizelocvars = n; | ||
174 | for (i = 0; i < n; i++) | ||
175 | f->locvars[i].varname = NULL; | ||
176 | for (i = 0; i < n; i++) { | ||
177 | f->locvars[i].varname = LoadString(S); | ||
178 | f->locvars[i].startpc = LoadInt(S); | ||
179 | f->locvars[i].endpc = LoadInt(S); | ||
180 | } | ||
181 | n = LoadInt(S); | ||
182 | for (i = 0; i < n; i++) | ||
183 | f->upvalues[i].name = LoadString(S); | ||
184 | } | ||
185 | |||
186 | |||
187 | static void LoadFunction (LoadState *S, Proto *f) { | ||
188 | f->linedefined = LoadInt(S); | ||
189 | f->lastlinedefined = LoadInt(S); | ||
190 | f->numparams = LoadByte(S); | ||
191 | f->is_vararg = LoadByte(S); | ||
192 | f->maxstacksize = LoadByte(S); | ||
193 | LoadCode(S, f); | ||
194 | LoadConstants(S, f); | ||
195 | LoadUpvalues(S, f); | ||
196 | LoadDebug(S, f); | ||
197 | } | ||
198 | |||
199 | |||
200 | static void checkstring (LoadState *S, const char *s, const char *msg) { | ||
201 | char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than each */ | ||
202 | LoadVector(S, buff, strlen(s) + 1); | ||
203 | if (strcmp(s, buff) != 0) | ||
204 | error(S, msg); | ||
205 | } | ||
206 | |||
207 | |||
208 | static void fchecksize (LoadState *S, size_t size, const char *tname) { | ||
209 | if (LoadByte(S) != size) | ||
210 | error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname)); | ||
211 | } | ||
212 | |||
213 | |||
205 | #define checksize(S,t) fchecksize(S,sizeof(t),#t) | 214 | #define checksize(S,t) fchecksize(S,sizeof(t),#t) |
206 | 215 | ||
207 | static void checkHeader(LoadState* S) | 216 | static void checkHeader (LoadState *S) { |
208 | { | 217 | checkstring(S, LUA_SIGNATURE + 1, "not a"); |
209 | checkstring(S,LUA_SIGNATURE+1,"not a"); | 218 | checkstring(S, LUAC_DATA, "corrupted"); |
210 | checkstring(S,LUAC_DATA,"corrupted"); | 219 | if (LoadByte(S) != LUAC_VERSION) |
211 | if (LoadByte(S) != LUAC_VERSION) error(S,"version mismatch in"); | 220 | error(S, "version mismatch in"); |
212 | if (LoadByte(S) != LUAC_FORMAT) error(S,"format mismatch in"); | 221 | if (LoadByte(S) != LUAC_FORMAT) |
213 | checksize(S,int); | 222 | error(S, "format mismatch in"); |
214 | checksize(S,size_t); | 223 | checksize(S, int); |
215 | checksize(S,Instruction); | 224 | checksize(S, size_t); |
216 | checksize(S,lua_Integer); | 225 | checksize(S, Instruction); |
217 | checksize(S,lua_Number); | 226 | checksize(S, lua_Integer); |
218 | if (LoadInteger(S) != LUAC_INT) error(S,"endianess mismatch in"); | 227 | checksize(S, lua_Number); |
219 | if (LoadNumber(S) != LUAC_NUM) error(S,"float format mismatch in"); | 228 | if (LoadInteger(S) != LUAC_INT) |
229 | error(S, "endianess mismatch in"); | ||
230 | if (LoadNumber(S) != LUAC_NUM) | ||
231 | error(S, "float format mismatch in"); | ||
220 | } | 232 | } |
221 | 233 | ||
234 | |||
222 | /* | 235 | /* |
223 | ** load precompiled chunk | 236 | ** load precompiled chunk |
224 | */ | 237 | */ |
225 | Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) | 238 | Closure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, |
226 | { | 239 | const char *name) { |
227 | LoadState S; | 240 | LoadState S; |
228 | Closure* cl; | 241 | Closure *cl; |
229 | if (*name=='@' || *name=='=') | 242 | if (*name == '@' || *name == '=') |
230 | S.name=name+1; | 243 | S.name = name + 1; |
231 | else if (*name==LUA_SIGNATURE[0]) | 244 | else if (*name == LUA_SIGNATURE[0]) |
232 | S.name="binary string"; | 245 | S.name = "binary string"; |
233 | else | 246 | else |
234 | S.name=name; | 247 | S.name = name; |
235 | S.L=L; | 248 | S.L = L; |
236 | S.Z=Z; | 249 | S.Z = Z; |
237 | S.b=buff; | 250 | S.b = buff; |
238 | checkHeader(&S); | 251 | checkHeader(&S); |
239 | cl=luaF_newLclosure(L,LoadByte(&S)); | 252 | cl = luaF_newLclosure(L, LoadByte(&S)); |
240 | setclLvalue(L,L->top,cl); incr_top(L); | 253 | setclLvalue(L, L->top, cl); |
241 | cl->l.p=luaF_newproto(L); | 254 | incr_top(L); |
242 | LoadFunction(&S,cl->l.p); | 255 | cl->l.p = luaF_newproto(L); |
243 | lua_assert(cl->l.nupvalues==cl->l.p->sizeupvalues); | 256 | LoadFunction(&S, cl->l.p); |
244 | luai_verifycode(L,buff,cl->l.p); | 257 | lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues); |
245 | return cl; | 258 | luai_verifycode(L, buff, cl->l.p); |
259 | return cl; | ||
246 | } | 260 | } |
261 | |||