Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 23, 2010, 11:46:11 AM (15 years ago)
Author:
rgrieder
Message:

Activated Strict.lua
That means you can no longer assign to or access an undeclared global variable on function scope.
Declaring: either assign anything to the variable (including nil) on file scope
or use the function global("myVar", "myVar2", …, "myVarN") (mind the strings!)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gamestates3/data/lua/Strict.lua

    r6746 r6774  
    1313end
    1414
    15 __STRICT = false
     15__STRICT = true
    1616mt.__declared = {}
    1717
    1818mt.__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)
     19  if not mt.__declared[n] then
     20    if __STRICT then
     21      local d = debug.getinfo(2, "S")
     22      local w = d and d.what or "C"
     23      if w ~= "main" and w ~= "C" then
     24        error("Assigning to undeclared global variable '"..n.."'", 2)
     25      end
    2426    end
    2527    mt.__declared[n] = true
     
    2931 
    3032mt.__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  if not mt.__declared[n] then
     34    local d = debug.getinfo(2, "S")
     35    local w = d and d.what or "C"
     36    if w ~= "C" then
     37      error("Global variable '"..n.."' was not declared", 2)
     38    else
     39      mt.__declared[n] = true
     40    end
    3341  end
    3442  return rawget(t, n)
     
    3644
    3745function global(...)
    38    for _, v in ipairs{...} do mt.__declared[v] = true end
     46  for _, v in ipairs{...} do
     47    mt.__declared[v] = true
     48  end
    3949end
Note: See TracChangeset for help on using the changeset viewer.