From c0397a7bf77a15b6dedb06d3ad5ad4a33da59ca3 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 12 Feb 2018 22:02:13 -0800 Subject: [PATCH] Updated readme. --- README.md | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bf52281..dc1b907 100644 --- a/README.md +++ b/README.md @@ -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