]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/common/strict.lua
Remove unused variable WARN_INIT
[dragonfireclient.git] / builtin / common / strict.lua
1 local getinfo = debug.getinfo
2
3 function core.global_exists(name)
4         if type(name) ~= "string" then
5                 error("core.global_exists: " .. tostring(name) .. " is not a string")
6         end
7         return rawget(_G, name) ~= nil
8 end
9
10
11 local meta = {}
12 local declared = {}
13 -- Key is source file, line, and variable name; seperated by NULs
14 local warned = {}
15
16 function meta:__newindex(name, value)
17         local info = getinfo(2, "Sl")
18         local desc = ("%s:%d"):format(info.short_src, info.currentline)
19         if not declared[name] then
20                 local warn_key = ("%s\0%d\0%s"):format(info.source,
21                                 info.currentline, name)
22                 if not warned[warn_key] and info.what ~= "main" and
23                                 info.what ~= "C" then
24                         core.log("warning", ("Assignment to undeclared "..
25                                         "global %q inside a function at %s.")
26                                 :format(name, desc))
27                         warned[warn_key] = true
28                 end
29                 declared[name] = true
30         end
31         rawset(self, name, value)
32 end
33
34
35 function meta:__index(name)
36         local info = getinfo(2, "Sl")
37         local warn_key = ("%s\0%d\0%s"):format(info.source, info.currentline, name)
38         if not declared[name] and not warned[warn_key] and info.what ~= "C" then
39                 core.log("warning", ("Undeclared global variable %q accessed at %s:%s")
40                                 :format(name, info.short_src, info.currentline))
41                 warned[warn_key] = true
42         end
43         return rawget(self, name)
44 end
45
46 setmetatable(_G, meta)