PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/game/scripts/nil.lua

https://bitbucket.org/vivkin/gam3b00bs/
Lua | 42 lines | 24 code | 11 blank | 7 comment | 2 complexity | 9a56e37cacfd5b237c377e65bdfd482e MD5 | raw file
 1--
 2-- nil.lua: restricts global var writes and catches undefined global var access
 3--
 4--
 5
 6print "nil.lua"
 7
 8do
 9    local mt = {};
10    mt.__index    = function( t, k ) error("Attempt to access global '" .. k .. "' (nil)") end;
11    mt.__newindex = function( t, k, v ) error("Attempt to write global '" .. k .. "'" ) end;
12
13    setmetatable( _G, mt );
14
15    rawset( _G, "global", function(expr)
16        for k,v in pairs(expr) do
17            rawset( _G, k, v );
18        end;
19    end );
20end
21
22-- Tests:
23if 1 then
24
25    -- should raise an error
26    local function test_for_global_access()
27        XXXXXXXXXXX = 42
28    end
29
30    --- should raise an error
31    local function test_for_uninitialized_global()
32        local var = XXXXXXXXXXXXXXXXXX
33    end
34  
35    local result,message = pcall( test_for_global_access )
36    assert( not result, "Test test_for_global_access FAILED!" )
37
38    local result,message = pcall( test_for_uninitialized_global )
39    assert( not result, "Test test_for_uninitialized_global failed!" )
40
41end
42