-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinit.lua
More file actions
70 lines (59 loc) · 1.58 KB
/
Copy pathinit.lua
File metadata and controls
70 lines (59 loc) · 1.58 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
-- This script is executed to initialize the scripting environment
-- after declaring the labrea object and registering all C functions.
--
-- Begin internals.
--
-- If exit handlers are used, the table for them must be created
labrea["exit_handlers"] = {}
labrea["periodic_queue"] = {}
labrea["current_timestamp"] = 0
labrea["available_hooks"] = {}
-- At init time, this function is called for each available hook
-- point.
function labrea_record_function(fname)
table.insert(labrea.available_hooks, fname)
end
-- This function is called immediately before the process exits.
function labrea_exiting()
for k,f in pairs(labrea.exit_handlers) do f() end
end
-- This function is called once a second.
function labrea_periodic(t)
labrea.current_timestamp = t
for k,funs in pairs(labrea.periodic_queue) do
if t >= k then
for x,f in pairs(funs) do
f()
end
labrea.periodic_queue[k] = nil
end
end
end
--
-- End of internals.
--
--
-- API provided to lua scripts.
--
function labrea.periodic(period, f)
labrea.schedule(period, function()
f()
labrea.periodic(period, f)
end)
end
function labrea.atexit(f)
table.insert(labrea.exit_handlers, f)
end
function labrea.schedule(t, f)
local tab = labrea.periodic_queue
t = t + labrea.current_timestamp
if not tab[t] then
tab[t] = {}
end
table.insert(tab[t], f)
end
-- When we're ready, we'll do this.
function labrea_load_user_script()
dofile(os.getenv("LABREA_SCRIPT"))
labrea.reinit()
end