diff --git a/integration/loveManager/init.lua b/integration/loveManager/init.lua index 472125c..6a26e8d 100644 --- a/integration/loveManager/init.lua +++ b/integration/loveManager/init.lua @@ -9,6 +9,7 @@ THREAD_ID = table.remove(args, 1) THREAD_NAME = table.remove(args, 1) GLOBAL, THREAD = require("multi.integration.loveManager.threads"):init() __FUNC = THREAD.unpackValue(table.remove(args, 1)) +ARGS = THREAD.unpackValue(table.remove(args, 1)) math.randomseed(THREAD_ID) math.random() math.random() @@ -23,7 +24,7 @@ end multi, thread = require("multi"):init() require("multi.integration.loveManager.extensions") require("multi.integration.sharedExtensions") -stab["returns"] = {__FUNC(multi.unpack(args))} +stab["returns"] = {__FUNC(multi.unpack(ARGS))} ]] _G.THREAD_NAME = "MAIN_THREAD" @@ -45,7 +46,7 @@ function multi:newSystemThread(name, func, ...) c.Name = name c.ID = tid c.thread = love.thread.newThread(ThreadFileData) - c.thread:start(c.ID, c.Name, THREAD.packValue(func), ...) + c.thread:start(c.ID, c.Name, THREAD.packValue(func), THREAD.packValue({...})) c.stab = THREAD.createTable(name .. c.ID) c.creationTime = os.clock() c.OnDeath = multi:newConnection() diff --git a/integration/loveManager/threads.lua b/integration/loveManager/threads.lua index a0c6dc1..83ec3da 100644 --- a/integration/loveManager/threads.lua +++ b/integration/loveManager/threads.lua @@ -52,8 +52,7 @@ local function createTable(n) chan:push(packValue(val)) end local function get(name) - local dat = love.thread.getChannel(n .. name):peek() - return unpackValue(dat) + return unpackValue(love.thread.getChannel(n .. name):peek()) end return setmetatable({}, { diff --git a/integration/loveManager/utils.lua b/integration/loveManager/utils.lua index bdfec3d..46a9a91 100644 --- a/integration/loveManager/utils.lua +++ b/integration/loveManager/utils.lua @@ -1,5 +1,5 @@ require("love.data") -local sutils = {} +local utils = {} local NIL = {Type="nil"} --love.data.newByteData("\2"..serpent.dump({t,true},{safe = true})) @@ -13,21 +13,22 @@ local t = function(value) else return v end end -function sutils.pack(tbl, seen) +function utils.pack(tbl, seen) if type(tbl) == "function" then return {["__$FUNC$__"] = love.data.newByteData(string.dump(tbl))} end if type(tbl) ~= "table" then return tbl end local seen = seen or {} local result = {} + result.__isPacked = true for i,v in pairs(tbl) do if seen[v] then result[i] = v elseif t(v) == "table" then seen[v] = true - result[i] = sutils.pack(v, seen) + result[i] = utils.pack(v, seen) elseif t(v) == "function" then result["$F"..i] = love.data.newByteData(string.dump(v)) elseif t{v} == "userdata" then - result[i] = tostring(v) + result[i] = "userdata" else -- Handle what we need to and pass the rest along as a value result[i] = v end @@ -35,7 +36,8 @@ function sutils.pack(tbl, seen) return result end -function sutils.unpack(tbl) +function utils.unpack(tbl) + if not tbl then return nil end if type(tbl) ~= "table" then return tbl end if tbl["__$FUNC$__"] then return loadstring(tbl["__$FUNC$__"]:getString()) end for i,v in pairs(tbl) do @@ -46,10 +48,11 @@ function sutils.unpack(tbl) tbl[i:sub(3,-1)] = loadstring(rawfunc) end if type(v) == "table" then - sutils.unpack(v) + utils.unpack(v) end end + tbl.__isPacked = nil return tbl end -return sutils \ No newline at end of file +return utils \ No newline at end of file diff --git a/lovethreads/main.lua b/lovethreads/main.lua index 3b0434f..8936ea3 100644 --- a/lovethreads/main.lua +++ b/lovethreads/main.lua @@ -1,35 +1,15 @@ package.path = "../?/init.lua;../?.lua;"..package.path local multi, thread = require("multi"):init{print=true, warning = true, error=true} -local flat = require("flatten") +local utils = require("multi.integration.loveManager.utils") -local people = { - { - name = "Fred", - address = "16 Long Street", - phone = "123456" - }, - { - name = "Wilma", - address = "16 Long Street", - phone = "123456", - func = function() - print("Hi") - end - }, - { - name = "Barney", - address = "17 Long Street", - phone = "123457", - important = love.data.newByteData("TEST") - } - } +local people = {1,2,3} function dump(o) if type(o) == 'table' then local s = '{ ' for k,v in pairs(o) do if type(k) ~= 'number' then k = '"'..k..'"' end - s = s .. '['..k..'] = ' .. dump(v) .. ',' + s = s .. '['..k..'] = ' .. dump(v) .. '('..type(v):sub(1,1)..'),' end return s .. '} ' else @@ -37,13 +17,14 @@ function dump(o) end end -local fpeople = flat.flatten(people) +local fpeople = utils.pack(people) -print("Flatten", dump(fpeople)) +print("Pack:", dump(fpeople)) -local people = flat.unflatten(fpeople) +local people = utils.unpack(fpeople) -print("Unflatten", dump(people)) +print("Unpack:", dump(people)) +print(type(people[3])) -- GLOBAL, THREAD = require("multi.integration.loveManager"):init() diff --git a/tests/threadtests.lua b/tests/threadtests.lua index f6acf67..f646153 100644 --- a/tests/threadtests.lua +++ b/tests/threadtests.lua @@ -55,6 +55,9 @@ multi:newThread("Scheduler Thread",function() multi_assert("Passing some args", a, "First argument is not as expected 'Passing some args'") multi_assert(true, e, "Argument e is not true!") multi_assert("table", type(f), "Argument f is not a table!") + for i,v in pairs(queue) do + print("Queue:",i,v) + end queue:push("done") end,"Passing some args", 1, 2, 3, true, {"Table"}).OnError(function(self,err) multi.error(err) @@ -90,14 +93,14 @@ multi:newThread("Scheduler Thread",function() local worked = false multi:newSystemThread("testing tables",function() - tab=THREAD.waitFor("YO"):init() + tab=THREAD.waitFor("YO") THREAD.hold(function() return tab["test1"] end) THREAD.sleep(.1) tab["test2"] = "Whats so funny?" end).OnError(multi.error) multi:newThread("test2",function() - thread.hold(function() return test["test2"] end) + print(thread.hold(function() return test["test2"] end)) worked = true end)