mirror of
https://github.com/gvx/Ser.git
synced 2024-11-14 18:34:22 +00:00
Add support for lots of tables (> 200)
Now only a single variable is used (_). What effect does this have on the file size and runtime? I have no idea. I don't think it'll be *that* much, though.
This commit is contained in:
parent
0548f7d383
commit
8cf0f0df6e
12
bigtest.lua
Normal file
12
bigtest.lua
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
local serialize = require 'ser'
|
||||||
|
|
||||||
|
local t = {}
|
||||||
|
for i = 1, 15 do
|
||||||
|
t[i] = {}
|
||||||
|
for j = 1, 10 do
|
||||||
|
t[i][j] = {{'woo'}}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local s = serialize(t)
|
||||||
|
print(s)
|
||||||
|
loadstring(s)()
|
17
ser.lua
17
ser.lua
@ -22,7 +22,7 @@ local function write(t, memo, rev_memo)
|
|||||||
memo[t] = index
|
memo[t] = index
|
||||||
rev_memo[index] = t
|
rev_memo[index] = t
|
||||||
end
|
end
|
||||||
return '_' .. memo[t]
|
return '_[' .. memo[t] .. ']'
|
||||||
else
|
else
|
||||||
error("Trying to serialize unsupported type " .. ty)
|
error("Trying to serialize unsupported type " .. ty)
|
||||||
end
|
end
|
||||||
@ -53,9 +53,9 @@ end
|
|||||||
|
|
||||||
local function write_table_ex(t, memo, rev_memo, srefs, name)
|
local function write_table_ex(t, memo, rev_memo, srefs, name)
|
||||||
if type(t) == 'function' then
|
if type(t) == 'function' then
|
||||||
return 'local _' .. name .. ' = loadstring ' .. make_safe(dump(t))
|
return '_[' .. name .. '] = loadstring ' .. make_safe(dump(t))
|
||||||
end
|
end
|
||||||
local m = {'local _', name, ' = {'}
|
local m = {'_[', name, '] = {'}
|
||||||
local mi = 3
|
local mi = 3
|
||||||
for i = 1, #t do -- don't use ipairs here, we need the gaps
|
for i = 1, #t do -- don't use ipairs here, we need the gaps
|
||||||
local v = t[i]
|
local v = t[i]
|
||||||
@ -106,16 +106,17 @@ return function(t)
|
|||||||
-- phase 3: add all the tricky cyclic stuff
|
-- phase 3: add all the tricky cyclic stuff
|
||||||
for i, v in ipairs(srefs) do
|
for i, v in ipairs(srefs) do
|
||||||
n = n + 1
|
n = n + 1
|
||||||
result[n] = write_key_value_pair(v[2], v[3], memo, rev_memo, '_' .. v[1])
|
result[n] = write_key_value_pair(v[2], v[3], memo, rev_memo, '_[' .. v[1] .. ']')
|
||||||
end
|
end
|
||||||
|
|
||||||
-- phase 4: add something about returning the main table
|
-- phase 4: add something about returning the main table
|
||||||
if result[n]:sub(1, 8) == 'local _0' then
|
if result[n]:sub(1, 5) == '_[0] ' then
|
||||||
result[n] = 'return' .. result[n]:sub(11)
|
result[n] = 'return' .. result[n]:sub(7)
|
||||||
else
|
else
|
||||||
result[n + 1] = 'return _0'
|
result[n + 1] = 'return _[0]'
|
||||||
end
|
end
|
||||||
|
|
||||||
-- phase 5: just concatenate everything
|
-- phase 5: just concatenate everything
|
||||||
return concat(result, '\n')
|
result = concat(result, '\n')
|
||||||
|
return n > 1 and 'local _ = {}\n' .. result or result
|
||||||
end
|
end
|
||||||
|
25
tests.lua
25
tests.lua
@ -36,14 +36,16 @@ case({}, 'return {}', 'empty table')
|
|||||||
case({true}, 'return {true}', 'simple table')
|
case({true}, 'return {true}', 'simple table')
|
||||||
|
|
||||||
case({{}}, [[
|
case({{}}, [[
|
||||||
local _1 = {}
|
local _ = {}
|
||||||
return {_1}]], 'empty table within a table')
|
_[1] = {}
|
||||||
|
return {_[1]}]], 'empty table within a table')
|
||||||
|
|
||||||
local _t = {}
|
local _t = {}
|
||||||
_t.self = _t
|
_t.self = _t
|
||||||
case(_t, [[local _0 = {}
|
case(_t, [=[local _ = {}
|
||||||
_0.self = _0
|
_[0] = {}
|
||||||
return _0]], 'simple cycle')
|
_[0].self = _[0]
|
||||||
|
return _[0]]=], 'simple cycle')
|
||||||
|
|
||||||
case_error({coroutine.create(function()end)}, './ser.lua:27: Trying to serialize unsupported type thread', 'unsupported type')
|
case_error({coroutine.create(function()end)}, './ser.lua:27: Trying to serialize unsupported type thread', 'unsupported type')
|
||||||
|
|
||||||
@ -54,12 +56,13 @@ case({'\127\230\255\254\128\12\0128\n\31'}, 'return {"\\127\\230\\255\\254\\128\
|
|||||||
case({['\127\230\255\254\128\12\0128\n\31'] = '\0'}, 'return {["\\127\\230\\255\\254\\128\\12\\0128\\n\\31"] = "\\0"}', 'non-ASCII or control characters in string key')
|
case({['\127\230\255\254\128\12\0128\n\31'] = '\0'}, 'return {["\\127\\230\\255\\254\\128\\12\\0128\\n\\31"] = "\\0"}', 'non-ASCII or control characters in string key')
|
||||||
|
|
||||||
local x = {}
|
local x = {}
|
||||||
case({x, {x}, x}, [[
|
case({x, {x}, x}, [=[
|
||||||
local _2 = {}
|
local _ = {}
|
||||||
local _1 = {}
|
_[2] = {}
|
||||||
local _0 = {_1, _2, _1}
|
_[1] = {}
|
||||||
_2[1] = _1
|
_[0] = {_[1], _[2], _[1]}
|
||||||
return _0]], 'repeated table')
|
_[2][1] = _[1]
|
||||||
|
return _[0]]=], 'repeated table')
|
||||||
|
|
||||||
case({['end'] = true, ['false'] = false}, 'return {["false"] = false, ["end"] = true}', 'keywords as table keys')
|
case({['end'] = true, ['false'] = false}, 'return {["false"] = false, ["end"] = true}', 'keywords as table keys')
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user