diff --git a/multi/init.lua b/multi/init.lua index 815029f..5f7b253 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -1481,7 +1481,7 @@ function thread:newFunction(func) local t = multi:newThread("TempThread",func,...) t.OnDeath(function(self,status,...) rets = {...} end) t.OnError(function(self,e) err = e end) - return { + local temp = { isTFunc = true, wait = wait, connect = function(f) @@ -1489,6 +1489,7 @@ function thread:newFunction(func) t.OnError(function(self,err) f(self, err) end) end } + return temp,temp,temp,temp,temp,temp,temp end setmetatable(c,c) return c @@ -1545,7 +1546,28 @@ function multi:newThread(name,func,...) if type(v)=="function" then rawset(t,k,thread:newFunction(v)) else - Gref[k]=v + if type(v)=="table" then + if v.isTFunc then + if not _G["_stack_"] or #_G["_stack_"]==0 then + _G["_stack_"] = {} + local s = _G["_stack_"] + local a,b,c,d,e,f,g = v.wait() + table.insert(s,a) + table.insert(s,b) + table.insert(s,c) + table.insert(s,d) + table.insert(s,e) + table.insert(s,f) + Gref[k]=table.remove(_G["_stack_"]) + else + Gref[k]=table.remove(_G["_stack_"]) + end + else + Gref[k]=v + end + else + Gref[k]=v + end end end }) diff --git a/test.lua b/test.lua index e784dc7..338ea58 100644 --- a/test.lua +++ b/test.lua @@ -1,7 +1,19 @@ package.path="?/init.lua;?.lua;"..package.path multi,thread = require("multi"):init() -multi:scheduleJob({min = 15, hour = 14},function() - print("hi!") +multi:newThread(function() + function test() + thread.sleep(1) + return 1,2 + end + -- This returns instantly even though the function isn't done! + test().connect(function(...) + print(...) + end) + print("Done") + -- This waits for the returns since we are demanding them + a,b = test() + print(a,b) + os.exit() end) --min,hour,day,wday,month multi:mainloop()