-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
89 lines (80 loc) · 2.05 KB
/
init.lua
File metadata and controls
89 lines (80 loc) · 2.05 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
local coroutine = require('coroutine')
local table = require('table')
local exports = {}
local SENTINEL = {}
exports.SENTINEL = SENTINEL
exports.yield = coroutine.yield
local _unpack = function(...)
-- this function is necessary because of the way lua
--handles nil within tables and in unpack
local args = {...}
local coro_status = false
local next_call = nil
local extras = {}
local extra_length = 0
for k,v in pairs(args) do
if k == 1 then
coro_status = v
elseif k == 2 then
next_call = v
else
extras[k-2] = v
extra_length = k - 2
end
end
return coro_status, next_call, extras, extra_length
end
local __inline_callbacks
__inline_callbacks = function(coro, cb, ...)
local v = ...
local previous = nil
local no_errs = true
local extra_args = {}
local length = 0
while true do
previous = v
if coroutine.status(coro) == 'dead' then
-- todo- pcall this and shove the result into the second argument or return an error or something
if not cb then
return
end
if type(previous) ~= 'table' then
return cb(previous)
else
return cb(unpack(previous))
end
end
-- yielded a function...
if type(v) == 'function' then
-- add a callback that will invoke coro
local f = function(...)
-- we resume ourselves later
__inline_callbacks(coro, cb, ...)
end
-- replace the sentinel if it exists, with the function
if extra_args[length] == SENTINEL then
extra_args[length] = f
else
length = length + 1
extra_args[length] = f
end
return v(unpack(extra_args, 1, length))
end
no_errs, v, extra_args, length = _unpack(coroutine.resume(coro, v))
-- donegoofed?
if no_errs ~= true then
if cb then
return cb(v)
else
return error(no_errs)
end
end
end
end
exports.inline_callbacks = function(f)
local coro = coroutine.create(f)
return function(cb, ...)
return __inline_callbacks(coro, cb, ...)
end
end
return exports