From 077cdaa2a4f40a589fbee300783785901d9fffab Mon Sep 17 00:00:00 2001 From: Lejo1 Date: Mon, 30 Mar 2020 13:05:25 +0200 Subject: [PATCH 1/2] Add all usefull helper function from lua_api.txt --- mesecons_luacontroller/init.lua | 38 ++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/mesecons_luacontroller/init.lua b/mesecons_luacontroller/init.lua index 1c93e48..08e4be5 100644 --- a/mesecons_luacontroller/init.lua +++ b/mesecons_luacontroller/init.lua @@ -231,6 +231,16 @@ local function safe_string_find(...) return string.find(...) end +-- do not allow pattern matching in string.split (see string.find for details) +local function safe_string_split(...) + if select(5, ...) then + debug.sethook() -- Clear hook + error("string.split: 'sep_is_pattern' (fifth parameter) may not be used in a Luacontroller") + end + + return string.split(...) +end + local function remove_functions(x) local tp = type(x) if tp == "function" then @@ -463,6 +473,7 @@ local function create_environment(pos, mem, event, itbl, send_warning) reverse = string.reverse, sub = string.sub, find = safe_string_find, + split = safe_string_split, }, math = { abs = math.abs, @@ -475,10 +486,12 @@ local function create_environment(pos, mem, event, itbl, send_warning) cosh = math.cosh, deg = math.deg, exp = math.exp, + factorial = math.factorial, floor = math.floor, fmod = math.fmod, frexp = math.frexp, huge = math.huge, + hypot = math.hypot, ldexp = math.ldexp, log = math.log, log10 = math.log10, @@ -490,6 +503,7 @@ local function create_environment(pos, mem, event, itbl, send_warning) rad = math.rad, random = math.random, sin = math.sin, + sing = math.sign, sinh = math.sinh, sqrt = math.sqrt, tan = math.tan, @@ -497,9 +511,14 @@ local function create_environment(pos, mem, event, itbl, send_warning) }, table = { concat = table.concat, + copy = table.copy, + indexof = table.indexof, insert = table.insert, + insert_all = table.insert_all, + key_value_swap = table.key_value_swap, maxn = table.maxn, remove = table.remove, + shuffle = table.shuffle, sort = table.sort, }, os = { @@ -508,6 +527,24 @@ local function create_environment(pos, mem, event, itbl, send_warning) time = os.time, datetable = safe_date, }, + dump2 = dump2, + dump = dump, + minetest = { + wrap_text = minetest.wrap_text, + pos_to_string = minetest.pos_to_string, + string_to_pos = minetest.string_to_pos, + string_to_area = minetest.string_to_area, + formspec_escape = minetest.formspec_escape, + is_yes = minetest.is_yes, + is_nan = minetest.is_nan, + get_us_time = minetest.get_us_time, + parse_json = minetest.parse_json, + write_json = minetest.write_json, + rgba = minetest.rgba, + encode_base64 = minetest.encode_base64, + decode_base64 = minetest.decode_base64, + }, + vector = vector, } env._G = env @@ -901,4 +938,3 @@ minetest.register_craft({ {'group:mesecon_conductor_craftable', 'group:mesecon_conductor_craftable', ''}, } }) - From a19ef422ea92558b41587a59a75a78e387eee665 Mon Sep 17 00:00:00 2001 From: Lejo1 Date: Mon, 30 Mar 2020 14:22:57 +0200 Subject: [PATCH 2/2] Use table.copy instead of each separate function --- mesecons_luacontroller/init.lua | 52 +++------------------------------ 1 file changed, 4 insertions(+), 48 deletions(-) diff --git a/mesecons_luacontroller/init.lua b/mesecons_luacontroller/init.lua index 08e4be5..bf1d552 100644 --- a/mesecons_luacontroller/init.lua +++ b/mesecons_luacontroller/init.lua @@ -475,52 +475,8 @@ local function create_environment(pos, mem, event, itbl, send_warning) find = safe_string_find, split = safe_string_split, }, - math = { - abs = math.abs, - acos = math.acos, - asin = math.asin, - atan = math.atan, - atan2 = math.atan2, - ceil = math.ceil, - cos = math.cos, - cosh = math.cosh, - deg = math.deg, - exp = math.exp, - factorial = math.factorial, - floor = math.floor, - fmod = math.fmod, - frexp = math.frexp, - huge = math.huge, - hypot = math.hypot, - ldexp = math.ldexp, - log = math.log, - log10 = math.log10, - max = math.max, - min = math.min, - modf = math.modf, - pi = math.pi, - pow = math.pow, - rad = math.rad, - random = math.random, - sin = math.sin, - sing = math.sign, - sinh = math.sinh, - sqrt = math.sqrt, - tan = math.tan, - tanh = math.tanh, - }, - table = { - concat = table.concat, - copy = table.copy, - indexof = table.indexof, - insert = table.insert, - insert_all = table.insert_all, - key_value_swap = table.key_value_swap, - maxn = table.maxn, - remove = table.remove, - shuffle = table.shuffle, - sort = table.sort, - }, + math = table.copy(math), + table = table.copy(table), os = { clock = os.clock, difftime = os.difftime, @@ -530,7 +486,7 @@ local function create_environment(pos, mem, event, itbl, send_warning) dump2 = dump2, dump = dump, minetest = { - wrap_text = minetest.wrap_text, + --wrap_text = minetest.wrap_text, pos_to_string = minetest.pos_to_string, string_to_pos = minetest.string_to_pos, string_to_area = minetest.string_to_area, @@ -544,7 +500,7 @@ local function create_environment(pos, mem, event, itbl, send_warning) encode_base64 = minetest.encode_base64, decode_base64 = minetest.decode_base64, }, - vector = vector, + vector = table.copy(vector), } env._G = env