Module:No globals: Difference between revisions

From N64brew Wiki
Jump to navigation Jump to search
Content added Content deleted
(Created page with 'local mt = getmetatable(_G) or {} local oldIndex = mt.__index or function() return nil end mt.__index = function(t, k) if k ~= 'name' and k ~= 'arg' then erro...')
 
m (8 revisions imported from wikipedia:Module:No_globals)
 
(9 intermediate revisions by 7 users not shown)
Line 1:
local mt = getmetatable(_G) or {}
local oldIndex =function mt.__index or(t, function(k) return nil end
if k ~= 'name' and k ~= 'arg' then
mt.__index = function(t, k)
if k ~= 'name' and k ~= 'arg' then
error('Tried to read nil global ' .. tostring(k), 2)
end
return oldIndex(t, k)nil
end
local oldNewindex = mt.__newindex or rawset
function mt.__newindex(t, k, v)
if k ~= 'name' and k ~= 'arg' then
error('Tried to write global ' .. tostring(k), 2)
end
oldNewindexrawset(t, k, v)
end
setmetatable(_G, mt)

Latest revision as of 21:05, 13 May 2021

Documentation for this module may be created at Module:No globals/doc

local mt = getmetatable(_G) or {}
function mt.__index (t, k)
	if k ~= 'arg' then
		error('Tried to read nil global ' .. tostring(k), 2)
	end
	return nil
end
function mt.__newindex(t, k, v)
	if k ~= 'arg' then
		error('Tried to write global ' .. tostring(k), 2)
	end
	rawset(t, k, v)
end
setmetatable(_G, mt)