模組:No globals

被永久保护的模块
维基百科,自由的百科全书
文档图示 模块文档[查看] [编辑] [历史] [清除缓存]
本模块将读取空全域变量和写入全局变量视为错误。要使用本模块,在模块上方添加
require('Module:No globals')
即可。排除arg变量是因为让 Scribunto require功能正确运行是必要的。(在此参阅 Scribunto 代码。)

local mt = getmetatable(_G) or {}
function mt.__index (t, k)
	if k ~= 'arg' then
		error('尝试读取空全局变量:' .. tostring(k), 2)
	end
	return nil
end
function mt.__newindex(t, k, v)
	if k ~= 'arg' then
		error('尝试写入全局变量:' .. tostring(k), 2)
	end
	rawset(t, k, v)
end
setmetatable(_G, mt)