This repository was archived by the owner on Jul 11, 2026. It is now read-only.
forked from darrellanderson/CrLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockTable.ttslua
More file actions
327 lines (277 loc) · 10.7 KB
/
Copy pathLockTable.ttslua
File metadata and controls
327 lines (277 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
-------------------------------------------------------------------------------
--- Access control for tables.
--
-- Locked tables are empty, using a metatable to detect and redirect all forms
-- of access to the actual table data. Metatables contain a few custom fields:
-- - '_name' string table name.
-- - '_attrSet' table from set attribute names to true.
-- - '_tableContents' table holding the actual locked table content.
--
-- @author Darrell
-------------------------------------------------------------------------------
local TAG = 'CrLua.LockTable'
CrLua = CrLua or {} -- global, <include> wraps in a do .. end block
CrLua.LockTable = assert(not CrLua.LockTable) and {
_require = {}
}
CrLua.LockTable.ATTR = {
READ_ONLY = 1,
REQUIRE_KEY = 2
}
CrLua.LockTable.ACCESS_TYPE = {
READ_MISSING = 'read missing',
WRITE_NEW = 'write new',
OVERWRITE = 'overwrite'
}
-------------------------------------------------------------------------------
--- Inject or overwrite a value into a locked table.
-- This is similar to rawset, except it preserves locked access to set items.
-- @param lockedTable table.
-- @param key.
-- @param value.
-------------------------------------------------------------------------------
function CrLua.LockTable.setEvenIfLocked(lockedTable, key, value)
assert(type(lockedTable) == 'table')
local metatable = getmetatable(lockedTable)
if metatable then
if not metatable._name or not metatable._tableContents then
error(TAG .. ': setEvenIfLocked has unknown metatable')
end
metatable._tableContents[key] = value
else
-- Not actually locked, do normal set.
lockedTable[key] = value
end
end
function CrLua.LockTable._testSetEvenIfLocked()
local table = CrLua.LockTable.readOnly('myTable', { 'foo', 'bar' })
CrLua.LockTable.setEvenIfLocked(table, 3, 'baz')
assert(table[1] == 'foo')
assert(table[3] == 'baz')
end
-------------------------------------------------------------------------------
--- Error and stop script when misusing a locked table.
-------------------------------------------------------------------------------
function CrLua.LockTable._error(lockedTable, key, accessType)
assert(type(lockedTable) == 'table' and type(accessType) == 'string')
local metatable = assert(getmetatable(lockedTable))
local tableName = assert(metatable._name)
local keyName = tostring(key or '<nil>')
error(TAG .. ': ' .. accessType .. ' ' .. tableName .. '.' .. keyName)
end
-------------------------------------------------------------------------------
--- Create a new table with the lock table metamethods.
-- @param tableName string : use this as the tableName.key when reporting errors.
-- @param attrs table : list of LockTable.ATTRS to apply.
-- @param table : table to lock (this table itself is mutated!)
-- @return table : the same table, returned for create-by-wrapping convenience.
-------------------------------------------------------------------------------
function CrLua.LockTable._apply(tableName, attrs, table)
assert(type(tableName) == 'string' and type(attrs) == 'table' and type(table) == 'table')
-- Do not attempt to lock a table that already has a metatable.
assert(not getmetatable(table), 'cannot apply LockTable when a metatable is already set: ' .. tableName)
local newMetaTable = {
_name = tableName,
_attrSet = {},
_tableContents = {}
}
-- Add the metatable methods to behave like the original table.
for k, v in pairs(CrLua.LockTable._metatable) do
newMetaTable[k] = v
end
-- Apply attrs.
for _, attr in ipairs(attrs) do
assert(type(attr) == 'number')
newMetaTable._attrSet[attr] = true
end
-- Move table contents.
for k, v in pairs(table) do
newMetaTable._tableContents[k] = v
table[k] = nil
end
setmetatable(table, newMetaTable)
return table
end
CrLua.LockTable._metatable = {}
function CrLua.LockTable._metatable.__index(lockedTable, key)
local metatable = getmetatable(lockedTable)
local attrSet = metatable._attrSet
local tableContents = metatable._tableContents
local value = tableContents[key]
local existing = value ~= nil
if not existing and attrSet[CrLua.LockTable.ATTR.REQUIRE_KEY] then
CrLua.LockTable._error(lockedTable, key, CrLua.LockTable.ACCESS_TYPE.READ_MISSING)
end
return value
end
function CrLua.LockTable._metatable.__newindex(lockedTable, key, value)
local metatable = getmetatable(lockedTable)
local attrSet = metatable._attrSet
local tableContents = metatable._tableContents
local existing = tableContents[key] ~= nil
if attrSet[CrLua.LockTable.ATTR.READ_ONLY] then
local accessType = existing and CrLua.LockTable.ACCESS_TYPE.OVERWRITE or CrLua.LockTable.ACCESS_TYPE.WRITE_NEW
CrLua.LockTable._error(lockedTable, key, accessType)
elseif not existing and attrSet[CrLua.LockTable.ATTR.REQUIRE_KEY] then
CrLua.LockTable._error(lockedTable, key, CrLua.LockTable.ACCESS_TYPE.WRITE_NEW)
end
tableContents[key] = value
end
function CrLua.LockTable._metatable.__pairs(lockedTable)
local metatable = getmetatable(lockedTable)
local tableContents = metatable._tableContents
local function iter(_, k)
local k, v = next(tableContents, k)
if v ~= nil then
return k, v
end
end
return iter, lockedTable, nil
end
function CrLua.LockTable._metatable.__ipairs(lockedTable)
local metatable = getmetatable(lockedTable)
local tableContents = metatable._tableContents
local function iter(_, i)
local v = tableContents[i + 1]
if v ~= nil then
return i + 1, v
end
end
return iter, lockedTable, 0
end
function CrLua.LockTable._metatable.__len(lockedTable)
local metatable = getmetatable(lockedTable)
local tableContents = metatable._tableContents
return #tableContents
end
-------------------------------------------------------------------------------
--- Make table read-only.
-- Reads to missing values return nil, as normal.
-- If the table has a _name entry, use that when reporting errors.
-- @param table
-- @param table : the same table as the argument for convenience assignment.
-------------------------------------------------------------------------------
function CrLua.LockTable.readOnly(tableName, table)
assert(type(tableName) == 'string' and type(table) == 'table')
assert(type(table) == 'table')
local attrs = { CrLua.LockTable.ATTR.READ_ONLY }
return CrLua.LockTable._apply(tableName, attrs, table)
end
function CrLua.LockTable._testReadOnly()
local table = CrLua.LockTable.readOnly('myTable', { 'foo', 'bar' })
-- Read existing key.
assert(table[1] == 'foo' and table[2] == 'bar')
-- Can read missing key.
assert(not table.missingKey)
-- Cannot write exiting key.
if pcall(function() table[2] = 'baz' end) then
error('wrote missing key')
end
-- Cannot write missing key.
if pcall(function() table.missingKey = 1 end) then
error('wrote missing key')
end
-- Verify pairs.
local count = 0
for k, v in pairs(table) do
assert((k == 1 and v == 'foo') or (k == 2 and v == 'bar'))
count = count + 1
end
assert(count == 2)
-- Verify ipairs.
local count = 0
for i, v in ipairs(table) do
assert((i == 1 and v == 'foo') or (i == 2 and v == 'bar'))
count = count + 1
end
assert(count == 2)
-- Verify len.
assert(#table == 2)
end
-------------------------------------------------------------------------------
--- Make table read-only, and error if accessing missing key.
-- Helps catch typos reading from must-have-item tables.
-- @param table
-- @param table : the same table as the argument for convenience assignment.
-------------------------------------------------------------------------------
function CrLua.LockTable.readOnlyRequireKey(tableName, table)
assert(type(tableName) == 'string' and type(table) == 'table')
local attrs = { CrLua.LockTable.ATTR.READ_ONLY, CrLua.LockTable.ATTR.REQUIRE_KEY }
return CrLua.LockTable._apply(tableName, attrs, table)
end
function CrLua.LockTable._testReadOnlyRequireKey()
local table = CrLua.LockTable.readOnlyRequireKey('myTable', { 'foo', 'bar' })
-- Read existing key.
assert(table[1] == 'foo' and table[2] == 'bar')
-- Cannot read missing key.
if pcall(function() return table.missingKey end) then
error('read missing key')
end
-- Cannot write exiting key.
if pcall(function() table[2] = 'baz' end) then
error('wrote missing key')
end
-- Cannot write missing key.
if pcall(function() table.missingKey = 1 end) then
error('wrote missing key')
end
-- Verify pairs.
local count = 0
for k, v in pairs(table) do
assert((k == 1 and v == 'foo') or (k == 2 and v == 'bar'))
count = count + 1
end
assert(count == 2)
-- Verify ipairs.
local count = 0
for i, v in ipairs(table) do
assert((i == 1 and v == 'foo') or (i == 2 and v == 'bar'))
count = count + 1
end
assert(count == 2)
-- Verify len.
assert(#table == 2)
end
-------------------------------------------------------------------------------
--- Require all table access read or write an existing key.
-- @param table
-- @param table : the same table as the argument for convenience assignment.
-------------------------------------------------------------------------------
function CrLua.LockTable.readWriteRequireKey(tableName, table)
assert(type(tableName) == 'string' and type(table) == 'table')
local attrs = { CrLua.LockTable.ATTR.REQUIRE_KEY }
return CrLua.LockTable._apply(tableName, attrs, table)
end
function CrLua.LockTable._testreadWriteRequireKey()
local table = CrLua.LockTable.readWriteRequireKey('myTable', { 'foo', 'bar' })
-- Read existing key.
assert(table[1] == 'foo' and table[2] == 'bar')
-- Cannot read missing key.
if pcall(function() return table.missingKey end) then
error('read missing key')
end
-- Write exiting key.
table[2] = 'baz'
assert(table[2] == 'baz')
table[2] = 'bar'
-- Cannot write missing key.
if pcall(function() table.missingKey = 1 end) then
error('wrote missing key')
end
-- Verify pairs.
local count = 0
for k, v in pairs(table) do
assert((k == 1 and v == 'foo') or (k == 2 and v == 'bar'))
count = count + 1
end
assert(count == 2)
-- Verify ipairs.
local count = 0
for i, v in ipairs(table) do
assert((i == 1 and v == 'foo') or (i == 2 and v == 'bar'))
count = count + 1
end
assert(count == 2)
-- Verify len.
assert(#table == 2)
end