aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-12-20 11:14:52 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-12-20 11:14:52 -0300
commit7d6a97e42bc3328b9c5ec1dabbd7e280e81c3efd (patch)
tree24c32dddd2b6df02d22bdcacc0636a18f37f7471
parentd70a0c91ad42275af1f6f1b6e37c604442b3f0d1 (diff)
downloadlua-7d6a97e42bc3328b9c5ec1dabbd7e280e81c3efd.tar.gz
lua-7d6a97e42bc3328b9c5ec1dabbd7e280e81c3efd.tar.bz2
lua-7d6a97e42bc3328b9c5ec1dabbd7e280e81c3efd.zip
Dump doesn't need to reuse 'source'
All strings are being reused now, including 'source'.
Diffstat (limited to '')
-rw-r--r--ldump.c12
-rw-r--r--lundump.c10
-rw-r--r--testes/calls.lua25
3 files changed, 35 insertions, 12 deletions
diff --git a/ldump.c b/ldump.c
index 70c7adc6..a99d7ec5 100644
--- a/ldump.c
+++ b/ldump.c
@@ -126,7 +126,7 @@ static void dumpCode (DumpState *D, const Proto *f) {
126} 126}
127 127
128 128
129static void dumpFunction(DumpState *D, const Proto *f, TString *psource); 129static void dumpFunction(DumpState *D, const Proto *f);
130 130
131static void dumpConstants (DumpState *D, const Proto *f) { 131static void dumpConstants (DumpState *D, const Proto *f) {
132 int i; 132 int i;
@@ -159,7 +159,7 @@ static void dumpProtos (DumpState *D, const Proto *f) {
159 int n = f->sizep; 159 int n = f->sizep;
160 dumpInt(D, n); 160 dumpInt(D, n);
161 for (i = 0; i < n; i++) 161 for (i = 0; i < n; i++)
162 dumpFunction(D, f->p[i], f->source); 162 dumpFunction(D, f->p[i]);
163} 163}
164 164
165 165
@@ -199,9 +199,9 @@ static void dumpDebug (DumpState *D, const Proto *f) {
199} 199}
200 200
201 201
202static void dumpFunction (DumpState *D, const Proto *f, TString *psource) { 202static void dumpFunction (DumpState *D, const Proto *f) {
203 if (D->strip || f->source == psource) 203 if (D->strip)
204 dumpString(D, NULL); /* no debug info or same source as its parent */ 204 dumpString(D, NULL); /* no debug info */
205 else 205 else
206 dumpString(D, f->source); 206 dumpString(D, f->source);
207 dumpInt(D, f->linedefined); 207 dumpInt(D, f->linedefined);
@@ -245,7 +245,7 @@ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
245 D.nstr = 0; 245 D.nstr = 0;
246 dumpHeader(&D); 246 dumpHeader(&D);
247 dumpByte(&D, f->sizeupvalues); 247 dumpByte(&D, f->sizeupvalues);
248 dumpFunction(&D, f, NULL); 248 dumpFunction(&D, f);
249 return D.status; 249 return D.status;
250} 250}
251 251
diff --git a/lundump.c b/lundump.c
index 4048fdea..3bff463f 100644
--- a/lundump.c
+++ b/lundump.c
@@ -162,7 +162,7 @@ static void loadCode (LoadState *S, Proto *f) {
162} 162}
163 163
164 164
165static void loadFunction(LoadState *S, Proto *f, TString *psource); 165static void loadFunction(LoadState *S, Proto *f);
166 166
167 167
168static void loadConstants (LoadState *S, Proto *f) { 168static void loadConstants (LoadState *S, Proto *f) {
@@ -211,7 +211,7 @@ static void loadProtos (LoadState *S, Proto *f) {
211 for (i = 0; i < n; i++) { 211 for (i = 0; i < n; i++) {
212 f->p[i] = luaF_newproto(S->L); 212 f->p[i] = luaF_newproto(S->L);
213 luaC_objbarrier(S->L, f, f->p[i]); 213 luaC_objbarrier(S->L, f, f->p[i]);
214 loadFunction(S, f->p[i], f->source); 214 loadFunction(S, f->p[i]);
215 } 215 }
216} 216}
217 217
@@ -266,10 +266,8 @@ static void loadDebug (LoadState *S, Proto *f) {
266} 266}
267 267
268 268
269static void loadFunction (LoadState *S, Proto *f, TString *psource) { 269static void loadFunction (LoadState *S, Proto *f) {
270 f->source = loadStringN(S, f); 270 f->source = loadStringN(S, f);
271 if (f->source == NULL) /* no source in dump? */
272 f->source = psource; /* reuse parent's source */
273 f->linedefined = loadInt(S); 271 f->linedefined = loadInt(S);
274 f->lastlinedefined = loadInt(S); 272 f->lastlinedefined = loadInt(S);
275 f->numparams = loadByte(S); 273 f->numparams = loadByte(S);
@@ -342,7 +340,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
342 luaD_inctop(L); 340 luaD_inctop(L);
343 cl->p = luaF_newproto(L); 341 cl->p = luaF_newproto(L);
344 luaC_objbarrier(L, cl, cl->p); 342 luaC_objbarrier(L, cl, cl->p);
345 loadFunction(&S, cl->p, NULL); 343 loadFunction(&S, cl->p);
346 lua_assert(cl->nupvalues == cl->p->sizeupvalues); 344 lua_assert(cl->nupvalues == cl->p->sizeupvalues);
347 luai_verifycode(L, cl->p); 345 luai_verifycode(L, cl->p);
348 L->top.p--; /* pop table */ 346 L->top.p--; /* pop table */
diff --git a/testes/calls.lua b/testes/calls.lua
index ee8cce73..cd2696e8 100644
--- a/testes/calls.lua
+++ b/testes/calls.lua
@@ -487,5 +487,30 @@ do
487 end 487 end
488end 488end
489 489
490
491do -- check reuse of strings in dumps
492 local str = "|" .. string.rep("X", 50) .. "|"
493 local foo = load(string.format([[
494 local str <const> = "%s"
495 return {
496 function () return str end,
497 function () return str end,
498 function () return str end
499 }
500 ]], str))
501 -- count occurrences of 'str' inside the dump
502 local dump = string.dump(foo)
503 local _, count = string.gsub(dump, str, {})
504 -- there should be only two occurrences:
505 -- one inside the source, other the string itself.
506 assert(count == 2)
507
508 if T then -- check reuse of strings in undump
509 local funcs = load(dump)()
510 assert(string.format("%p", T.listk(funcs[1])[1]) ==
511 string.format("%p", T.listk(funcs[3])[1]))
512 end
513end
514
490print('OK') 515print('OK')
491return deep 516return deep