Controller bugs fixed

This commit is contained in:
Joachim Stolberg 2018-06-25 20:42:47 +02:00
parent bdc277d8a2
commit b2c050dc2a
5 changed files with 33 additions and 29 deletions

@ -19,8 +19,16 @@ safer_lua.MaxTableSize = 1000 -- number of table entries considering string le
local BASE_ENV = {
Store = safer_lua.Store,
math = {
floor = math.floor
floor = math.floor,
abs = math.abs,
max = math.max,
min = math.min,
random = math.random,
},
tonumber = tonumber,
tostring = tostring,
type = type,
ticks = 0,
}
@ -62,7 +70,6 @@ function safer_lua.init(pos, init, loop, environ, err_clbk)
if code then
local env = BASE_ENV
env.S = {}
env.S._G = _G
env.S = map(env.S, environ)
setfenv(code, env)
local res, err = pcall(code)
@ -85,7 +92,6 @@ function safer_lua.run_loop(pos, elapsed, code, err_clbk)
env.event = false
env.ticks = env.ticks + 1
env.elapsed = elapsed
setfenv(code, env)
local res, err = pcall(code)
if not res then
err = err:gsub("%[string .+%]:", "loop() ")
@ -98,7 +104,7 @@ end
function safer_lua.run_event(pos, code, err_clbk)
local env = getfenv(code)
env.event = true
setfenv(code, env)
env.elapsed = 0
local res, err = pcall(code)
if not res then
err = err:gsub("%[string .+%]:", "loop() ")

@ -17,7 +17,7 @@ dofile('/home/joachim/minetest/mods/techpack/safer_lua/environ.lua')
--code = Cache[key]
local function foo(self, val)
_G = self._G
--_G = self._G
print("Hallo", val)
end
@ -28,14 +28,15 @@ end
local init = "init = 5"
local loop = [[
$foo("hallo")
S.foo("hallo")
--S._G.print("Fehler")
$foo("hallo")
$foo(math.floor(5.5))
$foo("Joe")
a = Store()
a.set("a", 123)
$foo(a.get("a"))
$foo(ticks)
if ticks == 10 then $foo("Fehler")
]]
local env = {foo = foo}
@ -43,7 +44,7 @@ local env = {foo = foo}
local code = safer_lua.init(0, init, loop, env, error)
if code then
print(safer_lua.run_loop(0, code, error))
safer_lua.run_loop(0, code, error)
safer_lua.run_loop(0, code, error)
print(safer_lua.run_loop(0, 0, code, error))
safer_lua.run_loop(0, 1, code, error)
safer_lua.run_loop(0, 2, code, error)
end

@ -16,7 +16,6 @@ local function on_timer(pos, elapsed)
local meta = minetest.get_meta(pos)
local percent = (sl_controller.battery_capacity - meta:get_int("content"))
percent = 100 - math.floor((percent * 100.0 / sl_controller.battery_capacity))
print("percent", percent, meta:get_int("content"))
meta:set_string("infotext", "Battery ("..percent.."%)")
if percent == 0 then
local node = minetest.get_node(pos)
@ -52,7 +51,7 @@ minetest.register_node("sl_controller:battery", {
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_int("content", sl_controller.battery_capacity)
minetest.get_node_timer(pos):start(2)
minetest.get_node_timer(pos):start(30)
end,
on_timer = on_timer,

@ -16,7 +16,6 @@
sl_controller.register_function("get_input", {
cmnd = function(self, num)
_G = self._G
num = tostring(num or "")
return sl_controller.get_input(self.meta.number, num)
end,
@ -28,7 +27,6 @@ sl_controller.register_function("get_input", {
sl_controller.register_function("get_status", {
cmnd = function(self, num)
_G = self._G
num = tostring(num or "")
return tubelib.send_request(num, "state", nil)
end,
@ -40,7 +38,6 @@ sl_controller.register_function("get_status", {
sl_controller.register_function("time_as_str", {
cmnd = function(self)
_G = self._G
local t = minetest.get_timeofday()
local h = math.floor(t*24) % 24
local m = math.floor(t*1440) % 60
@ -53,7 +50,6 @@ sl_controller.register_function("time_as_str", {
sl_controller.register_function("time_as_num", {
cmnd = function(self, num)
_G = self._G
local t = minetest.get_timeofday()
local h = math.floor(t*24) % 24
local m = math.floor(t*1440) % 60
@ -66,7 +62,6 @@ sl_controller.register_function("time_as_num", {
sl_controller.register_function("playerdetector", {
cmnd = function(self, num)
_G = self._G
num = tostring(num or "")
return tubelib.send_request(num, "name", nil)
end,
@ -77,7 +72,6 @@ sl_controller.register_function("playerdetector", {
sl_controller.register_action("send_cmnd", {
cmnd = function(self, num, text)
_G = self._G
num = tostring(num or "")
text = tostring(text or "")
tubelib.send_message(num, self.meta.owner, nil, text, nil)
@ -91,7 +85,6 @@ sl_controller.register_action("send_cmnd", {
sl_controller.register_action("display", {
cmnd = function(self, num, row, text1, text2, text3)
_G = self._G
text1 = tostring(text1 or "")
text2 = tostring(text2 or "")
text3 = tostring(text3 or "")
@ -106,7 +99,6 @@ sl_controller.register_action("display", {
sl_controller.register_action("clear_screen", {
cmnd = function(self, num)
_G = self._G
num = tostring(num or "")
tubelib.send_message(num, self.meta.owner, nil, "clear", nil)
end,
@ -118,7 +110,6 @@ sl_controller.register_action("clear_screen", {
sl_controller.register_action("chat", {
cmnd = function(self, text1, text2, text3)
_G = self._G
text1 = tostring(text1 or "")
text2 = tostring(text2 or "")
text3 = tostring(text3 or "")
@ -132,7 +123,6 @@ sl_controller.register_action("chat", {
sl_controller.register_action("door", {
cmnd = function(self, pos, text)
_G = self._G
pos = tostring(pos or "")
text = tostring(text or "")
pos = minetest.string_to_pos("("..pos..")")

@ -14,12 +14,13 @@
local sHELP = [[SaferLua Controller
Safer LUA is a subset of LUA with the following restrictions:
SaferLua is a subset of Lua with the following restrictions:
- No loop keywords like: for, while, repeat,...
- No table construction {..}
- Limited set of available functions
- Store() as alternative to LUA tables
- Store() as alternative to Lua tables
See: goo.gl/WRWZgt
]]
local Cache = {}
@ -68,7 +69,6 @@ end
sl_controller.register_action("print", {
cmnd = function(self, text1, text2, text3)
_G = self._G
local pos = self.meta.pos
text1 = tostring(text1 or "")
text2 = tostring(text2 or "")
@ -176,6 +176,8 @@ local function error(pos, err)
local number = meta:get_string("number")
meta:set_string("formspec", formspec3(meta))
meta:set_string("infotext", "Controller "..number..": error")
meta:set_int("state", tubelib.STOPPED)
minetest.get_node_timer(pos):stop()
return false
end
@ -209,15 +211,19 @@ local function start_controller(pos)
local number = meta:get_string("number")
if not battery(pos) then
meta:set_string("formspec", formspec0(meta))
return
return false
end
meta:set_string("output", "<press update>")
meta:set_string("formspec", formspec3(meta))
if compile(pos, meta, number) then
meta:set_int("state", tubelib.RUNNING)
minetest.get_node_timer(pos):start(1)
meta:set_string("infotext", "Controller "..number..": running")
return true
end
meta:set_string("output", "<press update>")
meta:set_string("formspec", formspec3(meta))
return false
end
local function stop_controller(pos)
@ -243,7 +249,6 @@ local function update_battery(meta, cpu)
if pos then
meta = minetest.get_meta(pos)
local content = meta:get_int("content") - cpu
print("content", content)
if content <= 0 then
meta:set_int("content", 0)
return false
@ -256,6 +261,9 @@ end
local function on_timer(pos, elapsed)
local t = minetest.get_us_time()
local meta = minetest.get_meta(pos)
if meta:get_int("state") ~= "running" then
return false
end
local number = meta:get_string("number")
if Cache[number] or compile(pos, meta, number) then