/game/scripts/nil.lua
https://bitbucket.org/vivkin/gam3b00bs/ · Lua · 42 lines · 24 code · 11 blank · 7 comment · 2 complexity · 9a56e37cacfd5b237c377e65bdfd482e MD5 · raw file
- --
- -- nil.lua: restricts global var writes and catches undefined global var access
- --
- --
-
- print "nil.lua"
-
- do
- local mt = {};
- mt.__index = function( t, k ) error("Attempt to access global '" .. k .. "' (nil)") end;
- mt.__newindex = function( t, k, v ) error("Attempt to write global '" .. k .. "'" ) end;
-
- setmetatable( _G, mt );
-
- rawset( _G, "global", function(expr)
- for k,v in pairs(expr) do
- rawset( _G, k, v );
- end;
- end );
- end
-
- -- Tests:
- if 1 then
-
- -- should raise an error
- local function test_for_global_access()
- XXXXXXXXXXX = 42
- end
-
- --- should raise an error
- local function test_for_uninitialized_global()
- local var = XXXXXXXXXXXXXXXXXX
- end
-
- local result,message = pcall( test_for_global_access )
- assert( not result, "Test test_for_global_access FAILED!" )
-
- local result,message = pcall( test_for_uninitialized_global )
- assert( not result, "Test test_for_uninitialized_global failed!" )
-
- end