Skip to content

Commit e15ed0e

Browse files
committed
Enable BaseEvent:Connect to recieve an AsyncTask or a function. Trigger now assumes AsyncTask if the listener is a table
1 parent 6c4e16c commit e15ed0e

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

LunaCoreRuntime/src/Core/Event.cpp

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,20 @@ void Core::EventHandlerCallback()
131131
static int l_Event_BaseEvent_Connect(lua_State *L)
132132
{
133133
luaL_checktype(L, 1, LUA_TTABLE);
134-
luaL_checktype(L, 2, LUA_TFUNCTION);
134+
if (!lua_isfunction(L, 2)) {
135+
bool valid = false;
136+
if (lua_istable(L, 2)) { // Check for AsyncTask
137+
if (luaL_getmetafield(L, -1, "__name") != LUA_TNIL) {
138+
if (lua_isstring(L, -1)) {
139+
if (std::string(lua_tostring(L, -1)) == "AsyncTask")
140+
valid = true;
141+
}
142+
lua_pop(L, 1);
143+
}
144+
}
145+
if (!valid)
146+
return luaL_error(L, "bad argument #2 to 'Connect' (function or AsyncTask expected, got %s)", luaL_typename(L, 2));
147+
}
135148

136149
lua_getfield(L, 1, "listeners");
137150
if (!lua_istable(L, -1)) {
@@ -176,29 +189,20 @@ static int l_Event_BaseEvent_Trigger(lua_State *L)
176189
for (int i = len; i >= 1; --i) {
177190
lua_rawgeti(L, listenersIdx, i);
178191
if (!lua_isfunction(L, -1)) {
179-
if (lua_istable(L, -1)) { // AsyncTask
180-
if (luaL_getmetafield(L, -1, "__name") != LUA_TNIL) {
181-
if (!lua_isstring(L, -1))
182-
lua_pop(L, 1);
183-
else {
184-
if (std::string(lua_tostring(L, -1)) == "AsyncTask") {
185-
lua_pop(L, 1);
186-
lua_pushstring(L, "_taskF");
187-
lua_rawget(L, -2);
188-
189-
lua_State *co = lua_newthread(L);
190-
lua_pushvalue(L, -2);
191-
lua_xmove(L, co, 1);
192-
193-
lua_sethook(co, Core::_TimeoutAsyncHook, LUA_MASKCOUNT, 100);
194-
195-
Core::Scheduler::getInstance().AddTask(L, -1);
196-
lua_pop(L, 2); // pop co and taskF
197-
}
198-
}
199-
}
192+
if (lua_istable(L, -1)) { // Assume AsyncTask
193+
lua_pushstring(L, "_taskF");
194+
lua_rawget(L, -2);
195+
196+
lua_State *co = lua_newthread(L);
197+
lua_pushvalue(L, -2);
198+
lua_xmove(L, co, 1);
199+
200+
lua_sethook(co, Core::_TimeoutAsyncHook, LUA_MASKCOUNT, 100);
201+
202+
Core::Scheduler::getInstance().AddTask(L, -1);
203+
lua_pop(L, 2); // pop co and taskF
200204
}
201-
lua_pop(L, 1);
205+
lua_pop(L, 1); // pop value
202206
continue;
203207
}
204208

0 commit comments

Comments
 (0)