Module:No globals: Difference between revisions

From The Satanic Wiki
Jump to navigation Jump to search
No edit summary
Tag: Reverted
No edit summary
Tag: Manual revert
 
Line 1: Line 1:
--  <pre>
--------------------------------------------------------------------------------
--- No globals prevents nil global variables from being written to or read.
--  This module greatly reduces the chance of errors from overriding globals.
-- 
--  To use the module, add the following code below your module table
--  definition:
-- 
--  {{#tag:pre|require('Module:No globals')}}&#010;
-- 
--  The @{require} function sets the `arg` global in the package library when
--  loading a module - see the [source code](https://git.io/JfKu8) in Scribunto
--  core. To ensure Scribunto can load modules, the `arg` global is whitelisted.
-- 
--  @script            getmetatable(_G)
--  @alias              mt
--  @release            stable
--  @author            [[wikipedia:User:Jackmcbarn|Jackmcbarn]] (Wikipedia)
--  @author            [[User:Dessamator|Dessamator]]
--  @attribution        [[wikipedia:Module:No globals|Wikipedia]]
--  @todo              Remove `name` exception in all metamethods. On Fandom,
--                      legacy Scribunto has a typo in the package library
--                      where `name` is accessed instead of `modname` - see
--                      the [source code](https://git.io/JfKzh). This bug was
--                      fixed in MediaWiki 1.23
--                      (see [this commit](https://git.io/JUilh)).
local mt = getmetatable(_G) or {}
local mt = getmetatable(_G) or {}
 
function mt.__index (t, k)
-- begin dev wikia change
if k ~= 'arg' then
local siteVersion = mw.site.currentVersion
error('Tried to read nil global ' .. tostring(k), 2)
local isNameDisallowed = tonumber(siteVersion:match('^%d%.%d+')) >= 1.23
end
-- end dev wikia change
return nil
 
--- Read access restriction on @{_G} global table.
--  @function          mt.__index
--  @param              {table} t Global table - @{_G}.
--  @param              k Indexed key.
--  @error[opt,28]      {string} 'tried to read nil global $k'
function mt.__index(t, k)
    -- begin dev wikia change
    if k ~= 'arg' and (isNameDisallowed or k ~= 'name') then
    -- end dev wikia change
        error('Tried to read nil global ' .. tostring(k), 2)
    end
    return nil
end
end
--- Write access restriction on @{_G} global table.
--  @function          mt.__newindex
--  @param              {table} t Global table - @{_G}.
--  @param              k Indexed key.
--  @param              v Value to be assigned.
--  @error[opt,42]      {string} 'tried to write global $k'
function mt.__newindex(t, k, v)
function mt.__newindex(t, k, v)
    -- begin dev wikia change
if k ~= 'arg' then
    if k ~= 'arg' and (isNameDisallowed or k ~= 'name') then
error('Tried to write global ' .. tostring(k), 2)
    -- end dev wikia change
end
        error('Tried to write global ' .. tostring(k), 2)
rawset(t, k, v)
    end
    rawset(t, k, v)
end
end
setmetatable(_G, mt)
setmetatable(_G, mt)

Latest revision as of 23:03, 19 June 2021

Documentation for this module may be created at Module:No globals/doc

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)