Created
May 26, 2015 01:56
-
-
Save notwa/4d87cdbc0c00fc0fb9d3 to your computer and use it in GitHub Desktop.
explicit globals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local mt = getmetatable(_G) | |
if mt == nil then | |
mt = {} | |
setmetatable(_G, mt) | |
end | |
mt.__declared = {} | |
function mt.__newindex(t, n, v) | |
if not mt.__declared[n] then | |
local info = debug.getinfo(2, "S") | |
if info and info.what ~= "main" and info.what ~= "C" then | |
error("cannot assign undeclared global '" .. tostring(n) .. "'", 2) | |
end | |
mt.__declared[n] = true | |
end | |
rawset(t, n, v) | |
end | |
function mt.__index(t, n) | |
if not mt.__declared[n] then | |
local info = debug.getinfo(2, "S") | |
if info and info.what ~= "main" and info.what ~= "C" then | |
error("cannot use undeclared global '" .. tostring(n) .. "'", 2) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment