1 local getinfo = debug.getinfo
3 function core.global_exists(name)
4 if type(name) ~= "string" then
5 error("core.global_exists: " .. tostring(name) .. " is not a string")
7 return rawget(_G, name) ~= nil
13 -- Key is source file, line, and variable name; seperated by NULs
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
24 core.log("warning", ("Assignment to undeclared "..
25 "global %q inside a function at %s.")
27 warned[warn_key] = true
31 rawset(self, name, value)
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
43 return rawget(self, name)
46 setmetatable(_G, meta)