求真百科歡迎當事人提供第一手真實資料,洗刷冤屈,終結網路霸凌。

模組:No globals檢視原始碼檢視歷史

事實揭露 揭密真相
於 2019年5月26日 (日) 10:06 由 mediawiki1>Antigng 所做的修訂 (ep:修正错误翻译)
前往: 導覽搜尋

This module causes an error if any nil global is read or if any global is written to, with the exception of arg. To use, add require('Module:No globals') to the top of the module using it. The arg variable is excluded because it is necessary for Scribunto's require function to work properly. (See the Scribunto source code here.)


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)