controller instance bug fix

for loops added
This commit is contained in:
Joachim Stolberg 2018-07-01 12:34:55 +02:00
parent ae426613ea
commit 04f5939055
4 changed files with 54 additions and 7 deletions

@ -28,7 +28,9 @@ safer_lua.DataStructHelp = [[
a.remove(3) --> {1,8,4,7,6}
a.insert(1, "hello") --> {"hello",1,8,4,7,6}
a.size() --> function returns 6
a.memsize() --> return returns 10
a.next() --> for loop iterator function
a.sort(reverse) --> sort the array elements in place
Unlike arrays, which are indexed by a range of numbers,
'stores' are indexed by keys:
@ -39,7 +41,9 @@ safer_lua.DataStructHelp = [[
s.set(0, "hello") --> {val = 12, [0] = "hello"}
s.del("val") --> {[0] = "hello"}
s.size() --> function returns 1
s.memsize() --> function returns 6
s.next() --> for loop iterator function
s.keys(order) --> return an array with the keys
A 'set' is an unordered collection with no duplicate
elements.
@ -51,6 +55,7 @@ safer_lua.DataStructHelp = [[
s.has("Susi") --> function returns `true`
s.has("Mike") --> function returns `false`
s.size() --> function returns 2
s.memsize() --> function returns 8
s.next() --> for loop iterator function
]]
@ -133,6 +138,28 @@ function safer_lua.Store()
if n then return n, Data[n] end
end
end
new_t.keys = function(order)
local keyset = {}
local n = 0
local size = 0
for k,v in pairs(Data) do
n = n + 1
keyset[n] = k
size = size + var_count(k)
end
if order == "up" then
table.sort(keyset, function(a,b) return a > b end)
elseif order == "down" then
table.sort(keyset)
end
local a = safer_lua.Array()
a.__load(size, keyset)
return a
end
new_t.__dump = function()
-- remove the not serializable meta data
return {Type = "Store", Size = Size, MemSize = MemSize, Data = Data}
@ -198,6 +225,14 @@ function safer_lua.Array(...)
return v
end
new_t.sort = function(reverse)
if reverse then
table.sort(Data, function(a,b) return a > b end)
else
table.sort(Data)
end
end
new_t.memsize = function(t)
return MemSize
end

@ -61,7 +61,7 @@ function safer_lua.config(max_code_size, max_table_size)
end
local function compile(pos, text, label, err_clbk)
if safer_lua:check(text, label, err_clbk) == 0 then
if safer_lua:check(pos, text, label, err_clbk) == 0 then
text = text:gsub("%$", "S:")
local code, err = loadstring(text)
if not code then

@ -75,7 +75,6 @@ end
local InvalidKeywords = {
["while"] = true,
["repeat"] = true,
["break"] = true,
["until"] = true,
["for"] = true,
["function"] = true,
@ -90,7 +89,7 @@ local InvalidChars = {
["}"] = true,
}
function safer_lua:check(text, label, err_clbk)
function safer_lua:check(pos, text, label, err_clbk)
local lToken = self:scanner(text)
local lineno = 0
local errno = 0
@ -98,13 +97,18 @@ function safer_lua:check(text, label, err_clbk)
if type(token) == "number" then
lineno = token
elseif InvalidKeywords[token] then
if token ~= "for" or lToken[idx + 3] ~= "in" or
lToken[idx + 5] ~= "next" then -- invalid for statement?
err_clbk(label..lineno..": Invalid keyword '"..token.."'")
if token == "for" then
-- invalid for statement?
if lToken[idx + 3] ~= "in" or lToken[idx + 5] ~= "next" then
err_clbk(pos, label..lineno..": Invalid use of 'for'")
errno = errno + 1
end
else
err_clbk(pos, label..lineno..": Invalid keyword '"..token.."'")
errno = errno + 1
end
elseif InvalidChars[token] then
err_clbk(label..lineno..": Invalid character '"..token.."'")
err_clbk(pos, label..lineno..": Invalid character '"..token.."'")
errno = errno + 1
end
end

@ -106,6 +106,14 @@ sl_controller.register_action("events", {
' e.g. $events(true) -- enable events'
})
sl_controller.register_function("get_ms_time", {
cmnd = function(self)
return math.floor(minetest.get_us_time() / 1000)
end,
help = "$get_ms_time()\n"..
" returns time with millisecond precision."
})
local function formspec0(meta)
local running = meta:get_int("state") == tubelib.RUNNING