revised callback system to use multiple arguments; added tobitstring function in utility

This commit is contained in:
Redo 2019-05-10 21:07:35 -05:00
parent 10611449bf
commit d2a648f7d9
3 changed files with 17 additions and 5 deletions

View File

@ -32,8 +32,8 @@ end
-- sim:queuecallback(self, str) -- sim:queuecallback(self, str)
-- end -- end
function Gate:cb(str) function Gate:cb(...)
sim:queuecallback(self, str) sim:queuecallback(self, ...)
end end
function Gate:queue(delay) function Gate:queue(delay)

View File

@ -328,13 +328,11 @@ function Simulation:sendcallbacks()
local data = "CB" local data = "CB"
for objref, args in pairs(self.callbacks) do for objref, args in pairs(self.callbacks) do
local argc = 0
local escargs = {} local escargs = {}
for argidx, argv in ipairs(args) do for argidx, argv in ipairs(args) do
table.insert(escargs, expandescape(tostring(argv))) table.insert(escargs, expandescape(tostring(argv)))
argc = argc+1
end end
data = data .. "\t" .. objref .. "\t"..argc..(#escargs>0 and ("\t"..table.concat(escargs, "\t")) or "") data = data .. "\t" .. objref .. "\t"..(#escargs)..(#escargs>0 and ("\t"..table.concat(escargs, "\t")) or "")
end end
client:send(data .. "\n") client:send(data .. "\n")

View File

@ -51,3 +51,17 @@ function collapseescape(str)
return table.concat(ostrt) return table.concat(ostrt)
end end
function tobitstring(num, len)
local maxval = bit.lshift(1, len)
if num>=maxval then error("bitstring value too big") end
num = num%maxval
local bitstring = ""
for i = len, 1, -1 do
bitstring = bitstring..bit.rshift(bit.band(num, bit.lshift(1, i-1)), i-1)
end
return bitstring
end