Home
Random
Log in
Settings
About NBITTRPG Wiki
Disclaimers
NBITTRPG Wiki
Search
Editing
Module:No globals
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
-- <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')}}
 -- -- 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 -- @note This module has been adapted as a library in -- [[mw:gerrit:q/834623|MediaWiki core]], called as -- `require('strict')`. -- @author [[wikipedia:User:Jackmcbarn|Jackmcbarn]] (Wikipedia) -- @author [[User:Dessamator|Dessamator]] -- @attribution [[wikipedia:Module:No globals|Wikipedia]] local mt = getmetatable(_G) or {} --- 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) if k ~= 'arg' then error('Tried to read nil global ' .. tostring(k), 2) end return nil 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) if k ~= 'arg' then error('Tried to write global ' .. tostring(k), 2) end rawset(t, k, v) end setmetatable(_G, mt)
Summary:
Please note that all contributions to NBITTRPG Wiki are considered to be released under the Creative Commons Attribution-ShareAlike (see
NBITTRPG Wiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Module:No globals/doc
(
edit
)