diff options
Diffstat (limited to 'mesecons/util.lua')
-rw-r--r-- | mesecons/util.lua | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/mesecons/util.lua b/mesecons/util.lua index 7fb95cc..a89d234 100644 --- a/mesecons/util.lua +++ b/mesecons/util.lua @@ -474,3 +474,21 @@ function mesecon.execute_autoconnect_hooks_queue(pos, node) mesecon.queue:add_action(pos, "autoconnect_hook_"..name, {node}) end end + +-- Performs a deep copy of a table, changing the environment of any functions. +-- Adapted from the builtin table.copy() function. +function mesecon.tablecopy_change_env(t, env, seen) + local n = {} + seen = seen or {} + seen[t] = n + for k, v in pairs(t) do + if type(v) == "function" then + setfenv(v, env) + n[(type(k) == "table" and (seen[k] or mesecon.tablecopy_change_env(k, env, seen))) or k] = v + else + n[(type(k) == "table" and (seen[k] or mesecon.tablecopy_change_env(k, env, seen))) or k] = + (type(v) == "table" and (seen[v] or mesecon.tablecopy_change_env(v, env, seen))) or v + end + end + return n +end |