diff options
Diffstat (limited to 'lib/weakref.rb')
-rw-r--r-- | lib/weakref.rb | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/lib/weakref.rb b/lib/weakref.rb index c31e959e74..c6fe8cd21b 100644 --- a/lib/weakref.rb +++ b/lib/weakref.rb @@ -2,11 +2,12 @@ # # Usage: # foo = Object.new -# foo.hash +# foo = Object.new +# p foo.to_s # original's class # foo = WeakRef.new(foo) -# foo.hash +# p foo.to_s # should be same class # ObjectSpace.garbage_collect -# foo.hash # => Raises WeakRef::RefError (because original GC'ed) +# p foo.to_s # should raise exception (recycled) require "delegate" @@ -15,27 +16,31 @@ class WeakRef<Delegator class RefError<StandardError end - ID_MAP = {} - ID_REV_MAP = {} + ID_MAP = {} # obj -> [ref,...] + ID_REV_MAP = {} # ref -> obj ObjectSpace.add_finalizer(lambda{|id| - rid = ID_MAP[id] - if rid - ID_REV_MAP[rid] = nil + rids = ID_MAP[id] + if rids + for rid in rids + ID_REV_MAP[rid] = nil + end ID_MAP[id] = nil end rid = ID_REV_MAP[id] if rid ID_REV_MAP[id] = nil - ID_MAP[rid] = nil + ID_MAP[rid].delete(id) + ID_MAP[rid] = nil if ID_MAP[rid].empty? end }) - + def initialize(orig) super @__id = orig.__id__ ObjectSpace.call_finalizer orig ObjectSpace.call_finalizer self - ID_MAP[@__id] = self.__id__ + ID_MAP[@__id] = [] unless ID_MAP[@__id] + ID_MAP[@__id].concat self.__id__ ID_REV_MAP[self.id] = @__id end @@ -60,10 +65,11 @@ class WeakRef<Delegator end if __FILE__ == $0 + require 'thread' foo = Object.new - p foo.hash # original's hash value + p foo.to_s # original's class foo = WeakRef.new(foo) - p foo.hash # should be same hash value + p foo.to_s # should be same class ObjectSpace.garbage_collect - p foo.hash # should raise exception (recycled) + p foo.to_s # should raise exception (recycled) end |