Dump doesn't need to reuse 'source'

All strings are being reused now, including 'source'.
This commit is contained in:
Roberto Ierusalimschy
2022-12-20 11:14:52 -03:00
parent d70a0c91ad
commit 7d6a97e42b
3 changed files with 35 additions and 12 deletions

View File

@@ -487,5 +487,30 @@ do
end
end
do -- check reuse of strings in dumps
local str = "|" .. string.rep("X", 50) .. "|"
local foo = load(string.format([[
local str <const> = "%s"
return {
function () return str end,
function () return str end,
function () return str end
}
]], str))
-- count occurrences of 'str' inside the dump
local dump = string.dump(foo)
local _, count = string.gsub(dump, str, {})
-- there should be only two occurrences:
-- one inside the source, other the string itself.
assert(count == 2)
if T then -- check reuse of strings in undump
local funcs = load(dump)()
assert(string.format("%p", T.listk(funcs[1])[1]) ==
string.format("%p", T.listk(funcs[3])[1]))
end
end
print('OK')
return deep