-
Notifications
You must be signed in to change notification settings - Fork 6
/
init.lua
424 lines (366 loc) · 10.2 KB
/
init.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
--[[
-- TODO:
-- * Document meta.len/ipairs/pairs
-- * Rewrite the debugging system.
-- * Rewrite the topological order algorithm.
-- * Test rsrc as last parameter of triggers.
-- * await(p) -- em vez de -- p() / TESTES / DOC
-- * tirar o timer daqui
-- * gvt / rct.*
--]]
local _G = _G
local co_running, co_create, co_resume, co_yield, co_status =
coroutine.running, coroutine.create,
coroutine.resume, coroutine.yield, coroutine.status
local t_insert, t_remove = table.insert, table.remove
local error, assert, type, pairs, ipairs, setmetatable, getmetatable, tostring =
error, assert, type, pairs, ipairs, setmetatable, getmetatable, tostring
module (...)
local SS = setmetatable({}, {__mode='k'})
S = nil
local trigger, schedule, run, finish, propagate, updateHeight,
addEdge, createTimer, checkTimers, check_create
trigger = function (rsrc, edgeType, rdst, param)
local st, ret
if (edgeType == 'resume') or (edgeType == 'call') then
assert(rdst.clearAwaiting)()
rdst.clearAwaiting = nil
end
-- CALL
S.stack[#S.stack+1] = rdst
if rdst.zero then
assert((not rdst.co) and (rdst.state=='ready'))
rdst.state = 'running'
if rdst.obj then
ret = rdst.body(rdst.obj, param, rsrc)
else
ret = rdst.body(param, rsrc)
end
else
if edgeType == 'start' then -- reacao duradoura: necessita de co-rotina
assert((not rdst.co) and (rdst.state=='ready'), rdst.state)
rdst.co = co_create(rdst.body)
else -- resume
assert((edgeType == 'resume') or (edgeType == 'call'), edgeType)
assert(rdst.co and (rdst.state=='awaiting'))
end
assert(co_status(rdst.co) == 'suspended')
rdst.state = 'running'
st, ret = co_resume(rdst.co, param, rsrc)
--[[
TEMP: nao fazia sentido passar o obj no resume!
if rdst.obj and (edgeType == 'start') then
st, ret = co_resume(rdst.co, rdst.obj, param, rsrc)
else
st, ret = co_resume(rdst.co, param, rsrc)
end
]]
assert(st, ret)
end
S.stack[#S.stack] = nil
-- not finished
if (not rdst.zero) and (co_status(rdst.co) == 'suspended') then
rdst.state = 'awaiting'
-- finished
else
assert(rdst.zero or (co_status(rdst.co) == 'dead'))
finish(rdst, ret) -- it will set rdst.state = 'ready'
end
return ret
end
-- TODO: criar ponteiro LAST e adicionar reacoes de tras pra frente
-- edgeType: 'start', 'resume'
schedule = function (rsrc, edgeType, rdst, param)
-- optimize when height is not used
--if not rdst.height then
--trigger(edgeType, rdst, param)
--return
--end
local cur = rdst.cEdgeType
rdst.cParam = param
rdst.cRsrc = rsrc
if cur then
assert(cur == edgeType)
else
rdst.cEdgeType = edgeType
local height = rdst.height or 0
local head, prev = S.torun, nil
rdst.cHeight = height
while head and (head.cHeight <= height) do
prev = head
head = head.cNext
end
if prev then
prev.cNext = rdst
else
S.torun = rdst
end
rdst.cNext = head
end
end
run = function (reactor, param)
while S.torun
do
local rdst = S.torun
S.torun = rdst.cNext
rdst.cNext = nil
local edgeType = rdst.cEdgeType
rdst.cEdgeType = nil
trigger(rdst.cRsrc, edgeType, rdst, rdst.cParam)
end
end
finish = function (reactor, ret)
reactor.state = 'ready'
if not reactor.zero then
reactor.co = nil
end
propagate(reactor, reactor.edges, ret)
end
propagate = function (src, edges, ret)
for rdst, edgeType in pairs(edges) do
if (ret~=cancel or edgeType=='call') and
(not is(rdst) or rdst.state~='deactivated') then
schedule(src, edgeType, rdst, ret)
end
end
end
updateHeight = function (rsrc, rdst, edgeType)
if edgeType ~= 'start' then return end -- TODO: entender isso novamente
if not rdst.zero then return end
assert(not rdst._wasVisited, 'tight cycle detected')
if not rsrc.height then
rsrc.height = 1
end
local new = rsrc.height + 1
if (rdst.height or 0) >= new then
return
end
rdst.height = new
rsrc._wasVisited = true
for r,tp in pairs(rdst.edges) do
updateHeight(rdst, r, tp)
end
rsrc._wasVisited = false
end
addEdge = function (rsrc, rdst, edgeType)
assert((type(rsrc) == 'string') or is(rsrc))
assert(is(rdst))
assert(edgeType)
local edges
if type(rsrc) == 'string' then
edges = S.strings[rsrc] or {}
S.strings[rsrc] = edges
else
edges = rsrc.edges
end
assert(not edges[rdst], tostring(rsrc)..' -> '..tostring(rdst)) -- TODO: never tested!
-- create the link
edges[rdst] = edgeType
if type(rsrc) ~= 'string' then
updateHeight(rsrc, rdst, edgeType)
end
return rsrc, rdst
end
remEdge = function (rsrc, rdst, edgeType)
local edges
if type(rsrc) == 'string' then
edges = S.strings[rsrc] or {}
S.strings[rsrc] = edges
else
edges = rsrc.edges
end
local tp = edges[rdst]
assert(tp and (tp == edgeType)) -- TODO: never tested!
edges[rdst] = nil
end
createTimer = function (msecs, rnow)
msecs = S.now + msecs
local I = 1
for i=#S.timers, 1, -1 do
local t = S.timers[i]
if t.msecs > msecs then
I = i + 1
break
end
end
local ret = {msecs=msecs,rdst=rnow}
t_insert(S.timers, I, ret)
return ret
end
checkTimers = function (dt)
S.now = S.now + dt
while true do
if #S.timers == 0 then break end
local t = S.timers[#S.timers]
if t.msecs > S.now then break end
S.timers[#S.timers] = nil
schedule('dt', 'resume', t.rdst, t.msecs)
end
end
cancelTimer = function (timer)
for i, cur in ipairs(S.timers) do
if timer == cur then
t_remove(S.timers, i)
return
end
end
end
check_create = function (reactor, t)
if not is(reactor) then
assert(type(reactor) == 'function')
reactor = create(reactor, t)
end
return reactor
end
------------------------
-- EXPORTED FUNCTIONS --
------------------------
mt_reactor = {}
cancel = {}
function kill (reactor)
assert(reactor.state == 'awaiting', reactor.state)
reactor.clearAwaiting()
reactor.clearAwaiting = nil
return finish(reactor, cancel)
end
function post (event, value)
local edges = S.strings[event]
if edges then
propagate(event, edges, value)
run()
end
end
function spawn (rdst, param)
rdst = check_create(rdst)
--assert(not rdst.zero)
schedule(S.stack[#S.stack], 'start', rdst, param) -- TODO: nao conferi se eh STACK[#STACK] msm
return rdst
end
function link (rsrc, rdst)
rdst = check_create(rdst, {zero=true})
return addEdge(rsrc, rdst, 'start')
end
function unlink (rsrc, rdst)
return remEdge(rsrc, rdst, 'start')
end
local function _await (edgeType, ...)
local rnow = S.stack[#S.stack]
assert(rnow, 'no reactor running')
assert(not rnow.zero, rnow.name)
local t = { ... }
assert(#t > 0)
for i, v in ipairs(t)
do
if v == 0 then v = 'dt' end
local tp = type(v)
if tp == 'number' then
t[i] = false
if v > 0 then
assert(not rnow.timer)
rnow.timer = createTimer(v, rnow)
else
assert(v == -1)
end
else -- reactor
assert((type(v)=='string') or is(v))--, TRACE())
t[i] = v
addEdge(v, rnow, edgeType)
end
end
-- remove links/timers after resume
rnow.clearAwaiting = function ()
if rnow.timer then
cancelTimer(rnow.timer)
rnow.timer = nil
end
for i, v in ipairs(t) do
if v then
remEdge(v, rnow, edgeType)
end
end
end
return co_yield()
end
function await (...)
return _await('resume', ...)
end
function call (rdst, param, ...)
if rdst.obj and (param == rdst.obj) then
param = ...
end
local ret, rsrc
if rdst.zero then
--ret, rsrc = _await('call', spawn(rdst,param))
ret, rsrc = trigger(S.stack[#S.stack], 'start', rdst, param) -- TODO: nao conferi se eh STACK[#STACK] msm
else
ret, rsrc = _await('call', spawn(rdst,param))
end
run() -- make all pending reactor execute before calling reactor
return ret, rsrc
end
function is (reactor)
return getmetatable(reactor) == mt_reactor
end
function create (body, t) -- {name,obj,zero}
t = t or {}
t.body = assert(type(body)=='function') and body
t.name = t.name or 'anon'
t.edges = {}
t.co = nil
t.state = 'ready' -- ready, awaiting, running, deactivated
t.height = nil
t.timer = nil
t.cEdgeType = nil
t.cParam = nil
t.cNext = nil
return setmetatable(t, mt_reactor)
end
function deactivate (reactor)
assert(reactor.state == 'awaiting')
reactor.state = 'deactivated'
if reactor.timer then
cancelTimer(reactor.timer)
reactor.timer.msecs = reactor.timer.msecs - S.now
end
end
function reactivate (reactor)
assert(reactor.state == 'deactivated')
reactor.state = 'awaiting'
if reactor.timer then
reactor.timer = createTimer(reactor.timer.msecs, reactor)
end
end
function loop (nextEvent, app, param)
app = start(app, param)
while app.state ~= 'ready' do
local evt, param = nextEvent()
step(app, evt, param)
end
end
function start (app, param)
app = check_create(app, {name='main'})
S = {
strings = {},
timers = {},
stack = {},
torun = nil,
now = 0,
dt = nil,
}
SS[app] = S
spawn(app, param)
run()
return app
end
function step (app, evt, param)
--assert(app.state == 'awaiting')
S = assert(SS[app])
if evt == 'dt' then
S.dt = param
checkTimers(param)
run()
end
if evt then
post(evt, param)
end
end