Updated readme.

This commit is contained in:
Bruce Hill 2018-02-12 22:02:13 -08:00
parent 45d410d00c
commit c0397a7bf7

View File

@ -99,28 +99,23 @@ function immutable(fields, class_fields)
__index = function(self, key)
local cls = getmetatable(self)
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]]
else
return cls[key]
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
}
for k,v in pairs(class_fields) do cls[k] = v end
cls.__fields, cls.__indices = fields, {}
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'})
self[key] = bucket
return bucket
end})
cls.__buckets = setmetatable({}, {__mode='k'})
return setmetatable(cls, {
__call=function(cls, ...)
local hash = make_hash(...)
@ -136,6 +131,7 @@ function immutable(fields, class_fields)
if match then return userdata end
end
local userdata = setmetatable(new_userdata(hash), cls)
cls.__buckets[userdata] = bucket
bucket[userdata] = {...}
return userdata
end