Skip to content

Commit

Permalink
Updated the table2string method in LuaStepper.c and lStepper.lua. upd…
Browse files Browse the repository at this point in the history
…ated the rockspec
  • Loading branch information
aryajur committed Jun 17, 2016
1 parent 97dcfec commit ca4ab45
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 132 deletions.
159 changes: 78 additions & 81 deletions LuaStepper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 "
Expand Down
2 changes: 1 addition & 1 deletion lStepper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
47 changes: 23 additions & 24 deletions test/demo.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
LuaStepper = require("lStepper")
print("LuaStepper version is:",LuaStepper._VERSION)
lStepper = require("lStepper")
print("lStepper version is:",lStepper._VERSION)

-- Define script 1
script1 = [[
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 = [[
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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:")
Expand All @@ -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)
Loading

0 comments on commit ca4ab45

Please sign in to comment.