-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtraceable.lua
366 lines (325 loc) · 9.04 KB
/
traceable.lua
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
local next = next
local traceable = {}
local _M = traceable
local function merge(dst, src)
for k, v in next, src do
dst[k] = v
end
return dst
end
local function normalize_field_path(field_path)
return field_path:gsub("^([-?%d]+)", "[%1]")
:gsub("%.([-?%d]+)", "[%1]")
:gsub("^([^.%[%]]+)", "['%1']")
:gsub("%.([^.%[%]]+)", "['%1']")
end
local getter_template = [[
return function(t)
local success, value = pcall(function (t)
return t%s
end, t)
if success then
return value
end
end
]]
local setter_template = [[
return function(t, v)
local success, err = pcall(function (t, v)
t%s = v
end, t, v)
end
]]
local getter_cache = {}
local setter_cache = {}
local function compile_normalized_property(field_path)
local getter, setter = getter_cache[field_path], setter_cache[field_path]
if not getter then
getter = assert(loadstring(getter_template:format(field_path)))()
getter_cache[field_path] = getter
setter = assert(loadstring(setter_template:format(field_path)))()
setter_cache[field_path] = setter
end
return getter, setter
end
function _M.compile_property(field_path)
return compile_normalized_property(normalize_field_path(field_path))
end
local set
local function create()
local o = {
dirty = false,
ignored = false,
opaque = false,
_stage = {},
_traced = {},
_lastversion = {},
_parent = false,
}
return setmetatable(o, {
__index = o._stage,
__newindex = set,
__len = _M.len,
__pairs = _M.pairs,
__ipairs = _M.ipairs,
})
end
local function mark_dirty(t)
while t and not t.dirty do
t.dirty = true
t = t._parent
end
end
set = function (t, k, v, force)
local stage = t._stage
local u = stage[k]
if _M.is_traceable(v) then
if _M.is(u) then
for k in next, u._stage do
if v[k] == nil then
u[k] = nil
end
end
else
u = create()
u._parent = t
stage[k] = u
end
merge(u, v)
else
local traced = t._traced
local lastverison = t._lastversion
if stage[k] ~= v or force then
if not traced[k] then
traced[k] = true
lastverison[k] = u
end
stage[k] = v
mark_dirty(t)
end
end
end
function _M.is(t)
local mt = getmetatable(t)
return type(mt) == "table" and mt.__newindex == set
end
function _M.is_traceable(t)
if type(t) ~= "table" then
return false
end
local mt = getmetatable(t)
return mt == nil or mt.__newindex == set
end
function _M.new(t)
local o = create()
if t then
merge(o, t)
end
return o
end
function _M.mark_changed(t, k)
local v = t[k]
if _M.is(v) then
mark_dirty(v)
else
set(t, k, v, true)
end
end
function _M.set_ignored(t, ignored)
t.ignored = ignored
if not ignored and t.dirty then
mark_dirty(t._parent)
end
end
function _M.set_opaque(t, opaque)
t.opaque = opaque
end
local k_stub = setmetatable({}, { __tostring = function () return "k_stub" end })
function _M.compile_map(src)
local o = {}
for i = 1, #src do
local v = src[i]
local argc = #v
-- v = {argc, action, arg1, arg2, ..., argn}
v = {argc, unpack(v)} -- copy
for j = 1, argc - 1 do
local k = j + 2
local arg = normalize_field_path(v[k])
v[k] = compile_normalized_property(arg)
local p, c = nil, o
for field in arg:gmatch("([^%[%]\'\"]+)") do
field = tonumber(field) or field
p, c = c, c[field]
if not c then
c = {}
p[field] = c
end
end
local stub = c[k_stub]
if not stub then
stub = {[1] = 1}
c[k_stub] = stub
end
local n = stub[1] + 1
stub[1] = n
stub[n] = v
end
end
return o
end
local function get_map_stub(map, k)
local map_k = map[k]
return map_k and map_k[k_stub]
end
local argv = {}
local function do_map(t, mappings, mapped)
for i = 2, mappings[1] do
local mapping = mappings[i]
if not mapped[mapping] then
mapped[mapping] = true
local argc = mapping[1]
argv[1] = t
for i = 2, argc do
argv[i] = mapping[i + 1](t)
end
local action = mapping[2]
action(unpack(argv, 1, argc))
end
end
end
local function next_traced(t, k)
local k = next(t._traced, k)
return k, t._stage[k], t._lastversion[k]
end
local function _commit(keep_dirty, t, sub, changed, newestversion, lastversion, map, mapped)
if not sub.dirty then
return false
end
local traced = sub._traced
local trace = sub._lastversion
local has_changed = next(traced) ~= nil
for k, v in next, sub._stage do
if _M.is(v) and not v.ignored then
if v.opaque then
has_changed = _commit(keep_dirty, t, v)
if has_changed then
if changed then
changed[k] = true
end
end
else
local c, n, l = changed and {}, newestversion and {}, lastversion and {}
has_changed = _commit(keep_dirty, t, v, c, n, l, map and map[k], mapped)
if has_changed then
if changed then
changed[k] = c
end
if newestversion then
newestversion[k] = n
end
if lastversion then
lastversion[k] = l
end
end
end
if has_changed and map then
local stub = get_map_stub(map, k)
if stub then
do_map(t, stub, mapped)
end
end
end
end
for k, v, u in next_traced, sub do
if not keep_dirty then
traced[k] = nil
trace[k] = nil
end
if changed then
changed[k] = true
end
if newestversion then
newestversion[k] = v
end
if lastversion then
lastversion[k] = u
end
if map then
local stub = get_map_stub(map, k)
if stub then
do_map(t, stub, mapped)
end
end
end
if not keep_dirty then
sub.dirty = false
end
return has_changed
end
local flag_map = {
[("k"):byte()] = "changed",
[("v"):byte()] = "newestversion",
[("u"):byte()] = "lastversion",
}
local function commit(t, map, diff_flags, keep_dirty)
local has_changed, diff
if diff_flags then
diff = {}
for i = 1, #diff_flags do
local field_name = flag_map[diff_flags:byte(i)]
if field_name then
diff[field_name] = {}
end
end
has_changed = _commit(keep_dirty,
t, t,
diff.changed, diff.newestversion, diff.lastversion,
map, map and {})
else
has_changed = _commit(keep_dirty,
t, t,
nil, nil, nil,
map, map and {})
end
return has_changed, diff
end
function _M.commit(t, map, diff_flags)
if type(map) == "string" then
map, diff_flags = nil, map
end
return commit(t, map, diff_flags, false)
end
function _M.diff(t, map, diff_flags)
if type(map) == "string" then
map, diff_flags = nil, map
end
return commit(t, map, diff_flags, true)
end
local function do_maps(t, sub, map, mapped)
for k, map_k in next, map do
local stub = map_k[k_stub]
if stub then
do_map(t, stub, mapped)
end
local v = sub[k]
if type(v) == "table" then
do_maps(t, v, map_k, mapped)
end
end
end
function _M.map(t, map)
do_maps(t, t, map, map and {})
end
local function forward_to(field, func)
return function (t, ...)
return func(t[field], ...)
end
end
function _M.len(t)
return #(t._stage)
end
_M.next = forward_to("_stage", next)
_M.pairs = forward_to("_stage", pairs)
_M.ipairs = forward_to("_stage", ipairs)
_M.unpack = forward_to("_stage", unpack)
return _M