diff options
author | Li Jin <dragon-fly@qq.com> | 2020-06-22 16:50:40 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2020-06-22 16:50:40 +0800 |
commit | cd2b60b101a398cb9356d746364e70eaed1860f1 (patch) | |
tree | a1fe71b76faabc4883f16905a94164ce5c23e692 /src/lua/lundump.c | |
parent | 88c1052e700f38cf3d8ad82d469da4c487760b7e (diff) | |
download | yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.gz yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.bz2 yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.zip |
add support for local variable declared with attribute 'close' and 'const' for Lua 5.4.
Diffstat (limited to 'src/lua/lundump.c')
-rw-r--r-- | src/lua/lundump.c | 323 |
1 files changed, 323 insertions, 0 deletions
diff --git a/src/lua/lundump.c b/src/lua/lundump.c new file mode 100644 index 0000000..4243678 --- /dev/null +++ b/src/lua/lundump.c | |||
@@ -0,0 +1,323 @@ | |||
1 | /* | ||
2 | ** $Id: lundump.c $ | ||
3 | ** load precompiled Lua chunks | ||
4 | ** See Copyright Notice in lua.h | ||
5 | */ | ||
6 | |||
7 | #define lundump_c | ||
8 | #define LUA_CORE | ||
9 | |||
10 | #include "lprefix.h" | ||
11 | |||
12 | |||
13 | #include <limits.h> | ||
14 | #include <string.h> | ||
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 | |||
27 | |||
28 | #if !defined(luai_verifycode) | ||
29 | #define luai_verifycode(L,f) /* empty */ | ||
30 | #endif | ||
31 | |||
32 | |||
33 | typedef struct { | ||
34 | lua_State *L; | ||
35 | ZIO *Z; | ||
36 | const char *name; | ||
37 | } LoadState; | ||
38 | |||
39 | |||
40 | static l_noret error (LoadState *S, const char *why) { | ||
41 | luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why); | ||
42 | luaD_throw(S->L, LUA_ERRSYNTAX); | ||
43 | } | ||
44 | |||
45 | |||
46 | /* | ||
47 | ** All high-level loads go through loadVector; you can change it to | ||
48 | ** adapt to the endianness of the input | ||
49 | */ | ||
50 | #define loadVector(S,b,n) loadBlock(S,b,(n)*sizeof((b)[0])) | ||
51 | |||
52 | static void loadBlock (LoadState *S, void *b, size_t size) { | ||
53 | if (luaZ_read(S->Z, b, size) != 0) | ||
54 | error(S, "truncated chunk"); | ||
55 | } | ||
56 | |||
57 | |||
58 | #define loadVar(S,x) loadVector(S,&x,1) | ||
59 | |||
60 | |||
61 | static lu_byte loadByte (LoadState *S) { | ||
62 | int b = zgetc(S->Z); | ||
63 | if (b == EOZ) | ||
64 | error(S, "truncated chunk"); | ||
65 | return cast_byte(b); | ||
66 | } | ||
67 | |||
68 | |||
69 | static size_t loadUnsigned (LoadState *S, size_t limit) { | ||
70 | size_t x = 0; | ||
71 | int b; | ||
72 | limit >>= 7; | ||
73 | do { | ||
74 | b = loadByte(S); | ||
75 | if (x >= limit) | ||
76 | error(S, "integer overflow"); | ||
77 | x = (x << 7) | (b & 0x7f); | ||
78 | } while ((b & 0x80) == 0); | ||
79 | return x; | ||
80 | } | ||
81 | |||
82 | |||
83 | static size_t loadSize (LoadState *S) { | ||
84 | return loadUnsigned(S, ~(size_t)0); | ||
85 | } | ||
86 | |||
87 | |||
88 | static int loadInt (LoadState *S) { | ||
89 | return cast_int(loadUnsigned(S, INT_MAX)); | ||
90 | } | ||
91 | |||
92 | |||
93 | static lua_Number loadNumber (LoadState *S) { | ||
94 | lua_Number x; | ||
95 | loadVar(S, x); | ||
96 | return x; | ||
97 | } | ||
98 | |||
99 | |||
100 | static lua_Integer loadInteger (LoadState *S) { | ||
101 | lua_Integer x; | ||
102 | loadVar(S, x); | ||
103 | return x; | ||
104 | } | ||
105 | |||
106 | |||
107 | /* | ||
108 | ** Load a nullable string into prototype 'p'. | ||
109 | */ | ||
110 | static TString *loadStringN (LoadState *S, Proto *p) { | ||
111 | lua_State *L = S->L; | ||
112 | TString *ts; | ||
113 | size_t size = loadSize(S); | ||
114 | if (size == 0) /* no string? */ | ||
115 | return NULL; | ||
116 | else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ | ||
117 | char buff[LUAI_MAXSHORTLEN]; | ||
118 | loadVector(S, buff, size); /* load string into buffer */ | ||
119 | ts = luaS_newlstr(L, buff, size); /* create string */ | ||
120 | } | ||
121 | else { /* long string */ | ||
122 | ts = luaS_createlngstrobj(L, size); /* create string */ | ||
123 | loadVector(S, getstr(ts), size); /* load directly in final place */ | ||
124 | } | ||
125 | luaC_objbarrier(L, p, ts); | ||
126 | return ts; | ||
127 | } | ||
128 | |||
129 | |||
130 | /* | ||
131 | ** Load a non-nullable string into prototype 'p'. | ||
132 | */ | ||
133 | static TString *loadString (LoadState *S, Proto *p) { | ||
134 | TString *st = loadStringN(S, p); | ||
135 | if (st == NULL) | ||
136 | error(S, "bad format for constant string"); | ||
137 | return st; | ||
138 | } | ||
139 | |||
140 | |||
141 | static void loadCode (LoadState *S, Proto *f) { | ||
142 | int n = loadInt(S); | ||
143 | f->code = luaM_newvectorchecked(S->L, n, Instruction); | ||
144 | f->sizecode = n; | ||
145 | loadVector(S, f->code, n); | ||
146 | } | ||
147 | |||
148 | |||
149 | static void loadFunction(LoadState *S, Proto *f, TString *psource); | ||
150 | |||
151 | |||
152 | static void loadConstants (LoadState *S, Proto *f) { | ||
153 | int i; | ||
154 | int n = loadInt(S); | ||
155 | f->k = luaM_newvectorchecked(S->L, n, TValue); | ||
156 | f->sizek = n; | ||
157 | for (i = 0; i < n; i++) | ||
158 | setnilvalue(&f->k[i]); | ||
159 | for (i = 0; i < n; i++) { | ||
160 | TValue *o = &f->k[i]; | ||
161 | int t = loadByte(S); | ||
162 | switch (t) { | ||
163 | case LUA_VNIL: | ||
164 | setnilvalue(o); | ||
165 | break; | ||
166 | case LUA_VFALSE: | ||
167 | setbfvalue(o); | ||
168 | break; | ||
169 | case LUA_VTRUE: | ||
170 | setbtvalue(o); | ||
171 | break; | ||
172 | case LUA_VNUMFLT: | ||
173 | setfltvalue(o, loadNumber(S)); | ||
174 | break; | ||
175 | case LUA_VNUMINT: | ||
176 | setivalue(o, loadInteger(S)); | ||
177 | break; | ||
178 | case LUA_VSHRSTR: | ||
179 | case LUA_VLNGSTR: | ||
180 | setsvalue2n(S->L, o, loadString(S, f)); | ||
181 | break; | ||
182 | default: lua_assert(0); | ||
183 | } | ||
184 | } | ||
185 | } | ||
186 | |||
187 | |||
188 | static void loadProtos (LoadState *S, Proto *f) { | ||
189 | int i; | ||
190 | int n = loadInt(S); | ||
191 | f->p = luaM_newvectorchecked(S->L, n, Proto *); | ||
192 | f->sizep = n; | ||
193 | for (i = 0; i < n; i++) | ||
194 | f->p[i] = NULL; | ||
195 | for (i = 0; i < n; i++) { | ||
196 | f->p[i] = luaF_newproto(S->L); | ||
197 | luaC_objbarrier(S->L, f, f->p[i]); | ||
198 | loadFunction(S, f->p[i], f->source); | ||
199 | } | ||
200 | } | ||
201 | |||
202 | |||
203 | static void loadUpvalues (LoadState *S, Proto *f) { | ||
204 | int i, n; | ||
205 | n = loadInt(S); | ||
206 | f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc); | ||
207 | f->sizeupvalues = n; | ||
208 | for (i = 0; i < n; i++) { | ||
209 | f->upvalues[i].name = NULL; | ||
210 | f->upvalues[i].instack = loadByte(S); | ||
211 | f->upvalues[i].idx = loadByte(S); | ||
212 | f->upvalues[i].kind = loadByte(S); | ||
213 | } | ||
214 | } | ||
215 | |||
216 | |||
217 | static void loadDebug (LoadState *S, Proto *f) { | ||
218 | int i, n; | ||
219 | n = loadInt(S); | ||
220 | f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte); | ||
221 | f->sizelineinfo = n; | ||
222 | loadVector(S, f->lineinfo, n); | ||
223 | n = loadInt(S); | ||
224 | f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); | ||
225 | f->sizeabslineinfo = n; | ||
226 | for (i = 0; i < n; i++) { | ||
227 | f->abslineinfo[i].pc = loadInt(S); | ||
228 | f->abslineinfo[i].line = loadInt(S); | ||
229 | } | ||
230 | n = loadInt(S); | ||
231 | f->locvars = luaM_newvectorchecked(S->L, n, LocVar); | ||
232 | f->sizelocvars = n; | ||
233 | for (i = 0; i < n; i++) | ||
234 | f->locvars[i].varname = NULL; | ||
235 | for (i = 0; i < n; i++) { | ||
236 | f->locvars[i].varname = loadStringN(S, f); | ||
237 | f->locvars[i].startpc = loadInt(S); | ||
238 | f->locvars[i].endpc = loadInt(S); | ||
239 | } | ||
240 | n = loadInt(S); | ||
241 | for (i = 0; i < n; i++) | ||
242 | f->upvalues[i].name = loadStringN(S, f); | ||
243 | } | ||
244 | |||
245 | |||
246 | static void loadFunction (LoadState *S, Proto *f, TString *psource) { | ||
247 | f->source = loadStringN(S, f); | ||
248 | if (f->source == NULL) /* no source in dump? */ | ||
249 | f->source = psource; /* reuse parent's source */ | ||
250 | f->linedefined = loadInt(S); | ||
251 | f->lastlinedefined = loadInt(S); | ||
252 | f->numparams = loadByte(S); | ||
253 | f->is_vararg = loadByte(S); | ||
254 | f->maxstacksize = loadByte(S); | ||
255 | loadCode(S, f); | ||
256 | loadConstants(S, f); | ||
257 | loadUpvalues(S, f); | ||
258 | loadProtos(S, f); | ||
259 | loadDebug(S, f); | ||
260 | } | ||
261 | |||
262 | |||
263 | static void checkliteral (LoadState *S, const char *s, const char *msg) { | ||
264 | char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ | ||
265 | size_t len = strlen(s); | ||
266 | loadVector(S, buff, len); | ||
267 | if (memcmp(s, buff, len) != 0) | ||
268 | error(S, msg); | ||
269 | } | ||
270 | |||
271 | |||
272 | static void fchecksize (LoadState *S, size_t size, const char *tname) { | ||
273 | if (loadByte(S) != size) | ||
274 | error(S, luaO_pushfstring(S->L, "%s size mismatch", tname)); | ||
275 | } | ||
276 | |||
277 | |||
278 | #define checksize(S,t) fchecksize(S,sizeof(t),#t) | ||
279 | |||
280 | static void checkHeader (LoadState *S) { | ||
281 | /* skip 1st char (already read and checked) */ | ||
282 | checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk"); | ||
283 | if (loadByte(S) != LUAC_VERSION) | ||
284 | error(S, "version mismatch"); | ||
285 | if (loadByte(S) != LUAC_FORMAT) | ||
286 | error(S, "format mismatch"); | ||
287 | checkliteral(S, LUAC_DATA, "corrupted chunk"); | ||
288 | checksize(S, Instruction); | ||
289 | checksize(S, lua_Integer); | ||
290 | checksize(S, lua_Number); | ||
291 | if (loadInteger(S) != LUAC_INT) | ||
292 | error(S, "integer format mismatch"); | ||
293 | if (loadNumber(S) != LUAC_NUM) | ||
294 | error(S, "float format mismatch"); | ||
295 | } | ||
296 | |||
297 | |||
298 | /* | ||
299 | ** Load precompiled chunk. | ||
300 | */ | ||
301 | LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { | ||
302 | LoadState S; | ||
303 | LClosure *cl; | ||
304 | if (*name == '@' || *name == '=') | ||
305 | S.name = name + 1; | ||
306 | else if (*name == LUA_SIGNATURE[0]) | ||
307 | S.name = "binary string"; | ||
308 | else | ||
309 | S.name = name; | ||
310 | S.L = L; | ||
311 | S.Z = Z; | ||
312 | checkHeader(&S); | ||
313 | cl = luaF_newLclosure(L, loadByte(&S)); | ||
314 | setclLvalue2s(L, L->top, cl); | ||
315 | luaD_inctop(L); | ||
316 | cl->p = luaF_newproto(L); | ||
317 | luaC_objbarrier(L, cl, cl->p); | ||
318 | loadFunction(&S, cl->p, NULL); | ||
319 | lua_assert(cl->nupvalues == cl->p->sizeupvalues); | ||
320 | luai_verifycode(L, cl->p); | ||
321 | return cl; | ||
322 | } | ||
323 | |||