remove ad from mod.conf
[waspsaliva.git] / builtin / common / strict.lua
blobccde9676b3af202bbba0f13b6b11b306a0e70287
2 -- Always warn when creating a global variable, even outside of a function.
3 -- This ignores mod namespaces (variables with the same name as the current mod).
4 local WARN_INIT = false
6 local getinfo = debug.getinfo
8 function core.global_exists(name)
9 if type(name) ~= "string" then
10 error("core.global_exists: " .. tostring(name) .. " is not a string")
11 end
12 return rawget(_G, name) ~= nil
13 end
16 local meta = {}
17 local declared = {}
18 -- Key is source file, line, and variable name; seperated by NULs
19 local warned = {}
21 function meta:__newindex(name, value)
22 local info = getinfo(2, "Sl")
23 local desc = ("%s:%d"):format(info.short_src, info.currentline)
24 if not declared[name] then
25 local warn_key = ("%s\0%d\0%s"):format(info.source,
26 info.currentline, name)
27 if not warned[warn_key] and info.what ~= "main" and
28 info.what ~= "C" then
29 core.log("warning", ("Assignment to undeclared "..
30 "global %q inside a function at %s.")
31 :format(name, desc))
32 warned[warn_key] = true
33 end
34 declared[name] = true
35 end
36 -- Ignore mod namespaces
37 if WARN_INIT and name ~= core.get_current_modname() then
38 core.log("warning", ("Global variable %q created at %s.")
39 :format(name, desc))
40 end
41 rawset(self, name, value)
42 end
45 function meta:__index(name)
46 local info = getinfo(2, "Sl")
47 local warn_key = ("%s\0%d\0%s"):format(info.source, info.currentline, name)
48 if not declared[name] and not warned[warn_key] and info.what ~= "C" then
49 core.log("warning", ("Undeclared global variable %q accessed at %s:%s")
50 :format(name, info.short_src, info.currentline))
51 warned[warn_key] = true
52 end
53 return rawget(self, name)
54 end
56 setmetatable(_G, meta)