模块:No globals

来自魔法纪录中文Wiki
跳转至: 导航 搜索


本模块在读取非全域数据或写入全局变量出错时生成提示,含有变量arg的例外。为使用本模块,在模块上方添加
require('Module:No globals')
即可使用。arg变量被排除是因为让 Scribunto 请求功能正确运行是必要的。(在此参阅 Scribunto 代码。)



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)