2 local _G_mt = getmetatable(_G)
5 -- Set the __globals metafield in the global environment's metatable,
6 -- if not already there.
8 if _G_mt.__globals then return else
9 print( "Warning: _G already has a metatable,"..
10 " which might interfere with xglobals")
14 _G_mt = { __globals = { } }
15 setmetatable(_G, _G_mt)
18 -- add a series of variable names to the list of declared globals
19 function _G_mt.__newglobal(...)
20 local g = _G_mt.__globals
21 for v in ivalues{...} do g[v]=true end
24 -- Try to set a global that's not in _G:
25 -- if it isn't declared, fail
26 function _G_mt.__newindex(_G, var, val)
27 if not _G_mt.__globals[var] then
28 error ("Setting undeclared global variable "..var)
33 -- Try to read a global that's not in _G:
34 -- if it isn't declared, fail
35 function _G_mt.__index(_G, var)
36 if not _G_mt.__globals[var] then
37 error ("Reading undeclared global variable "..var)