1 local getinfo, rawget, rawset = debug.getinfo, rawget, rawset
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; separated by NULs
16 function meta:__newindex(name, value)
17 rawset(self, name, value)
18 if declared[name] then
21 local info = getinfo(2, "Sl")
22 local desc = ("%s:%d"):format(info.short_src, info.currentline)
23 local warn_key = ("%s\0%d\0%s"):format(info.source, info.currentline, name)
24 if not warned[warn_key] and info.what ~= "main" and info.what ~= "C" then
25 core.log("warning", ("Assignment to undeclared global %q inside a function at %s.")
27 warned[warn_key] = true
33 function meta:__index(name)
34 if declared[name] then
37 local info = getinfo(2, "Sl")
38 local warn_key = ("%s\0%d\0%s"):format(info.source, info.currentline, name)
39 if not warned[warn_key] and info.what ~= "C" then
40 core.log("warning", ("Undeclared global variable %q accessed at %s:%s")
41 :format(name, info.short_src, info.currentline))
42 warned[warn_key] = true
46 setmetatable(_G, meta)