range(from, to) added, standard string functions added

This commit is contained in:
Joachim Stolberg 2019-01-20 17:39:34 +01:00
parent f9309286bb
commit edbefbf3d6
3 changed files with 53 additions and 5 deletions

@ -4,6 +4,7 @@
## V2.00.04 (2019-01-20) ## V2.00.04 (2019-01-20)
### Additions ### Additions
- SaferLua: range(from, to) added, standard string functions added
### Removals ### Removals

@ -14,16 +14,29 @@
safer_lua.MaxCodeSize = 5000 -- size if source code in bytes safer_lua.MaxCodeSize = 5000 -- size if source code in bytes
safer_lua.MaxTableSize = 1000 -- sum over all table sizes safer_lua.MaxTableSize = 1000 -- sum over all table sizes
safer_lua.MaxExeTime = 5000 -- max. execution time in us
local function memsize() local function memsize()
return safer_lua.MaxTableSize return safer_lua.MaxTableSize
end end
local function range(from, to)
return function(expired_at,last)
assert(expired_at > minetest.get_us_time(), "Runtime limit exceeded")
if last >= to then
return nil
else
return last+1
end
end, minetest.get_us_time() + safer_lua.MaxExeTime, from-1
end
local BASE_ENV = { local BASE_ENV = {
Array = safer_lua.Array, Array = safer_lua.Array,
Store = safer_lua.Store, Store = safer_lua.Store,
Set = safer_lua.Set, Set = safer_lua.Set,
memsize = memsize, memsize = memsize,
range = range,
math = { math = {
floor = math.floor, floor = math.floor,
abs = math.abs, abs = math.abs,
@ -31,6 +44,20 @@ local BASE_ENV = {
min = math.min, min = math.min,
random = math.random, random = math.random,
}, },
string = {
byte = string.byte,
char = string.char,
find = string.find,
format = string.format,
gmatch = string.gmatch,
gsub = string.gsub,
len = string.len,
lower = string.lower,
match = string.match,
rep = string.rep,
sub = string.sub,
upper = string.upper,
},
tonumber = tonumber, tonumber = tonumber,
tostring = tostring, tostring = tostring,
type = type, type = type,

@ -40,16 +40,26 @@ function safer_lua:string(pttrn)
-- result is not needed -- result is not needed
end end
local function lines(str)
local t = {}
local function helper(line)
table.insert(t, line)
return ""
end
helper((str:gsub("(.-)\r?\n", helper)))
return t
end
function safer_lua:scanner(text) function safer_lua:scanner(text)
local lToken = {} local lToken = {}
for idx, line in ipairs(text:split("\n")) do for idx, line in ipairs(lines(text)) do
self.line = line self.line = line
self.pos = 1 self.pos = 1
self.line = trim(self.line) self.line = trim(self.line)
self.line = self.line:split("--")[1] self.line = self.line:split("--")[1]
table.insert(lToken, idx) -- line number
if self.line then if self.line then
-- devide line in tokens -- devide line in tokens
table.insert(lToken, idx) -- line number
while true do while true do
if self.pos > #self.line then break end if self.pos > #self.line then break end
local ch = self.line:sub(self.pos, self.pos) local ch = self.line:sub(self.pos, self.pos)
@ -80,6 +90,7 @@ local InvalidKeywords = {
["repeat"] = true, ["repeat"] = true,
["until"] = true, ["until"] = true,
["for"] = true, ["for"] = true,
["range"] = true,
--["function"] = true, --["function"] = true,
["_G"] = true, ["_G"] = true,
["__load"] = true, ["__load"] = true,
@ -104,9 +115,18 @@ function safer_lua:check(pos, text, label, err_clbk)
elseif InvalidKeywords[token] then elseif InvalidKeywords[token] then
if token == "for" then if token == "for" then
-- invalid for statement? -- invalid for statement?
if lToken[idx + 3] ~= "in" or lToken[idx + 5] ~= "next" then if lToken[idx + 3] == "in" and lToken[idx + 5] == "next" then
err_clbk(pos, label..":"..lineno..": Invalid use of 'for'") --
errno = errno + 1 elseif lToken[idx + 2] == "in" and lToken[idx + 3] == "range" then
--
else
err_clbk(pos, label..":"..lineno..": Invalid use of 'for'")
errno = errno + 1
end
elseif token == "range" then
if lToken[idx - 1] ~= "in" then
err_clbk(pos, label..":"..lineno..": Invalid use of 'range'")
errno = errno + 1
end end
else else
err_clbk(pos, label..":"..lineno..": Invalid keyword '"..token.."'") err_clbk(pos, label..":"..lineno..": Invalid keyword '"..token.."'")