@@ -51,20 +51,37 @@ do -- table copy https://gist.github.com/SoniEx2/fc5d3614614e4e3fe131
51
51
-- (or turn it into table.copy(table,deep) where deep is a boolean)
52
52
shallowcopy ,deepcopy = shallow ,deep
53
53
end
54
-
54
+ M . shallowcopy , M . deepcopy = shallowcopy , deepcopy
55
55
local ccEnv = deepcopy (_G )
56
56
57
- local function prepareEnv (ccEnv , eventQueue )
57
+ -- Prepare a _G clone
58
+ M .prepareEnv = function (ccEnv , eventQueue )
58
59
local type ,error ,pcall ,require ,setfenv ,getfenv ,getmetatable = type ,error ,pcall ,require ,setfenv ,getfenv ,getmetatable
59
60
local cyield = coroutine.yield
60
61
local oldenv = getfenv ()
61
62
62
63
-- make it so every new function has ccEnv as the env
63
64
setfenv (1 ,ccEnv )
64
65
65
- eventQueue .n = 0
66
- eventQueue .c = 1
66
+ -- setup eventQueue
67
+ eventQueue .count = 0
68
+ eventQueue .current = 1
69
+ function eventQueue :push (evt ,p1 ,p2 ,p3 ,p4 ,p5 )
70
+ local newcount = self .count + 1
71
+ self [newcount ] = {evt , p1 , p2 , p3 , p4 , p5 }
72
+ self .count = newcount
73
+ end
74
+ function eventQueue :pop ()
75
+ if self .count < self .current then
76
+ return nil
77
+ else
78
+ local current = self .current
79
+ self .current = current + 1
80
+ return self [current ]
81
+ end
82
+ end
67
83
84
+ -- setup environment
68
85
ccEnv .string .dump = nil
69
86
ccEnv .debug = nil
70
87
ccEnv .require = nil
@@ -89,10 +106,9 @@ local function prepareEnv(ccEnv, eventQueue)
89
106
90
107
ccEnv .os .pullEventRaw = cyield
91
108
92
- ccEnv .os .queueEvent = function (n ,a ,b ,c ,d ,e )
93
- local x = eventQueue .n
94
- eventQueue [x ] = {n ,a ,b ,c ,d ,e }
95
- eventQueue .n = x + 1
109
+ ccEnv .os .queueEvent = function (evt ,p1 ,p2 ,p3 ,p4 ,p5 )
110
+ if n == nil then error () end -- todo test this
111
+ eventQueue :push (evt ,p1 ,p2 ,p3 ,p4 ,p5 )
96
112
end
97
113
98
114
ccEnv .os .pullEvent = function (_evt )
@@ -108,7 +124,7 @@ local function prepareEnv(ccEnv, eventQueue)
108
124
109
125
if pcall (require ," socket" ) then
110
126
-- enable HTTP API
111
-
127
+ print ( " ccapi doesn't support HTTP API yet " )
112
128
end
113
129
114
130
setfenv (1 ,oldenv )
117
133
local eventQueue = {}
118
134
prepareEnv (ccEnv , eventQueue )
119
135
136
+ local function scan (t , what , callback )
137
+ local toScan = {t }
138
+ while next (toScan ) do
139
+ local k ,v = next (toScan )
140
+ toScan [k ] = nil
141
+ if type (v ) == " table" then
142
+ for x ,y in pairs (v ) do
143
+ if type (y ) == what then
144
+ y = callback (y )
145
+ end
146
+ if type (y ) == " table" then
147
+ table.insert (toScan , y )
148
+ end
149
+ v [x ] = y
150
+ end
151
+ else
152
+ error (string.format (" Table expected, got %s (%s)" , type (v ), v ))
153
+ end
154
+ end
155
+ end
156
+
120
157
function M .runCC (func , env )
121
- setfenv (func , ccEnv )
122
- -- loop
158
+ scan (env , " function" , function (f )
159
+ return setfenv (function (...) return f (... ) end , env )
160
+ end )
161
+ setfenv (func , env )
162
+ -- main loop
123
163
while true do
124
164
125
165
end
0 commit comments