跳转到内容

Module:No globals

轻之舟百科,让知识轻装启航

本模块将读取空全域变量和写入全局变量视为错误。要使用本模块,在模块上方添加

require('Module:No globals')

即可。排除arg变量是因为让 Scribunto require功能正确运行是必要的。


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)