Open Schokokex opened 4 years ago
local G = {}
local immutables = {}
setmetatable(_G,{
__index = G,
__newindex = function(t,k,v)
if immutables[_G[k]] then error("Can't modify "..k..", it's a const.",2) end
G[k] = v
end,
__metatable = false
})
for k,v in pairs(_G) do
G[k] = v
_G[k] = nil
end
_G.immute = function(obj)
immutables[obj] = true
end
immute(immute)
all globals are immutable once written
local G = {}
setmetatable(_G,{
__index = G,
__newindex = function(t,k,v)
if nil~=G[k] then error(k.." is already known",2) end
G[k] = v
end,
__metatable = false
})
for k,v in pairs(_G) do
G[k] = v
_G[k] = nil
end
-- How to proxy like a god in lua
-- 1: Get a local object.
local o = {a=1, b=2}
-- 2: Throw your object onto an empty table
local table1 = {}
-- 3: Do all her positions on the table, and mute her meanwhile
for k,v in pairs(o) do
table1[k] = v
o[k] = nil
end
-- 4: Tell everyone they can see what your
-- object is able of by looking at the table.
-- Tell everyone how to couple with the
-- object (what they're not able of).
-- Also make your object holy and untouchable.
setmetatable(o,{
__index = o,
__pairs = function(o) return
function(o, k) return next(table1, k) end,
o,
nil
end,
__ipairs = function(o) return
function(o,i)
i=i+1
if nil~=table1[i] then return i,table1[i] end
end,
o,
0
end,
__metatable = false
})
shallow: