Anonymous user
Module:IP: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
need to use __index to have a unique key which is really private - if outside code gets uniqueKey with getmetatable then they can set it in another table's __eq metamethod, thus foiling isSubnetObject
(mt can still be defined outside the constructor, though - it can just return true for uniqueKey instead of an upvalue) |
(need to use __index to have a unique key which is really private - if outside code gets uniqueKey with getmetatable then they can set it in another table's __eq metamethod, thus foiling isSubnetObject) |
||
Line 489:
-- is a Subnet object.
local uniqueKey = setmetatable({}, {__metatable = false})
__index = function (self, key)▼
if key == uniqueKey then▼
return true▼
end▼
end,▼
return self:getCIDR() == obj:getCIDR()▼
end,▼
return tostring(self) .. tostring(obj)▼
end,▼
__tostring = function (self)▼
return self:getCIDR()▼
end,▼
__metatable = false▼
-- Private static methods
local function isSubnetObject(val)
-- Return true if val is a Subnet object, and false otherwise.
return
end
Line 521 ⟶ 502:
-- arg - the argument to be validated
validateSubnet = makeValidationFunction('Subnet', isSubnetObject)
-- Metamethods that don't need upvalues
local function subnetEquals(subnet1, subnet2)
local function concatenateSubnets(subnet1, subnet2)
local function subnetToString(subnet)
-- Constructors
makeSubnetFromRaw = function (rawIP, bitLength)
-- Set up structure
local obj =
local data = {
rawIP = rawIP,
Line 641 ⟶ 635:
end
return setmetatable(obj, {
▲ __index = function (self, key)
▲ if key == uniqueKey then
▲ return true
▲ end
▲ end,
__eq = subnetEquals,
__concat = concatenateSubnets,
▲ __metatable = false,
})
end
|