Schokokex / addon_template_butt

A template for Dota But Mods. Initiated by Baumis Twitch/Youtube channel and the community.
https://www.twitch.tv/l34um1
22 stars 13 forks source link

Immutation #47

Open Schokokex opened 4 years ago

Schokokex commented 4 years ago

shallow:


local immutables = {}
local newProp = function(gl,ke,va)
 if immutables[ke] then return nil end
 gl[ke] = va
end
_G.immute = function(name)
 if(nil~=_G[name]) then
  immutables[name] = true
 else
  error(name.." doesn't exist on a global scope")
 end
end
_G.immute("immute")
setmetatable(_G, {__newindex = newProp})
Schokokex commented 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)
Schokokex commented 4 years ago

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
Schokokex commented 4 years ago

-- 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
})