Subversion Repositories WoWGM

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 tristanc 1
--
2
-- strict.lua
3
-- checks uses of undeclared global variables
4
-- All global variables must be 'declared' through a regular assignment
5
-- (even assigning nil will do) in a main chunk before being used
6
-- anywhere or assigned to inside a function.
7
--
8
 
9
local mt = getmetatable(_G)
10
if mt == nil then
11
  mt = {}
12
  setmetatable(_G, mt)
13
end
14
 
15
mt.__declared = {}
16
 
17
mt.__newindex = function (t, n, v)
18
  if not mt.__declared[n] then
19
    local w = debug.getinfo(2, "S").what
20
    if w ~= "main" and w ~= "C" then
21
      error("assign to undeclared variable '"..n.."'", 2)
22
    end
23
    mt.__declared[n] = true
24
  end
25
  rawset(t, n, v)
26
end
27
 
28
mt.__index = function (t, n)
29
  if not mt.__declared[n] and debug.getinfo(2, "S").what ~= "C" then
30
    error("variable '"..n.."' is not declared", 2)
31
  end
32
  return rawget(t, n)
33
end
34