Posted 07 February 2016 - 07:50 PM
Rewriting this to detail the original problem and what the solution was for anyone else who would have this problem the following is the now fixed code.
originally adding any functions with the add function would crash the computer this was because i had originally written my custom stateless iterator (see fIter) like this
I am not sure why i put that +1 in there or why i didnt catch it but that was causing the problem. Basically every iteration was causing the function to subtract one but return 1 higher and that first return is how for loops determine what to pass next to a stateless iterator. This caused an infinite loop with no sleep and since I was working within os.pullEvent instead of simply saying program didnt yield it caused the computer to reboot.
local function fIter (t, i)
i = not i and #t or (i - 1)
if i>0 then
return i, t[i]
end
end
local nxt = next
local tBackground = setmetatable({},{
__newindex = function(t,k,v)
v = {cRoutine=coroutine.create(v[1]),sFilter=nil,sIdentifier=v[2]}
rawset(t,k,v)
end;
})
function add(fFunc,sIdentifier)
tBackground[#tBackground+1] = {fFunc,sIdentifier}
return tBackground[#tBackground]
end
function rem(cRoutine)
cRoutine.sFilter="Daemon_internal_remove"
end
function getBackground()
return tBackground
end
local bPullMeta = false
function os.pullEventRaw(sEvent)
local tData
if bPullMeta then
return coroutine.yield(sEvent)
else
repeat
tData = {coroutine.yield()}
for iIndex,cRoutine in fIter, tBackground do
if cRoutine.sFilter==tData[1] or not cRoutine.sFilter then
bPullMeta = true
local bOk, sInnerEvent = coroutine.resume(cRoutine.cRoutine,unpack(tData))
bPullMeta = false
if bOk then
cRoutine.sFilter = sInnerEvent
else
table.remove(tBackground,iIndex)
end
elseif cRoutine.sFilter=="Daemon_internal_remove" then
table.remove(tBackground,iIndex)
end
end
until tData[1] == sEvent or not sEvent
return unpack(tData)
end
end
originally adding any functions with the add function would crash the computer this was because i had originally written my custom stateless iterator (see fIter) like this
local function fIter (t, i)
i = not i and #t or (i - 1)
if i>0 then
return i+1, t[i]
end
end
I am not sure why i put that +1 in there or why i didnt catch it but that was causing the problem. Basically every iteration was causing the function to subtract one but return 1 higher and that first return is how for loops determine what to pass next to a stateless iterator. This caused an infinite loop with no sleep and since I was working within os.pullEvent instead of simply saying program didnt yield it caused the computer to reboot.
Edited on 07 February 2016 - 09:54 PM