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)
### Additions
- SaferLua: range(from, to) added, standard string functions added
### Removals

@ -14,16 +14,29 @@
safer_lua.MaxCodeSize = 5000 -- size if source code in bytes
safer_lua.MaxTableSize = 1000 -- sum over all table sizes
safer_lua.MaxExeTime = 5000 -- max. execution time in us
local function memsize()
return safer_lua.MaxTableSize
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 = {
Array = safer_lua.Array,
Store = safer_lua.Store,
Set = safer_lua.Set,
memsize = memsize,
range = range,
math = {
floor = math.floor,
abs = math.abs,
@ -31,6 +44,20 @@ local BASE_ENV = {
min = math.min,
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,
tostring = tostring,
type = type,

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