Debian Codegraph @

CorsixTH/Lua/strict.lua

raw
1 --[[ Copyright (c) 2009 Peter "Corsix" Cawley
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 -- Force local variables to be used for everything (except for explicitly
22 -- declared variables and any code running in derestriced mode). This helps to
23 -- catch typos in variable names, and promotes usage of locals over globals
24 -- (which improves speed).
25
26 local rawset, error, tostring
27 = rawset, error, tostring
28 local strict_mt = {}
29 local allowed_globals = setmetatable({}, {__mode = "k"})
30
31 local function newindex(t, k, v)
32 if allowed_globals[k] then
33 rawset(t, k, v)
34 else
35 error("assign to undeclared variable \'" .. tostring(k) .. "\'", 2)
36 end
37 end
38
39 local function index(_, k)
40 if allowed_globals[k] then
41 return nil
42 else
43 error("use of undeclared variable \'" .. tostring(k) .. "\'", 2)
44 end
45 end
46
47 local function restrict(ni, i, ...)
48 strict_mt.__newindex = ni
49 strict_mt.__index = i
50 return ...
51 end
52 restrict(newindex, index)
53
54 --!! Wrap a function so that it is freely able to set global variables
55 --[[ Some existing functions (for example, `require`) should be allowed to read
56 and write global variables without having to worry about declaring them
57 with `strict_declare_global`.
58 !param fn The function which should be able to freely set globals
59 !return A new function which acts just as `fn` and is free to set globals
60 !example
61 require = destrict(require) ]]
62 function destrict(fn)
63 return function(...)
64 local ni, i = strict_mt.__newindex, strict_mt.__index
65 strict_mt.__newindex, strict_mt.__index = nil, nil
66 return restrict(ni, i, fn(...))
67 end
68 end
69
70 --!! Declare a global variable so that it can later be used
71 --[[!param name The name of the global to declare
72 !example
73 strict_declare_global "some_var"
74 some_var = 42 ]]
75 function strict_declare_global(name)
76 allowed_globals[name] = true
77 end
78
79 setmetatable(_G, strict_mt)