5 ms·
Metatables are your friend. -- LuaJIT local oldG = _G local newG = setmetatable({}, { __index = oldG, }) setmetatable(oldG, {
by rweichler 9y ago
Metatables are your friend.
-- LuaJIT
local oldG = _G
local newG = setmetatable({}, {
__index = oldG,
})
setmetatable(oldG, {
__index = function(t, k)
return rawget(newG, k)
end,
__newindex = function()
error('attempt to assign global variable')
end,
})
_G = newG
If you want to assign a global variable, `x = 5` will error but `_G.x = 5` won't.