mirror of
https://github.com/joe7575/techpack.git
synced 2024-08-21 13:44:13 +02:00
controller instance bug fix
for loops added
This commit is contained in:
parent
ae426613ea
commit
04f5939055
@ -28,7 +28,9 @@ safer_lua.DataStructHelp = [[
|
|||||||
a.remove(3) --> {1,8,4,7,6}
|
a.remove(3) --> {1,8,4,7,6}
|
||||||
a.insert(1, "hello") --> {"hello",1,8,4,7,6}
|
a.insert(1, "hello") --> {"hello",1,8,4,7,6}
|
||||||
a.size() --> function returns 6
|
a.size() --> function returns 6
|
||||||
|
a.memsize() --> return returns 10
|
||||||
a.next() --> for loop iterator function
|
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,
|
Unlike arrays, which are indexed by a range of numbers,
|
||||||
'stores' are indexed by keys:
|
'stores' are indexed by keys:
|
||||||
@ -39,7 +41,9 @@ safer_lua.DataStructHelp = [[
|
|||||||
s.set(0, "hello") --> {val = 12, [0] = "hello"}
|
s.set(0, "hello") --> {val = 12, [0] = "hello"}
|
||||||
s.del("val") --> {[0] = "hello"}
|
s.del("val") --> {[0] = "hello"}
|
||||||
s.size() --> function returns 1
|
s.size() --> function returns 1
|
||||||
|
s.memsize() --> function returns 6
|
||||||
s.next() --> for loop iterator function
|
s.next() --> for loop iterator function
|
||||||
|
s.keys(order) --> return an array with the keys
|
||||||
|
|
||||||
A 'set' is an unordered collection with no duplicate
|
A 'set' is an unordered collection with no duplicate
|
||||||
elements.
|
elements.
|
||||||
@ -51,6 +55,7 @@ safer_lua.DataStructHelp = [[
|
|||||||
s.has("Susi") --> function returns `true`
|
s.has("Susi") --> function returns `true`
|
||||||
s.has("Mike") --> function returns `false`
|
s.has("Mike") --> function returns `false`
|
||||||
s.size() --> function returns 2
|
s.size() --> function returns 2
|
||||||
|
s.memsize() --> function returns 8
|
||||||
s.next() --> for loop iterator function
|
s.next() --> for loop iterator function
|
||||||
]]
|
]]
|
||||||
|
|
||||||
@ -133,6 +138,28 @@ function safer_lua.Store()
|
|||||||
if n then return n, Data[n] end
|
if n then return n, Data[n] end
|
||||||
end
|
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()
|
new_t.__dump = function()
|
||||||
-- remove the not serializable meta data
|
-- remove the not serializable meta data
|
||||||
return {Type = "Store", Size = Size, MemSize = MemSize, Data = Data}
|
return {Type = "Store", Size = Size, MemSize = MemSize, Data = Data}
|
||||||
@ -198,6 +225,14 @@ function safer_lua.Array(...)
|
|||||||
return v
|
return v
|
||||||
end
|
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)
|
new_t.memsize = function(t)
|
||||||
return MemSize
|
return MemSize
|
||||||
end
|
end
|
||||||
|
@ -61,7 +61,7 @@ function safer_lua.config(max_code_size, max_table_size)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function compile(pos, text, label, err_clbk)
|
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:")
|
text = text:gsub("%$", "S:")
|
||||||
local code, err = loadstring(text)
|
local code, err = loadstring(text)
|
||||||
if not code then
|
if not code then
|
||||||
|
@ -75,7 +75,6 @@ end
|
|||||||
local InvalidKeywords = {
|
local InvalidKeywords = {
|
||||||
["while"] = true,
|
["while"] = true,
|
||||||
["repeat"] = true,
|
["repeat"] = true,
|
||||||
["break"] = true,
|
|
||||||
["until"] = true,
|
["until"] = true,
|
||||||
["for"] = true,
|
["for"] = true,
|
||||||
["function"] = true,
|
["function"] = true,
|
||||||
@ -90,7 +89,7 @@ local InvalidChars = {
|
|||||||
["}"] = true,
|
["}"] = true,
|
||||||
}
|
}
|
||||||
|
|
||||||
function safer_lua:check(text, label, err_clbk)
|
function safer_lua:check(pos, text, label, err_clbk)
|
||||||
local lToken = self:scanner(text)
|
local lToken = self:scanner(text)
|
||||||
local lineno = 0
|
local lineno = 0
|
||||||
local errno = 0
|
local errno = 0
|
||||||
@ -98,13 +97,18 @@ function safer_lua:check(text, label, err_clbk)
|
|||||||
if type(token) == "number" then
|
if type(token) == "number" then
|
||||||
lineno = token
|
lineno = token
|
||||||
elseif InvalidKeywords[token] then
|
elseif InvalidKeywords[token] then
|
||||||
if token ~= "for" or lToken[idx + 3] ~= "in" or
|
if token == "for" then
|
||||||
lToken[idx + 5] ~= "next" then -- invalid for statement?
|
-- invalid for statement?
|
||||||
err_clbk(label..lineno..": Invalid keyword '"..token.."'")
|
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
|
errno = errno + 1
|
||||||
end
|
end
|
||||||
elseif InvalidChars[token] then
|
elseif InvalidChars[token] then
|
||||||
err_clbk(label..lineno..": Invalid character '"..token.."'")
|
err_clbk(pos, label..lineno..": Invalid character '"..token.."'")
|
||||||
errno = errno + 1
|
errno = errno + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -106,6 +106,14 @@ sl_controller.register_action("events", {
|
|||||||
' e.g. $events(true) -- enable 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 function formspec0(meta)
|
||||||
local running = meta:get_int("state") == tubelib.RUNNING
|
local running = meta:get_int("state") == tubelib.RUNNING
|
||||||
|
Loading…
Reference in New Issue
Block a user