모듈:No globals: 두 판 사이의 차이

영어 위키백과>Minorax
m (7 revisions imported from meta:Module:No_globals)
m (판 2개를 가져왔습니다: 위키백과에서 가져옴)
2번째 줄: 2번째 줄:
function mt.__index (t, k)
function mt.__index (t, k)
if k ~= 'arg' then
if k ~= 'arg' then
-- perf optimization here and below: do not load Module:TNT unless there is an error
error('Tried to read nil global ' .. tostring(k), 2)
error(require('Module:TNT').format('I18n/No globals', 'err-read', tostring(k)), 2)
end
end
return nil
return nil
9번째 줄: 8번째 줄:
function mt.__newindex(t, k, v)
function mt.__newindex(t, k, v)
if k ~= 'arg' then
if k ~= 'arg' then
error(require('Module:TNT').format('I18n/No globals', 'err-write', 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년 3월 28일 (토) 20:14 판

이 모듈은 nil 전역 변수를 읽거나 전역 변수 기록을 할 경우(arg 제외) 오류를 발생시킵니다. 모듈 최상단에 require('Module:No globals')를 추가하여 이 모듈을 사용할 수 있습니다. arg 변수가 제외되는 이유는 Scribunto의 require 함수가 정상 동작해야 하기 때문에 그렇습니다. (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)
• 현재 페이지 URL 줄이기