Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/data/lua/Strict.lua @ 8986

Last change on this file since 8986 was 6746, checked in by rgrieder, 15 years ago

Merged gamestates2 branch back to trunk.
This brings in some heavy changes in the GUI framework.
It should also fix problems with triggered asserts in the InputManager.

Note: PickupInventory does not seem to work —> Segfault when showing because before, the owner in GUIOverlay::setGUIName is already NULL.
I haven't tested it before, so I can't tell whether it's my changes.

  • Property svn:eol-style set to native
File size: 948 bytes
Line 
1--
2-- strict.lua
3-- checks uses of undeclared global variables
4-- All global variables must be 'declared' through a regular assignment
5-- (even assigning nil will do) in a main chunk before being used
6-- anywhere or assigned to inside a function.
7--
8
9local mt = getmetatable(_G)
10if mt == nil then
11  mt = {}
12  setmetatable(_G, mt)
13end
14
15__STRICT = false
16mt.__declared = {}
17
18mt.__newindex = function (t, n, v)
19  if __STRICT and not mt.__declared[n] then
20    local d = debug.getinfo(2, "S")
21    local w = d and d.what or "C"
22    if w ~= "main" and w ~= "C" then
23      error("assign to undeclared variable '"..n.."'", 2)
24    end
25    mt.__declared[n] = true
26  end
27  rawset(t, n, v)
28end
29 
30mt.__index = function (t, n)
31  if not mt.__declared[n] and debug.getinfo(2, "S").what ~= "C" then
32    error("variable '"..n.."' is not declared", 2)
33  end
34  return rawget(t, n)
35end
36
37function global(...)
38   for _, v in ipairs{...} do mt.__declared[v] = true end
39end
Note: See TracBrowser for help on using the repository browser.