“模块:No globals”的版本间的差异

来自中文乐谱库
wiki_>Antigng
(ep:修正错误翻译)
 
wiki_>MusikAnimal
(Undid revision 948472525 by [[Special:Contributions/w>DiBabelYurikBot|w>DiBabelYurikBot]] ([[User talk:w>DiBabelYurikBot|talk]]))
第2行: 第2行:
 
function mt.__index (t, k)
 
function mt.__index (t, k)
 
if k ~= 'arg' then
 
if k ~= 'arg' then
error('尝试读取空全局变量:' .. tostring(k), 2)
+
error('Tried to read nil global ' .. tostring(k), 2)
 
end
 
end
 
return nil
 
return nil
第8行: 第8行:
 
function mt.__newindex(t, k, v)
 
function mt.__newindex(t, k, v)
 
if k ~= 'arg' then
 
if k ~= 'arg' then
error('尝试写入全局变量:' .. tostring(k), 2)
+
error('Tried to write global ' .. tostring(k), 2)
 
end
 
end
 
rawset(t, k, v)
 
rawset(t, k, v)
 
end
 
end
 
setmetatable(_G, mt)
 
setmetatable(_G, mt)

2020年4月1日 (三) 14:28的版本

Lua错误:无法创建进程:proc_open(/dev/null): failed to open stream: Operation not permitted

本模块将读取空全域变量和写入全局变量视为错误。要使用本模块,在模块上方添加
require('Module:No globals')
即可。排除arg变量是因为让 Scribunto require功能正确运行是必要的。(在此参阅 Scribunto 代码。)



脚本错误:Lua错误:无法创建进程:proc_open(/dev/null): failed to open stream: Operation not permitted

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)