/src/testdir/build/lua-master/source/ldump.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: ldump.c $ |
3 | | ** save precompiled Lua chunks |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define ldump_c |
8 | | #define LUA_CORE |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <limits.h> |
14 | | #include <stddef.h> |
15 | | |
16 | | #include "lua.h" |
17 | | |
18 | | #include "lobject.h" |
19 | | #include "lstate.h" |
20 | | #include "lundump.h" |
21 | | |
22 | | |
23 | | typedef struct { |
24 | | lua_State *L; |
25 | | lua_Writer writer; |
26 | | void *data; |
27 | | int strip; |
28 | | int status; |
29 | | } DumpState; |
30 | | |
31 | | |
32 | | /* |
33 | | ** All high-level dumps go through dumpVector; you can change it to |
34 | | ** change the endianness of the result |
35 | | */ |
36 | 1.84M | #define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0])) |
37 | | |
38 | 4.85k | #define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char)) |
39 | | |
40 | | |
41 | 1.84M | static void dumpBlock (DumpState *D, const void *b, size_t size) { |
42 | 1.84M | if (D->status == 0 && size > 0) { |
43 | 1.83M | lua_unlock(D->L); |
44 | 1.83M | D->status = (*D->writer)(D->L, b, size, D->data); |
45 | 1.83M | lua_lock(D->L); |
46 | 1.83M | } |
47 | 1.84M | } |
48 | | |
49 | | |
50 | 326k | #define dumpVar(D,x) dumpVector(D,&x,1) |
51 | | |
52 | | |
53 | 318k | static void dumpByte (DumpState *D, int y) { |
54 | 318k | lu_byte x = (lu_byte)y; |
55 | 318k | dumpVar(D, x); |
56 | 318k | } |
57 | | |
58 | | |
59 | | /* |
60 | | ** 'dumpSize' buffer size: each byte can store up to 7 bits. (The "+6" |
61 | | ** rounds up the division.) |
62 | | */ |
63 | 2.58M | #define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7) |
64 | | |
65 | 1.10M | static void dumpSize (DumpState *D, size_t x) { |
66 | 1.10M | lu_byte buff[DIBS]; |
67 | 1.10M | int n = 0; |
68 | 1.47M | do { |
69 | 1.47M | buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */ |
70 | 1.47M | x >>= 7; |
71 | 1.47M | } while (x != 0); |
72 | 1.10M | buff[DIBS - 1] |= 0x80; /* mark last byte */ |
73 | 1.10M | dumpVector(D, buff + DIBS - n, n); |
74 | 1.10M | } |
75 | | |
76 | | |
77 | 727k | static void dumpInt (DumpState *D, int x) { |
78 | 727k | dumpSize(D, x); |
79 | 727k | } |
80 | | |
81 | | |
82 | 5.52k | static void dumpNumber (DumpState *D, lua_Number x) { |
83 | 5.52k | dumpVar(D, x); |
84 | 5.52k | } |
85 | | |
86 | | |
87 | 2.42k | static void dumpInteger (DumpState *D, lua_Integer x) { |
88 | 2.42k | dumpVar(D, x); |
89 | 2.42k | } |
90 | | |
91 | | |
92 | 378k | static void dumpString (DumpState *D, const TString *s) { |
93 | 378k | if (s == NULL) |
94 | 27.0k | dumpSize(D, 0); |
95 | 351k | else { |
96 | 351k | size_t size = tsslen(s); |
97 | 351k | const char *str = getstr(s); |
98 | 351k | dumpSize(D, size + 1); |
99 | 351k | dumpVector(D, str, size); |
100 | 351k | } |
101 | 378k | } |
102 | | |
103 | | |
104 | 28.3k | static void dumpCode (DumpState *D, const Proto *f) { |
105 | 28.3k | dumpInt(D, f->sizecode); |
106 | 28.3k | dumpVector(D, f->code, f->sizecode); |
107 | 28.3k | } |
108 | | |
109 | | |
110 | | static void dumpFunction(DumpState *D, const Proto *f, TString *psource); |
111 | | |
112 | 28.3k | static void dumpConstants (DumpState *D, const Proto *f) { |
113 | 28.3k | int i; |
114 | 28.3k | int n = f->sizek; |
115 | 28.3k | dumpInt(D, n); |
116 | 147k | for (i = 0; i < n; i++) { |
117 | 119k | const TValue *o = &f->k[i]; |
118 | 119k | int tt = ttypetag(o); |
119 | 119k | dumpByte(D, tt); |
120 | 119k | switch (tt) { |
121 | 3.09k | case LUA_VNUMFLT: |
122 | 3.09k | dumpNumber(D, fltvalue(o)); |
123 | 3.09k | break; |
124 | 0 | case LUA_VNUMINT: |
125 | 0 | dumpInteger(D, ivalue(o)); |
126 | 0 | break; |
127 | 116k | case LUA_VSHRSTR: |
128 | 116k | case LUA_VLNGSTR: |
129 | 116k | dumpString(D, tsvalue(o)); |
130 | 116k | break; |
131 | 6 | default: |
132 | 6 | lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE); |
133 | 119k | } |
134 | 119k | } |
135 | 28.3k | } |
136 | | |
137 | | |
138 | 28.3k | static void dumpProtos (DumpState *D, const Proto *f) { |
139 | 28.3k | int i; |
140 | 28.3k | int n = f->sizep; |
141 | 28.3k | dumpInt(D, n); |
142 | 54.2k | for (i = 0; i < n; i++) |
143 | 25.9k | dumpFunction(D, f->p[i], f->source); |
144 | 28.3k | } |
145 | | |
146 | | |
147 | 28.3k | static void dumpUpvalues (DumpState *D, const Proto *f) { |
148 | 28.3k | int i, n = f->sizeupvalues; |
149 | 28.3k | dumpInt(D, n); |
150 | 61.4k | for (i = 0; i < n; i++) { |
151 | 33.0k | dumpByte(D, f->upvalues[i].instack); |
152 | 33.0k | dumpByte(D, f->upvalues[i].idx); |
153 | 33.0k | dumpByte(D, f->upvalues[i].kind); |
154 | 33.0k | } |
155 | 28.3k | } |
156 | | |
157 | | |
158 | 28.3k | static void dumpDebug (DumpState *D, const Proto *f) { |
159 | 28.3k | int i, n; |
160 | 28.3k | n = (D->strip) ? 0 : f->sizelineinfo; |
161 | 28.3k | dumpInt(D, n); |
162 | 28.3k | dumpVector(D, f->lineinfo, n); |
163 | 28.3k | n = (D->strip) ? 0 : f->sizeabslineinfo; |
164 | 28.3k | dumpInt(D, n); |
165 | 33.2k | for (i = 0; i < n; i++) { |
166 | 4.92k | dumpInt(D, f->abslineinfo[i].pc); |
167 | 4.92k | dumpInt(D, f->abslineinfo[i].line); |
168 | 4.92k | } |
169 | 28.3k | n = (D->strip) ? 0 : f->sizelocvars; |
170 | 28.3k | dumpInt(D, n); |
171 | 245k | for (i = 0; i < n; i++) { |
172 | 217k | dumpString(D, f->locvars[i].varname); |
173 | 217k | dumpInt(D, f->locvars[i].startpc); |
174 | 217k | dumpInt(D, f->locvars[i].endpc); |
175 | 217k | } |
176 | 28.3k | n = (D->strip) ? 0 : f->sizeupvalues; |
177 | 28.3k | dumpInt(D, n); |
178 | 45.1k | for (i = 0; i < n; i++) |
179 | 16.8k | dumpString(D, f->upvalues[i].name); |
180 | 28.3k | } |
181 | | |
182 | | |
183 | 28.3k | static void dumpFunction (DumpState *D, const Proto *f, TString *psource) { |
184 | 28.3k | if (D->strip || f->source == psource) |
185 | 27.0k | dumpString(D, NULL); /* no debug info or same source as its parent */ |
186 | 1.32k | else |
187 | 1.32k | dumpString(D, f->source); |
188 | 28.3k | dumpInt(D, f->linedefined); |
189 | 28.3k | dumpInt(D, f->lastlinedefined); |
190 | 28.3k | dumpByte(D, f->numparams); |
191 | 28.3k | dumpByte(D, f->is_vararg); |
192 | 28.3k | dumpByte(D, f->maxstacksize); |
193 | 28.3k | dumpCode(D, f); |
194 | 28.3k | dumpConstants(D, f); |
195 | 28.3k | dumpUpvalues(D, f); |
196 | 28.3k | dumpProtos(D, f); |
197 | 28.3k | dumpDebug(D, f); |
198 | 28.3k | } |
199 | | |
200 | | |
201 | 2.42k | static void dumpHeader (DumpState *D) { |
202 | 2.42k | dumpLiteral(D, LUA_SIGNATURE); |
203 | 2.42k | dumpByte(D, LUAC_VERSION); |
204 | 2.42k | dumpByte(D, LUAC_FORMAT); |
205 | 2.42k | dumpLiteral(D, LUAC_DATA); |
206 | 2.42k | dumpByte(D, sizeof(Instruction)); |
207 | 2.42k | dumpByte(D, sizeof(lua_Integer)); |
208 | 2.42k | dumpByte(D, sizeof(lua_Number)); |
209 | 2.42k | dumpInteger(D, LUAC_INT); |
210 | 2.42k | dumpNumber(D, LUAC_NUM); |
211 | 2.42k | } |
212 | | |
213 | | |
214 | | /* |
215 | | ** dump Lua function as precompiled chunk |
216 | | */ |
217 | | int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, |
218 | 2.42k | int strip) { |
219 | 2.42k | DumpState D; |
220 | 2.42k | D.L = L; |
221 | 2.42k | D.writer = w; |
222 | 2.42k | D.data = data; |
223 | 2.42k | D.strip = strip; |
224 | 2.42k | D.status = 0; |
225 | 2.42k | dumpHeader(&D); |
226 | 2.42k | dumpByte(&D, f->sizeupvalues); |
227 | 2.42k | dumpFunction(&D, f, NULL); |
228 | 2.42k | return D.status; |
229 | 2.42k | } |
230 | | |