diff --git a/changes.md b/changes.md index 7c00a15..4a72efd 100644 --- a/changes.md +++ b/changes.md @@ -73,6 +73,10 @@ Going Forward: --- - There is no longer any plans for sterilization! Functions do not play nice on different platforms and there is no simple way to ensure that things work. +Quality Of Life: +--- +- threaded functions now return only the arguments that are needed, if it has trailing nils, they wont be returned like they used to. + Added: --- - Type: destroyed diff --git a/multi/init.lua b/multi/init.lua index ba3c7b8..387e185 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -1090,6 +1090,18 @@ function multi.holdFor(n,func) end end) end +local function cleanReturns(...) + local n = select("#", ...) + local returns = {...} + local rets = {} + local ind = 0 + for i=n,1,-1 do + if returns[i] then + ind=i + end + end + return unpack(returns,1,ind) +end function thread:newFunction(func,holdme) return function(...) local rets, err @@ -1099,7 +1111,7 @@ function thread:newFunction(func,holdme) if err then return multi.NIL, err elseif rets then - return (rets[1] or multi.NIL),rets[2],rets[3],rets[4],rets[5],rets[6],rets[7] + return cleanReturns((rets[1] or multi.NIL),rets[2],rets[3],rets[4],rets[5],rets[6],rets[7]) end end) else @@ -1109,11 +1121,11 @@ function thread:newFunction(func,holdme) if err then return nil,err end - return rets[1],rets[2],rets[3],rets[4],rets[5],rets[6],rets[7] + return cleanReturns(rets[1],rets[2],rets[3],rets[4],rets[5],rets[6],rets[7]) end end local t = multi:newThread("TempThread",func,...) - t.OnDeath(function(self,status,a1,a2,a3,a4,a5,a6,a7) rets = {a1,a2,a3,a4,a5,a6,a7} end) + t.OnDeath(function(self,status,...) rets = {...} end) t.OnError(function(self,e) err = e end) if holdme then return wait() @@ -1122,7 +1134,7 @@ function thread:newFunction(func,holdme) isTFunc = true, wait = wait, connect = function(f) - t.OnDeath(function(self,status,...) f(...) end) + t.OnDeath(function(self,status,...) f(cleanReturns(...)) end) t.OnError(function(self,err) f(err) end) end } diff --git a/test.lua b/test.lua index 37f7401..58ab214 100644 --- a/test.lua +++ b/test.lua @@ -1,14 +1,23 @@ package.path="?.lua;?/init.lua;?.lua;?/?/init.lua;"..package.path multi, thread = require("multi"):init() a=0 +local function cleanReturns(...) + local n = select("#", ...) + print(n) + local returns = {...} + local rets = {} + local ind = 0 + for i=n,1,-1 do + if returns[i] then + ind=i + end + end + return unpack(returns,1,ind) +end func = thread:newFunction(function() - print(thread.holdFor(3,function() - return a==5 - end)) - print(thread.hold(function() - return multi.NIL,"test" - end)) -end,true) -- Tell the code to wait and then return -a,b = func() -print(a,b) -multi:lightloop() \ No newline at end of file + return thread.holdFor(3,function() + return a==5 -- Condition being tested! + end) +end,true) +print(func()) +--multi:lightloop() \ No newline at end of file