From ca4ab45a658fd731235897bae22591affad9cc93 Mon Sep 17 00:00:00 2001 From: aryajur Date: Fri, 17 Jun 2016 16:43:57 -0700 Subject: [PATCH] Updated the table2string method in LuaStepper.c and lStepper.lua. updated the rockspec --- LuaStepper.c | 159 +++++++++--------- lStepper.lua | 2 +- ...ckspec => luastepper-1.16.06.17-1.rockspec | 6 +- test/demo.lua | 47 +++--- test/test1.lua | 46 ++--- 5 files changed, 128 insertions(+), 132 deletions(-) rename luastepper-1.14.07.12-1.rockspec => luastepper-1.16.06.17-1.rockspec (55%) diff --git a/LuaStepper.c b/LuaStepper.c index eac012e..d514ae7 100644 --- a/LuaStepper.c +++ b/LuaStepper.c @@ -29,7 +29,7 @@ #define urgentTasks 2 #define LS_DEFSTEPS 10 -#define VERSION "1.14.07.12" +#define VERSION "1.16.06.17" struct Task { @@ -374,86 +374,83 @@ static int addTask(lua_State *L) msg = "_LS={} " "function _LS.tableToString(t) " - " if type(t) ~= 'table' then return nil, 'Expected table parameter' end " - " local rL = {cL = 1} " - " rL[rL.cL] = {} " - " local tabIndex = {} " - " local latestTab = 0 " - " do " - " rL[rL.cL]._f,rL[rL.cL]._s,rL[rL.cL]._var = pairs(t) " - " rL[rL.cL].str = 't0={}' " - " rL[rL.cL].t = t " - " rL[rL.cL].tabIndex = 0 " - " tabIndex[t] = rL[rL.cL].tabIndex " - " while true do " - " local key " - " local k,v = rL[rL.cL]._f(rL[rL.cL]._s,rL[rL.cL]._var) " - " rL[rL.cL]._var = k " - " if not k and rL.cL == 1 then " - " break " - " elseif not k then " - " rL[rL.cL-1].str = rL[rL.cL-1].str..'\\n'..rL[rL.cL].str " - " rL.cL = rL.cL - 1 " - " if rL[rL.cL].vNotDone then " - " key = 't'..rL[rL.cL].tabIndex..'[t'..tostring(rL[rL.cL+1].tabIndex)..']' " - " rL[rL.cL].str = rL[rL.cL].str..'\\n'..key..'=' " - " v = rL[rL.cL].vNotDone " - " end " - " rL[rL.cL+1] = nil " - " else " - " if type(k) == 'number' then " - " key = 't'..rL[rL.cL].tabIndex..'['..tostring(k)..']' " - " rL[rL.cL].str = rL[rL.cL].str..'\\n'..key..'=' " - " elseif type(k) == 'string' then " - " key = 't'..rL[rL.cL].tabIndex..'.'..tostring(k) " - " rL[rL.cL].str = rL[rL.cL].str..'\\n'..key..'=' " - " else " - " if tabIndex[k] then " - " key = 't'..rL[rL.cL].tabIndex..'[t'..tabIndex[k]..']' " - " rL[rL.cL].str = rL[rL.cL].str..'\\n'..key..'=' " - " else " - " latestTab = latestTab + 1 " - " rL[rL.cL].str = rL[rL.cL].str..'\\nt'..tostring(latestTab)..'={}' " - " rL[rL.cL].vNotDone = v " - " rL.cL = rL.cL + 1 " - " rL[rL.cL] = {} " - " rL[rL.cL]._f,rL[rL.cL]._s,rL[rL.cL]._var = pairs(k) " - " rL[rL.cL].tabIndex = latestTab " - " rL[rL.cL].t = k " - " rL[rL.cL].str = '' " - " tabIndex[k] = rL[rL.cL].tabIndex " - " end " - " end " - " end " - " if key then " - " rL[rL.cL].vNotDone = nil " - " if type(v) == 'table' then " - " if tabIndex[v] then " - " rL[rL.cL].str = rL[rL.cL].str..'t'..tabIndex[v] " - " else " - " latestTab = latestTab + 1 " - " rL[rL.cL].str = rL[rL.cL].str..'{}' " - " rL[rL.cL].str = rL[rL.cL].str..'\\nt'..tostring(latestTab)..'='..key " - " rL.cL = rL.cL + 1 " - " rL[rL.cL] = {} " - " rL[rL.cL]._f,rL[rL.cL]._s,rL[rL.cL]._var = pairs(v) " - " rL[rL.cL].tabIndex = latestTab " - " rL[rL.cL].t = v " - " rL[rL.cL].str = '' " - " tabIndex[v] = rL[rL.cL].tabIndex " - " end " - " elseif type(v) == 'number' then " - " rL[rL.cL].str = rL[rL.cL].str..tostring(v) " - " elseif type(v) == 'boolean' then " - " rL[rL.cL].str = rL[rL.cL].str..tostring(v) " - " else " - " rL[rL.cL].str = rL[rL.cL].str..string.format('%q',tostring(v)) " - " end " - " end " - " end " - " end " - " return rL[rL.cL].str " - "end " + " if type(t) ~= 'table' then return nil, 'Expected table parameter' end " + " local rL = {cL = 1} " + " rL[rL.cL] = {} " + " local tabIndex = {} " + " local latestTab = 0 " + " local result = {} " + " do " + " rL[rL.cL]._f,rL[rL.cL]._s,rL[rL.cL]._var = pairs(t) " + " result[#result + 1] = 't0={}' " + " rL[rL.cL].t = t " + " rL[rL.cL].tabIndex = 0 " + " tabIndex[t] = rL[rL.cL].tabIndex " + " while true do " + " local key " + " local k,v = rL[rL.cL]._f(rL[rL.cL]._s,rL[rL.cL]._var) " + " rL[rL.cL]._var = k " + " if not k and rL.cL == 1 then " + " break " + " elseif not k then " + " rL.cL = rL.cL - 1 " + " if rL[rL.cL].vNotDone then " + " key = 't'..rL[rL.cL].tabIndex..'[t'..tostring(rL[rL.cL+1].tabIndex)..']' " + " result[#result + 1] = '\\n'..key..'=' " + " v = rL[rL.cL].vNotDone " + " end " + " rL[rL.cL+1] = nil " + " else " + " if type(k) == 'number' then " + " key = 't'..rL[rL.cL].tabIndex..'['..tostring(k)..']' " + " result[#result + 1] = '\\n'..key..'=' " + " elseif type(k) == 'string' then " + " key = 't'..rL[rL.cL].tabIndex..'.'..tostring(k) " + " result[#result + 1] = '\\n'..key..'=' " + " else " + " if tabIndex[k] then " + " key = 't'..rL[rL.cL].tabIndex..'[t'..tabIndex[k]..']' " + " result[#result + 1] = '\\n'..key..'=' " + " else " + " latestTab = latestTab + 1 " + " result[#result + 1] = '\\nt'..tostring(latestTab)..'={}' " + " rL[rL.cL].vNotDone = v " + " rL.cL = rL.cL + 1 " + " rL[rL.cL] = {} " + " rL[rL.cL]._f,rL[rL.cL]._s,rL[rL.cL]._var = pairs(k) " + " rL[rL.cL].tabIndex = latestTab " + " rL[rL.cL].t = k " + " tabIndex[k] = rL[rL.cL].tabIndex " + " end " + " end " + " end " + " if key then " + " rL[rL.cL].vNotDone = nil " + " if type(v) == 'table' then " + " if tabIndex[v] then " + " result[#result + 1] = 't'..tabIndex[v] " + " else " + " latestTab = latestTab + 1 " + " result[#result + 1] = '{}\nt'..tostring(latestTab)..'='..key " + " rL.cL = rL.cL + 1 " + " rL[rL.cL] = {} " + " rL[rL.cL]._f,rL[rL.cL]._s,rL[rL.cL]._var = pairs(v) " + " rL[rL.cL].tabIndex = latestTab " + " rL[rL.cL].t = v " + " tabIndex[v] = rL[rL.cL].tabIndex " + " end " + " elseif type(v) == 'number' then " + " result[#result + 1] = tostring(v) " + " elseif type(v) == 'boolean' then " + " result[#result + 1] = tostring(v) " + " else " + " result[#result + 1] = string.format('%q',tostring(v)) " + " end " + " end " + " end " + " end " + " return table.concat(result) " + "end " "do" " local pack = package " " local req = require " diff --git a/lStepper.lua b/lStepper.lua index 9926892..343fffb 100644 --- a/lStepper.lua +++ b/lStepper.lua @@ -13,7 +13,7 @@ local string = string local package = package -local ver = "1.14.07.12" -- Should correspond to the version of the C module +local ver = "1.16.06.17" -- Should correspond to the version of the C module -- Create the module table here local M = {} diff --git a/luastepper-1.14.07.12-1.rockspec b/luastepper-1.16.06.17-1.rockspec similarity index 55% rename from luastepper-1.14.07.12-1.rockspec rename to luastepper-1.16.06.17-1.rockspec index 37a8732..eb3313b 100644 --- a/luastepper-1.14.07.12-1.rockspec +++ b/luastepper-1.16.06.17-1.rockspec @@ -1,13 +1,13 @@ package = "LuaStepper" -version = "1.14.07.12-1" +version = "1.16.06.17-1" source = { url = "git://github.com/aryajur/LuaStepper.git", - tag = "1.14.07" + tag = "1.16.06" } description = { summary = "Allow Lua scripts to run multiple Lua scripts in parallel, platform independent without using threading.", detailed = [[ - C module to allow a Lua state to run multiple Lua scripts in parallel. This is platform independent and will work wherever Lua works. LuaStepper is a module to help lua programs control simultaneous execution of multiple Lua threads without them needing to be coroutines. + LuaStepper is a module to help lua programs control simultaneous execution of multiple Lua threads without them needing to be coroutines. So it is like pre-emptive multitasking but instead of the operating system controlling the task switch and time slicing the Lua parent process does that with the API of this module. ]], homepage = "http://www.amved.com/milindsweb/LuaStepper.html", license = "MIT" diff --git a/test/demo.lua b/test/demo.lua index 9e38533..8826bf2 100644 --- a/test/demo.lua +++ b/test/demo.lua @@ -1,5 +1,5 @@ -LuaStepper = require("lStepper") -print("LuaStepper version is:",LuaStepper._VERSION) +lStepper = require("lStepper") +print("lStepper version is:",lStepper._VERSION) -- Define script 1 script1 = [[ @@ -7,12 +7,12 @@ while true do print('A') end]] -- Add script 1 to the queue with some print statements that are executed immediately -print("Add script 1. ID is -->",LuaStepper.addTask(script1,"print('Initialization step 1') print('Initialization step 2')")) +print("Add script 1. ID is -->",lStepper.addTask(script1,"print('Initialization step 1') print('Initialization step 2')")) -- Define script 2 script2 = "while true do print('B') end" -- Add script 2 to the queue with some print statements that are executed immediately -print("Add script 2. ID is -->",LuaStepper.addTask(script2,"print('Initialization step x') print('Initialization step y')")) +print("Add script 2. ID is -->",lStepper.addTask(script2,"print('Initialization step x') print('Initialization step y')")) -- Define script 3 script3 = [[ @@ -34,13 +34,12 @@ script3 = [[ end end]] -- Add script 3 to the queue with a print statement initialization and a variable setting that is then printed in the script -print("Add script 3. ID is -->",LuaStepper.addTask(script3,"print('Adding Script 3') test5='script3'",5,true)) -- 5 means the number os codes to execute for each step of this script +print("Add script 3. ID is -->",lStepper.addTask(script3,"print('Adding Script 3') test5='script3'",5,true)) -- 5 means the number os codes to execute for each step of this script -- This script is also placed in the urgent task pile by setting true - -print("Number of tasks=",LuaStepper.getNumOfTasks()) -- displays 2 1 -- 2 regular 1 urgent +print("Number of tasks=",lStepper.getNumOfTasks()) -- displays 2 1 -- 2 regular 1 urgent print("Start stepper loop") for i=1,50 do - x,y=LuaStepper.runLoop() -- Run all the tasks for 1 step + x,y=lStepper.runLoop() -- Run all the tasks for 1 step if not x then print(y) break @@ -50,26 +49,26 @@ for i=1,50 do break end if i==5 then - print("i=5, script 1 status is ",LuaStepper.taskStatus(0)) - print("i=5, script 2 status is ",LuaStepper.taskStatus(1)) + print("i=5, script 1 status is ",lStepper.taskStatus(0)) + print("i=5, script 2 status is ",lStepper.taskStatus(1)) end if i==30 then - print("i=30, script 1 status is ",LuaStepper.taskStatus(0)) - print("i=30, script 2 status is ",LuaStepper.taskStatus(1)) + print("i=30, script 1 status is ",lStepper.taskStatus(0)) + print("i=30, script 2 status is ",lStepper.taskStatus(1)) end if i==20 then print("B suspended") - LuaStepper.suspendTask(1) + lStepper.suspendTask(1) end if i==40 then print("B resumed") - LuaStepper.resumeTask(1) + lStepper.resumeTask(1) end if i==5 then - x,y = LuaStepper.getTaskData(4,"test3") -- Get the value of test3 from script 3 + x,y = lStepper.getTaskData(4,"test3") -- Get the value of test3 from script 3 if x then print("Value from script3 test3=",x) - print(LuaStepper.setTaskData(4,{test4=x})) -- Set value of test4 to be same as test3 in script 3 + print(lStepper.setTaskData(4,{test4=x})) -- Set value of test4 to be same as test3 in script 3 else print("Error getting data:",y) end @@ -78,8 +77,8 @@ for i=1,50 do local t = { test6='Added Table!' } - print(LuaStepper.setTaskData(4,{test5=t})) - x=LuaStepper.getTaskData(4,"test10") + print(lStepper.setTaskData(4,{test5=t})) + x=lStepper.getTaskData(4,"test10") print(x) if type(x) == 'table' then print("test10 in script 3 is a table:") @@ -90,9 +89,9 @@ for i=1,50 do end end -print("END",LuaStepper.taskStatus(0)) -print("END",LuaStepper.taskStatus(1)) -print("END",LuaStepper.taskStatus(4)) -LuaStepper.closeTask(0) -LuaStepper.closeTask(1) -LuaStepper.closeTask(4) +print("END",lStepper.taskStatus(0)) +print("END",lStepper.taskStatus(1)) +print("END",lStepper.taskStatus(4)) +lStepper.closeTask(0) +lStepper.closeTask(1) +lStepper.closeTask(4) diff --git a/test/test1.lua b/test/test1.lua index 0cfbda8..356e60f 100644 --- a/test/test1.lua +++ b/test/test1.lua @@ -1,5 +1,5 @@ -LuaStepper = require("lStepper") -print("LuaStepper version is:",LuaStepper._VERSION) +lStepper = require("lStepper") +print("lStepper version is:",lStepper._VERSION) --script1 = "for i=1,10 do print('Script 1 i:',i) end" @@ -42,12 +42,12 @@ while true do coroutine.resume(c) end]] --script1 = "print('I am done')" -print(LuaStepper.addTask(script1,"print('Initialization step 1') print('Initialization step 2')")) +print(lStepper.addTask(script1,"print('Initialization step 1') print('Initialization step 2')")) --script2 = "for i=1,20 do print('Script 2 i:',i) end" script2 = "while true do print('C') end" --script2 = "print('I am done')" -print(LuaStepper.addTask(script2,"print('Initialization step x') print('Initialization step y')")) +print(lStepper.addTask(script2,"print('Initialization step x') print('Initialization step y')")) script3 = [[ test1='Hello' @@ -67,13 +67,13 @@ script3 = [[ break end end]] -print(LuaStepper.addTask(script3,"print('Adding Script 3')",5,true)) +print(lStepper.addTask(script3,"print('Adding Script 3')",5,true)) -print(LuaStepper.getNumOfTasks()) +print(lStepper.getNumOfTasks()) print("Start stepper loop") --while(true) do for i=1,50 do - x,y=LuaStepper.runLoop() + x,y=lStepper.runLoop() --print(x[0][1],x[0][2],x[1][1],x[1][2]) if not x then print(y) @@ -84,26 +84,26 @@ for i=1,50 do break end if i==5 then - print("i=5",LuaStepper.taskStatus(0)) - print("i=5",LuaStepper.taskStatus(1)) + print("i=5",lStepper.taskStatus(0)) + print("i=5",lStepper.taskStatus(1)) end if i==30 then - print("i=30",LuaStepper.taskStatus(0)) - print("i=30",LuaStepper.taskStatus(1)) + print("i=30",lStepper.taskStatus(0)) + print("i=30",lStepper.taskStatus(1)) end if i==20 then print("C suspended") - LuaStepper.suspendTask(1) + lStepper.suspendTask(1) end if i==40 then print("C resumed") - LuaStepper.resumeTask(1) + lStepper.resumeTask(1) end if i==5 then - x,y = LuaStepper.getTaskData(4,"test3") + x,y = lStepper.getTaskData(4,"test3") if x then print("Value from script3 test3=",x) - print(LuaStepper.setTaskData(4,{test4=x})) + print(lStepper.setTaskData(4,{test4=x})) else print("Error getting data:",y) end @@ -112,8 +112,8 @@ for i=1,50 do local t = { test6='Added Table!' } - print(LuaStepper.setTaskData(4,{test5=t})) - x=LuaStepper.getTaskData(4,"test10") + print(lStepper.setTaskData(4,{test5=t})) + x=lStepper.getTaskData(4,"test10") print(x) if type(x) == 'table' then print("test10 in script 3 is a table:") @@ -124,9 +124,9 @@ for i=1,50 do end end -print("END",LuaStepper.taskStatus(0)) -print("END",LuaStepper.taskStatus(1)) -print("END",LuaStepper.taskStatus(4)) -LuaStepper.closeTask(0) -LuaStepper.closeTask(1) -LuaStepper.closeTask(4) +print("END",lStepper.taskStatus(0)) +print("END",lStepper.taskStatus(1)) +print("END",lStepper.taskStatus(4)) +lStepper.closeTask(0) +lStepper.closeTask(1) +lStepper.closeTask(4)