-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathlazyvar.lua
More file actions
309 lines (267 loc) · 9.01 KB
/
lazyvar.lua
File metadata and controls
309 lines (267 loc) · 9.01 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
local TableInsert = table.insert
local TableEmpty = table.empty
local iscallable = iscallable
local pcall = pcall
local setmetatable = setmetatable
-- Set this true to get tracebacks in error messages. It slows down lazyvars a lot,
-- so don't use except when debugging.
local ExtendedErrorMessages = false
local EvalContext = nil
local WeakKeyMeta = { __mode = 'k' }
---@alias Lazy<T> T | LazyVar<T> | fun(): T
---@class LazyVar<T> : Destroyable, OnDirtyListener, function
---@field OnDirty? function
---@field [1] any # Cached result of what we represent
---@field [2] boolean # Flag whether the lazy var is busy
---@field [3] string # Optional trace when `ExtendedErrorMessages` is set
---@field [4] function # Optional compute value when lazy var represents a function
---@field [5] table<LazyVar, boolean> # Table of lazy vars that we use
---@field [6] table<LazyVar, boolean> # Table of lazy vars that use us
---@field [7] LazyVar? # Lazy var that is dirty
---@field [8] LazyVar? # Lazy var that is dirty
---@field [9] LazyVar? # Lazy var that is dirty
---@field [10] LazyVar? # Lazy var that is dirty
---@field [11] LazyVar? # Lazy var that is dirty
---@field [12] LazyVar? # Lazy var that is dirty
---@field [13] LazyVar? # Lazy var that is dirty
---@field [14] LazyVar? # Lazy var that is dirty
---@field [15] LazyVar? # Lazy var that is dirty
---@field [16] LazyVar? # Lazy var that is dirty
---@field [17] LazyVar? # Lazy var that is dirty
---@field [18] LazyVar? # Lazy var that is dirty
---@field [19] LazyVar? # Lazy var that is dirty
---@field [20] LazyVar? # Lazy var that is dirty
LazyVarMetaTable = {
__call = function(self)
local value = self[1]
if value == nil then
-- track circular dependencies and error them out
if self[2] then
local trace = self[3]
if self[4] then
trace = trace or "[Set lazyvar.ExtendedErrorMessages for extra trace info]"
else
trace = trace or ""
end
error("circular dependency in lazy evaluation for variable " .. trace, 2)
end
-- clean up where we're used and what we use
local uses = self[5]
if not TableEmpty(uses) then
for use in uses do
use[6][self] = nil
end
for k, v in uses do
uses[k] = nil
end
end
-- compute the value of this lazy var, this populates the `uses` and `user_by` fields again
local currentContext = EvalContext
self[2] = true
EvalContext = self
local okay, value = pcall(self[4])
EvalContext = currentContext
self[2] = nil
-- check if it worked out
if okay then
self[1] = value
else
local trace = self[3]
if iscallable(self[4]) then
trace = trace or "[Set lazyvar.ExtendedErrorMessages for extra trace info]"
else
trace = trace or ""
end
error("error evaluating lazy variable: " .. value .. "\nStack trace from definition: " .. trace .. '\n', 2)
end
-- keep track of who is using us
if currentContext then
currentContext[5][self] = true
self[6][currentContext] = true
end
return value
end
-- keep track of who is using us
local currentContext = EvalContext
if currentContext then
currentContext[5][self] = true
self[6][currentContext] = true
end
return value
end,
--- Gather all lazy variables that respond to being re-evaluated
---@param self LazyVar
---@param onDirtyList LazyVar[]
---@return number
SetDirty = function(self, onDirtyList, head)
if self[1] ~= nil then
if self.OnDirty then
onDirtyList[head] = self
head = head + 1
end
self[1] = nil
for use in self[6] do
head = use:SetDirty(onDirtyList, head)
end
end
return head
end,
--- Lazy variable represents a function, the result is cached
---@param self LazyVar
---@param func function
SetFunction = function(self, func)
-- do not allow nils
if func == nil then
error("You are attempting to set a LazyVar's evaluation function to nil, don't do that!")
return
end
-- gather all those that use us
local head = 7
if self[1] ~= nil then
self[1] = nil
for use in self[6] do
head = use:SetDirty(self, head)
end
end
-- setup internal state for a function
self[4] = func
if ExtendedErrorMessages then
self[3] = debug.traceback('set from:')
end
-- tell ourself that we have a new value
local onDirty = self.OnDirty
if onDirty then
onDirty(self)
end
-- tell those that use us that we have a new value
for k = 7, head - 1 do
local lv = self[k]
onDirty = lv.OnDirty
if onDirty then
onDirty(lv)
end
self[k] = nil
end
end,
--- Lazy variable represents a value
---@param self LazyVar
---@param value any
SetValue = function(self, value)
if value == nil then
error("You are attempting to set a LazyVar's value to nil, don't do that!")
value = 0
end
-- gather all those that use us
local head = 7
if self[1] ~= nil then
self[1] = nil
for use in self[6] do
head = use:SetDirty(self, head)
end
end
-- setup internal state for a value
self[4] = nil
self[3] = nil
self[1] = value
-- now remove us from the `used_by` lists for any lazy vars we used to compute our value
local uses = self[5]
if not TableEmpty(uses) then
for use in uses do
use[6][self] = nil
end
self[5] = {}
end
-- tell ourself that we have a new value
local onDirty = self.OnDirty
if onDirty then
onDirty(self)
end
-- tell those that use us that we have a new value
for k = 7, head - 1 do
local lv = self[k]
onDirty = lv.OnDirty
if onDirty then
onDirty(lv)
end
self[k] = nil
end
end,
--- Lazy variable represents a value or a function
---@see SetFunction when the parameter is a function
---@see SetValue when the parameter is a value
---@param self LazyVar
---@param value any
Set = function(self, value)
if value == nil then
error("You are attempting to set a LazyVar to nil, don't do that!")
return
end
if iscallable(value) then
self:SetFunction(value)
else
self:SetValue(value)
end
end,
---@param self LazyVar
Destroy = function(self)
self.OnDirty = nil
self[4] = nil
local val = self[1]
self[1] = nil
if val then
local destroy = val.Destroy
if destroy then
destroy(val)
end
end
local onDestroy = self.OnDestroy
if onDestroy then
onDestroy(self)
self.OnDestroy = nil
end
end,
---@param self LazyVar
---@return boolean?
GetBusy = function(self)
return self[2]
end,
---@see This value is empty when `ExtendedErrorMessages` is set to false
---@param self LazyVar
---@return string
GetTrace = function(self)
return self[3]
end,
---@param self LazyVar
---@return function?
GetCompute = function(self)
return self[4]
end,
---@param self LazyVar
---@return table<LazyVar, boolean>
GetUses = function(self)
return self[5]
end,
---@param self LazyVar
---@return table<LazyVar, boolean>
GetUsedBy = function(self)
return self[6]
end,
}
LazyVarMetaTable.__index = LazyVarMetaTable
---@generic T
---@param initial? T defaults to `0`
---@return LazyVar<T>
function Create(initial)
local setmetatable = setmetatable
local WeakKeyMeta = WeakKeyMeta
if initial == nil then
initial = 0
end
---@type LazyVar
local result = {&1 &8} -- preallocate table with hashsize=1, arraysize=8
setmetatable(result, LazyVarMetaTable)
result[1] = initial
result[5] = setmetatable({ }, WeakKeyMeta)
result[6] = setmetatable({ }, WeakKeyMeta)
return result
end