-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdroutine.lua
87 lines (77 loc) · 2.52 KB
/
droutine.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
------------------------------------------------------------
-- Davenge Routine Library ||| written by Daniel R. Koris --
------------------------------------------------------------
-- The droutine's goal is to provide a container for a --
-- lua coroutine. It will allow us to call it as a normal --
-- function, like a wrapped coroutine but will provide --
-- all the functionality of a non-wrapped coroutine. --
------------------------------------------------------------
local DR = {}
-- setup OOP by indexing itself
DR.__index = DR
-- setup __call metamethod, a call to a DRoutine should pass the args in a resume to the thread contained within
DR.__call = function( t, ... )
-- sanity checks
if( not t.thread ) then
print( "Call on DRoutine has no thread." )
return
end
if( coroutine.status( t.thread ) == "dead" ) then
print( "Call on DRoutine with a dead thread." )
return
end
-- make the call, or in this case the resume and return the results
local result = assert( coroutine.resume( t.thread, ... ) )
return result
end
--------------
-- DR:new() --
--------------
-- creating a new container
function DR:new( arg )
-- create table and set its metatable to the library
local dr = {}
setmetatable( dr, self )
if( arg ) then
dr:wrap( arg )
end
return dr
end
-- returns a new DR object
-----------------
-- DR:status() --
-----------------
-- get the status of the contained coroutine
function DR:status()
if( not self.thread ) then
return "none"
end
return coroutine.status( self.thread )
end
-- returns a string
---------------
-- DR:wrap() --
---------------
-- takes either a path on which it will run doFile, a function, or an existing thread
-- most efficient: thread -> function -> path string
function DR:wrap( arg )
local t = type( arg )
-- if its a thread, assign it and leave
if( t ~= "thread" ) then
-- if it's a string, run dofile on it because we assume thats a path
if( t == "string" ) then
arg = dofile( arg )
end
-- because it's another function or something crazy our dofile returned, we need to do some additional magic
-- use of goto to keep things neat and change 2 lines into 1... awesome
t = type( arg )
if( t == "function" ) then
arg = coroutine.create( arg )
elseif( t ~= "thread" ) then
print( "DR:wrap has reached the end of its two steps deep logic and did not end up with a thread." )
return
end
end
self.thread = arg
end
return DR