-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstrict.lua
43 lines (38 loc) · 1.23 KB
/
strict.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
local IGNORED_WRITES = {}
local IGNORED_READS = {
qt=true,
_PROMPT=true,
_PROMPT2=true,
writeObjects=true,
arg=true,
}
-- Raises an error when an undeclared variable is read.
local function guardGlobals()
assert(getmetatable(_G) == nil, "another global metatable exists")
-- The detecting of undeclared vars is discussed on:
-- http://www.lua.org/pil/14.2.html
-- http://lua-users.org/wiki/DetectingUndefinedVariables
setmetatable(_G, {
__newindex = function (table, key, value)
if not IGNORED_WRITES[key] then
local info = debug.getinfo(2, "Sl")
io.stderr:write(string.format(
"strict: %s:%s: write to undeclared variable: %s\n",
tostring(info.short_src), tostring(info.currentline), key))
end
rawset(table, key, value)
end,
__index = function (table, key)
if IGNORED_READS[key] then
return
end
error("attempt to read undeclared variable "..key, 2)
end,
})
local origRequire = require
function require(modname)
IGNORED_WRITES[modname] = true
return origRequire(modname)
end
end
guardGlobals()