Updated readme.
This commit is contained in:
parent
45d410d00c
commit
c0397a7bf7
12
README.md
12
README.md
@ -99,28 +99,23 @@ function immutable(fields, class_fields)
|
|||||||
__index = function(self, key)
|
__index = function(self, key)
|
||||||
local cls = getmetatable(self)
|
local cls = getmetatable(self)
|
||||||
if cls.indices[key] then
|
if cls.indices[key] then
|
||||||
local data = cls.__instances[extract_hash(self)]
|
local data = cls.__instances[extract_hash(self)][self]
|
||||||
return data[cls.indices[key]]
|
return data[cls.indices[key]]
|
||||||
else
|
else
|
||||||
return cls[key]
|
return cls[key]
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
__gc = function(self)
|
|
||||||
local __instances = getmetatable(self).__instances
|
|
||||||
local hash = extract_hash(self)
|
|
||||||
__instances[hash][self] = nil
|
|
||||||
if not next(__instances[hash]) then __instances[hash] = nil end
|
|
||||||
end,
|
|
||||||
-- also: __len, __pairs, __ipairs, __tostring, from_table, is_instance
|
-- also: __len, __pairs, __ipairs, __tostring, from_table, is_instance
|
||||||
}
|
}
|
||||||
for k,v in pairs(class_fields) do cls[k] = v end
|
for k,v in pairs(class_fields) do cls[k] = v end
|
||||||
cls.__fields, cls.__indices = fields, {}
|
cls.__fields, cls.__indices = fields, {}
|
||||||
for i,f in ipairs(fields) do cls.__indices[f] = i end
|
for i,f in ipairs(fields) do cls.__indices[f] = i end
|
||||||
cls.__instances = setmetatable({}, {__index=function(self, key)
|
cls.__instances = setmetatable({}, {__mode='v', __index=function(self, key)
|
||||||
local bucket = setmetatable({}, {__mode='k'})
|
local bucket = setmetatable({}, {__mode='k'})
|
||||||
self[key] = bucket
|
self[key] = bucket
|
||||||
return bucket
|
return bucket
|
||||||
end})
|
end})
|
||||||
|
cls.__buckets = setmetatable({}, {__mode='k'})
|
||||||
return setmetatable(cls, {
|
return setmetatable(cls, {
|
||||||
__call=function(cls, ...)
|
__call=function(cls, ...)
|
||||||
local hash = make_hash(...)
|
local hash = make_hash(...)
|
||||||
@ -136,6 +131,7 @@ function immutable(fields, class_fields)
|
|||||||
if match then return userdata end
|
if match then return userdata end
|
||||||
end
|
end
|
||||||
local userdata = setmetatable(new_userdata(hash), cls)
|
local userdata = setmetatable(new_userdata(hash), cls)
|
||||||
|
cls.__buckets[userdata] = bucket
|
||||||
bucket[userdata] = {...}
|
bucket[userdata] = {...}
|
||||||
return userdata
|
return userdata
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user