LuaBlock: don't set the function environment + remove created pos global

This commit is contained in:
upsilon 2017-05-02 18:26:09 +02:00
parent abe8368e32
commit e1d2e84746
No known key found for this signature in database
GPG Key ID: A80DAE1F266E1C3C

@ -81,8 +81,8 @@ minetest.register_node("moremesecons_luablock:luablock", {
return meta:get_string("owner") == player:get_player_name() return meta:get_string("owner") == player:get_player_name()
end, end,
mesecons = {effector = { mesecons = {effector = {
action_on = function(pos, node) action_on = function(npos, node)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(npos)
local code = meta:get_string("code") local code = meta:get_string("code")
if code == "" then if code == "" then
return return
@ -98,11 +98,12 @@ minetest.register_node("moremesecons_luablock:luablock", {
make_formspec(meta, pos) make_formspec(meta, pos)
return return
end end
local env = _G -- Set the "pos" global
env.pos = table.copy(pos) local old_pos = pos -- In case there's already an existing "pos" global
setfenv(func, env) pos = table.copy(npos)
local good, err = pcall(func) local good, err = pcall(func)
-- Still alive! No shutdown requested? No infinite loop? pos = old_pos
if not good then -- Runtime error if not good then -- Runtime error
meta:set_string("errmsg", err) meta:set_string("errmsg", err)
make_formspec(meta, pos) make_formspec(meta, pos)