Skip to content

Commit c639c7d

Browse files
committed
Fixed bug re serializing variables named 'true' or 'false'
1 parent 5c40012 commit c639c7d

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

lua/serialize.lua

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ module (..., package.seeall)
88
-- See "Programming In Lua" chapter 12.1.2.
99
-- Also see forum thread:
1010
-- http://www.gammon.com.au/forum/?id=4960
11+
12+
-- Amended 7th April 2020 to fix bug re serializing variables
13+
-- named 'true' or 'false'
1114
-- ----------------------------------------------------------
1215

1316
--[[
@@ -16,7 +19,7 @@ module (..., package.seeall)
1619
1720
require "serialize"
1821
SetVariable ("mobs", serialize.save ("mobs")) --> serialize mobs table
19-
loadstring (GetVariable ("mobs")) () --> restore mobs table
22+
loadstring (GetVariable ("mobs")) () --> restore mobs table
2023
2124
If you need to serialize two tables where subsequent ones refer to earlier ones
2225
you can supply your own "saved tables" variable, like this:
@@ -26,10 +29,10 @@ module (..., package.seeall)
2629
result = result .. "\n" .. serialize.save ("quests", nil, t)
2730
2831
In this example the serializing of "quests" also knows about the "mobs" table
29-
and will use references to it where necessary.
32+
and will use references to it where necessary.
3033
3134
You can also supply the actual variable if the variable to be serialized does
32-
not exist in the global namespace (for instance, if the variable is a local
35+
not exist in the global namespace (for instance, if the variable is a local
3336
variable to a function). eg.
3437
3538
require "serialize"
@@ -42,17 +45,17 @@ module (..., package.seeall)
4245
because it would not be found in the _G namespace.
4346
4447
----- Added on 19 July 2007:
45-
48+
4649
You can now do a "simple save" which is intended for tables without cycles. That is,
4750
tables, that do not refer to other tables. This is appropriate for "simple" data, like
4851
a straight table of keys/values, including subtables.
49-
52+
5053
For a simple save, all you need to do is supply the value, like this:
51-
54+
5255
print (serialize.save_simple ({ foo = 22, bar = "hi", t = { s = 9, k = 22 } }))
53-
56+
5457
This produces:
55-
58+
5659
{
5760
t = {
5861
s = 9,
@@ -61,7 +64,7 @@ module (..., package.seeall)
6164
bar = "hi",
6265
foo = 22,
6366
}
64-
67+
6568
--]]
6669

6770
local save_item -- forward declaration, function appears near the end
@@ -72,12 +75,12 @@ function save (what, v, saved)
7275
saved = saved or {} -- initial table of tables we have already done
7376
v = v or _G [what] -- default to "what" in global namespace
7477

75-
assert (type (what) == "string",
78+
assert (type (what) == "string",
7679
"1st argument to serialize.save should be the *name* of a variable")
77-
80+
7881
assert (v, "Variable '" .. what .. "' does not exist")
7982

80-
assert (type (saved) == "table" or saved == nil,
83+
assert (type (saved) == "table" or saved == nil,
8184
"3rd argument to serialize.save should be a table or nil")
8285

8386
local out = {} -- output to this table
@@ -88,7 +91,7 @@ end -- serialize.save
8891
function save_simple (v)
8992
local out = {} -- output to this table
9093
save_item_simple (v, out, 2) -- do serialization
91-
return table.concat (out) -- turn into a string
94+
return table.concat (out) -- turn into a string
9295
end -- serialize.save_simple
9396

9497
--- below are local functions for this module -------------
@@ -99,7 +102,7 @@ local function basicSerialize (o)
99102
else -- assume it is a string
100103
return string.format("%q", o)
101104
end
102-
end -- basicSerialize
105+
end -- basicSerialize
103106

104107
--
105108
-- Lua keywords might look OK to not be quoted as keys but must be.
@@ -109,22 +112,22 @@ end -- basicSerialize
109112
local lua_reserved_words = {}
110113

111114
for _, v in ipairs ({
112-
"and", "break", "do", "else", "elseif", "end",
113-
"for", "function", "if", "in", "local", "nil", "not", "or",
114-
"repeat", "return", "then", "until", "while"
115+
"and", "break", "do", "else", "elseif", "end",
116+
"for", "function", "if", "in", "local", "nil", "not", "or",
117+
"repeat", "return", "then", "until", "while", "true", "false"
115118
}) do lua_reserved_words [v] = true end
116119

117120
-- ----------------------------------------------------------
118121
-- save one variable (calls itself recursively)
119-
--
122+
--
120123
-- Modified on 23 October 2005 to better handle keys (like table keys)
121124
-- ----------------------------------------------------------
122125
function save_item (name, value, out, indent, saved) -- important! no "local" keyword
123126
local iname = string.rep (" ", indent) .. name -- indented name
124127

125128
-- numbers, strings, and booleans can be simply serialized
126129

127-
if type (value) == "number" or
130+
if type (value) == "number" or
128131
type (value) == "string" or
129132
type (value) == "boolean" then
130133
table.insert (out, iname .. " = " .. basicSerialize(value))
@@ -141,31 +144,31 @@ function save_item (name, value, out, indent, saved) -- important! no "local" k
141144
saved [value] = name -- save name for next time
142145

143146
-- make the table constructor, and recurse to save its contents
144-
147+
145148
table.insert (out, iname .. " = {}") -- create a new table
146149

147150
for k, v in pairs (value) do -- save its fields
148-
local fieldname
151+
local fieldname
149152

150153
-- if key is a Lua variable name which is not a reserved word
151154
-- we can express it as tablename.keyname
152155

153156
if type (k) == "string"
154-
and string.find (k, "^[_%a][_%a%d]*$")
157+
and string.find (k, "^[_%a][_%a%d]*$")
155158
and not lua_reserved_words [k] then
156159
fieldname = string.format("%s.%s", name, k)
157160

158161
-- if key is a table itself, and we know its name then we can use that
159162
-- eg. tablename [ tablekeyname ]
160163

161164
elseif type (k) == "table" and saved[k] then
162-
fieldname = string.format("%s[%s]", name, saved [k])
165+
fieldname = string.format("%s[%s]", name, saved [k])
163166

164167
-- if key is an unknown table, we have to raise an error as we cannot
165168
-- deduce its name
166-
169+
167170
elseif type (k) == "table" then
168-
error ("Key table entry " .. tostring (k) ..
171+
error ("Key table entry " .. tostring (k) ..
169172
" in table " .. name .. " is not known")
170173

171174
-- if key is a number or a boolean it can simply go in brackets,
@@ -175,7 +178,7 @@ function save_item (name, value, out, indent, saved) -- important! no "local" k
175178
fieldname = string.format("%s[%s]", name, tostring (k))
176179

177180
-- now key should be a string, otherwise an error
178-
181+
179182
elseif type (k) ~= "string" then
180183
error ("Cannot serialize table keys of type '" .. type (k) ..
181184
"' in table " .. name)
@@ -185,13 +188,13 @@ function save_item (name, value, out, indent, saved) -- important! no "local" k
185188

186189
else
187190
fieldname = string.format("%s[%s]", name,
188-
basicSerialize(k))
191+
basicSerialize(k))
189192
end
190193

191194
-- now we have finally worked out a suitable name for the key,
192195
-- recurse to save the value associated with it
193196

194-
save_item(fieldname, v, out, indent + 2, saved)
197+
save_item(fieldname, v, out, indent + 2, saved)
195198
end
196199
end
197200

@@ -200,13 +203,13 @@ function save_item (name, value, out, indent, saved) -- important! no "local" k
200203
else
201204
error ("Cannot serialize '" .. name .. "' (" .. type (value) .. ")")
202205
end -- if type of variable
203-
end -- save_item
206+
end -- save_item
204207

205208
-- saves tables *without* cycles (produces smaller output)
206209
function save_item_simple (value, out, indent)
207210
-- numbers, strings, and booleans can be simply serialized
208211

209-
if type (value) == "number" or
212+
if type (value) == "number" or
210213
type (value) == "string" or
211214
type (value) == "boolean" then
212215
table.insert (out, basicSerialize(value))
@@ -215,23 +218,23 @@ function save_item_simple (value, out, indent)
215218

216219
for k, v in pairs (value) do -- save its fields
217220
table.insert (out, string.rep (" ", indent))
218-
if not string.find (k, '^[_%a][_%a%d]*$')
221+
if not string.find (k, '^[_%a][_%a%d]*$')
219222
or lua_reserved_words [k] then
220223
table.insert (out, "[" .. basicSerialize (k) .. "] = ")
221224
else
222225
table.insert (out, k .. " = ")
223226
end -- if
224-
save_item_simple (v, out, indent + 2)
227+
save_item_simple (v, out, indent + 2)
225228
table.insert (out, ",\n")
226229
end -- for each table item
227-
230+
228231
table.insert (out, string.rep (" ", indent) .. "}")
229-
232+
230233
-- cannot serialize things like functions, threads
231234

232235
else
233236
error ("Cannot serialize " .. type (value))
234237
end -- if type of variable
235-
238+
236239
end -- save_item_simple
237240

0 commit comments

Comments
 (0)