Trying to lock a variable (and it's value) in _G, by making a new environment and resetting a changed _G variable back to default, if attempted to change it.
-- CodingEvolution
-- Locking variables (and values) in _G
_G={} -- New _G
_G.x=1
_G.y=2
setfenv(1,setmetatable({},{
__index=function(t,k)
if k=='_G' then
return setmetatable({},{
__newindex=function(t,k,v)
if k=='x' then
print'This value is locked; changing back to default.'
rawset(t,k,_G[k]) -- should set back to 1, but doesn't?
else
print'changing global...'
rawset(t,k,v) -- again, should return normal. but returns nothing (nil).
end
end
})
end
return getfenv()[k] -- If not _G, return old environment and index.
end
}))
print(_G.y) --> "nil" shouldn't it be 2?
_G.x='Changed' --> as expected, it triggers the __newindex function.
print(_G.x) --> but even then, it still returns nil.
-- I have to be somewhat close, because it does detect when and if a value in _G changed. It just
-- Doesn't do anything about it.
Shouldn't rawset return the value of _G[k] from the previous environment? If not, why? Thank you for reading.
-- EDIT: You can run this code and test it here: http://repl.it/BCF6

1 comment
Thank you very much sir.