| 1 | -- strict.lua (fork of http://www.lua.org/extras/5.1/strict.lua) |
| 2 | -- checks uses of undeclared global variables |
| 3 | -- All global variables must be 'declared' through a regular assignment |
| 4 | -- (even assigning nil will do) in a main chunk before being used |
| 5 | -- anywhere or assigned to inside a function. |
| 6 | -- distributed under the Lua license: http://www.lua.org/license.html |
| 7 | -- Scribunto modifications: |
| 8 | -- * exempt arg, used by require() |
| 9 | -- * remove what(), since debug.getinfo isn't allowed |
| 10 | |
| 11 | local error, rawset, rawget = error, rawset, rawget |
| 12 | |
| 13 | local mt = getmetatable(_G) |
| 14 | if mt == nil then |
| 15 | mt = {} |
| 16 | setmetatable(_G, mt) |
| 17 | end |
| 18 | |
| 19 | mt.__newindex = function (t, n, v) |
| 20 | if n ~= "arg" then |
| 21 | error("assign to undeclared variable '"..n.."'", 2) |
| 22 | end |
| 23 | rawset(t, n, v) |
| 24 | end |
| 25 | |
| 26 | mt.__index = function (t, n) |
| 27 | if n ~= "arg" then |
| 28 | error("variable '"..n.."' is not declared", 2) |
| 29 | end |
| 30 | return rawget(t, n) |
| 31 | end |