From b7d58147fee30c133877831eaa34ae48549a4410 Mon Sep 17 00:00:00 2001 From: Joachim Stolberg Date: Sat, 30 Jun 2018 16:04:55 +0200 Subject: [PATCH] controller instance bug fix for loops added --- safer_lua/data_struct.lua | 29 +++++++++++++++++++++++++++++ safer_lua/environ.lua | 2 +- safer_lua/scanner.lua | 10 ++++++---- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/safer_lua/data_struct.lua b/safer_lua/data_struct.lua index 4584254..58220cc 100644 --- a/safer_lua/data_struct.lua +++ b/safer_lua/data_struct.lua @@ -28,6 +28,7 @@ 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.next() --> for loop iterator function Unlike arrays, which are indexed by a range of numbers, 'stores' are indexed by keys: @@ -38,6 +39,7 @@ safer_lua.DataStructHelp = [[ s.set(0, "hello") --> {val = 12, [0] = "hello"} s.del("val") --> {[0] = "hello"} s.size() --> function returns 1 + s.next() --> for loop iterator function A 'set' is an unordered collection with no duplicate elements. @@ -49,6 +51,7 @@ safer_lua.DataStructHelp = [[ s.has("Susi") --> function returns `true` s.has("Mike") --> function returns `false` s.size() --> function returns 2 + s.next() --> for loop iterator function ]] local function var_count(v) @@ -123,6 +126,13 @@ function safer_lua.Store() return Size end + new_t.next = function(t) + local n = nil + return function () + n = next(Data, n) + if n then return n, Data[n] end + end + end new_t.__dump = function() -- remove the not serializable meta data return {Type = "Store", Size = Size, MemSize = MemSize, Data = Data} @@ -196,6 +206,15 @@ function safer_lua.Array(...) return #Data end + new_t.next = function(t) + local i = 0 + local n = #Data + return function () + i = i + 1 + if i <= n then return i, Data[i] end + end + end + new_t.__dump = function() -- remove the not serializable meta data return {Type = "Array", MemSize = MemSize, Data = Data} @@ -257,6 +276,16 @@ function safer_lua.Set(...) return Size end + new_t.next = function(t) + local i = 0 + local n = nil + return function () + i = i + 1 + n = next(Data, n) + if n then return i, n end + end + end + new_t.__dump = function() -- remove the not serializable meta data return {Type = "Set", Size = Size, MemSize = MemSize, Data = Data} diff --git a/safer_lua/environ.lua b/safer_lua/environ.lua index 8a209ef..4963223 100644 --- a/safer_lua/environ.lua +++ b/safer_lua/environ.lua @@ -84,7 +84,7 @@ function safer_lua.init(pos, init, loop, environ, err_clbk) end local code = compile(pos, init, "init() ", err_clbk) if code then - local env = BASE_ENV + local env = table.copy(BASE_ENV) env.S = {} env.S = map(env.S, environ) setfenv(code, env) diff --git a/safer_lua/scanner.lua b/safer_lua/scanner.lua index 22460f4..3a2a30b 100644 --- a/safer_lua/scanner.lua +++ b/safer_lua/scanner.lua @@ -78,7 +78,6 @@ local InvalidKeywords = { ["break"] = true, ["until"] = true, ["for"] = true, - ["do"] = true, ["function"] = true, ["_G"] = true, ["__load"] = true, @@ -95,12 +94,15 @@ function safer_lua:check(text, label, err_clbk) local lToken = self:scanner(text) local lineno = 0 local errno = 0 - for _,token in ipairs(lToken) do + for idx,token in ipairs(lToken) do if type(token) == "number" then lineno = token elseif InvalidKeywords[token] then - err_clbk(label..lineno..": Invalid keyword '"..token.."'") - errno = errno + 1 + if token ~= "for" or lToken[idx + 3] ~= "in" or + lToken[idx + 5] ~= "next" then -- invalid for statement? + err_clbk(label..lineno..": Invalid keyword '"..token.."'") + errno = errno + 1 + end elseif InvalidChars[token] then err_clbk(label..lineno..": Invalid character '"..token.."'") errno = errno + 1