summaryrefslogtreecommitdiff
path: root/library/lua
diff options
context:
space:
mode:
authorAlexander Gavrilov2012-04-21 20:15:57 +0400
committerAlexander Gavrilov2012-04-21 20:15:57 +0400
commit2ef321a2086f03de30d184c06c527f504893cd07 (patch)
treec582c906eb3b2760d0fa1ca0a32fa5c5275902e0 /library/lua
parent4af051bab3e455a9115a44e43b8c3da0cd189f43 (diff)
downloaddfhack-2ef321a2086f03de30d184c06c527f504893cd07.tar.gz
dfhack-2ef321a2086f03de30d184c06c527f504893cd07.tar.bz2
dfhack-2ef321a2086f03de30d184c06c527f504893cd07.tar.xz
Preserve the original lua global environment for modules.
The intent is to prevent accidental pollution of module namespaces by globals defined from careless scripts running in the _G environment.
Diffstat (limited to 'library/lua')
-rw-r--r--library/lua/dfhack.lua11
1 files changed, 10 insertions, 1 deletions
diff --git a/library/lua/dfhack.lua b/library/lua/dfhack.lua
index c7e2669c..5c10b002 100644
--- a/library/lua/dfhack.lua
+++ b/library/lua/dfhack.lua
@@ -1,6 +1,15 @@
-- Common startup file for all dfhack plugins with lua support
-- The global dfhack table is already created by C++ init code.
+-- Setup the global environment.
+-- BASE_G is the original lua global environment,
+-- preserved as a common denominator for all modules.
+-- This file uses it instead of the new default one.
+
+local dfhack = dfhack
+local base_env = dfhack.BASE_G
+local _ENV = base_env
+
-- Console color constants
COLOR_RESET = -1
@@ -70,7 +79,7 @@ function mkmodule(module,env)
if plugname then
dfhack.open_plugin(pkg,plugname)
end
- setmetatable(pkg, { __index = (env or _G) })
+ setmetatable(pkg, { __index = (env or base_env) })
return pkg
end