webmcp
changeset 237:ba596113a799
Exception for setting global variables
author | jbe |
---|---|
date | Sat Feb 28 23:51:20 2015 +0100 (2015-02-28) |
parents | 4ab91adeab6f |
children | 4a735f198ded |
files | framework/bin/mcp.lua |
line diff
1.1 --- a/framework/bin/mcp.lua Sat Feb 28 23:37:27 2015 +0100 1.2 +++ b/framework/bin/mcp.lua Sat Feb 28 23:51:20 2015 +0100 1.3 @@ -4,6 +4,7 @@ 1.4 1.5 -- allow control of global environment 1.6 local _G = _G 1.7 +local allowed_globals = {} 1.8 local global_metatable = { 1.9 __index = _G, 1.10 __newindex = function(self, key, value) 1.11 @@ -163,20 +164,17 @@ 1.12 end 1.13 end 1.14 if self == _ENV then 1.15 - _G[key] = false -- allow writing global variable 1.16 + allowed_globals[key] = true 1.17 end 1.18 if merge and try_exec(merge_path .. ".lua") then 1.19 elseif merge and try_dir(merge_path .. "/") then 1.20 elseif try_exec(path .. ".lua") then 1.21 elseif try_dir(path .. "/") then 1.22 else end 1.23 - local value = rawget(self, key) 1.24 - if self == _ENV and value == false then 1.25 - _G[key] = nil 1.26 - return nil 1.27 - else 1.28 - return value 1.29 + if self == _ENV then 1.30 + allowed_globals[key] = nil 1.31 end 1.32 + return rawget(self, key) 1.33 end 1.34 install_autoloader(_G, nil, "") 1.35 try_exec(WEBMCP_FRAMEWORK_PATH .. "env/__init.lua") 1.36 @@ -191,8 +189,11 @@ 1.37 end 1.38 1.39 -- prohibit (unintended) definition of new global variables 1.40 -function global_metatable.__newindex() 1.41 - error("Setting of global variable prohibited", 2) 1.42 +function global_metatable.__newindex(self, key, value) 1.43 + if not allowed_globals[key] then 1.44 + error("Setting of global variable prohibited", 2) 1.45 + end 1.46 + rawset(self, key, value) 1.47 end 1.48 1.49 -- execute configurations and pre-fork initializers