# WeakRef is a class to represent a reference to an object that is not seen by
# the tracing phase of the garbage collector. This allows the referenced
# object to be garbage collected as if nothing is referring to it. Because
# WeakRef delegates method calls to the referenced object, it may be used in
# place of that object, i.e. it is of the same duck type.
# p foo.to_s # original's class
# p foo.to_s # should be same class
# ObjectSpace.garbage_collect
# p foo.to_s # should raise exception (recycled)
# RefError is raised if an object cannot be referenced by a WeakRef.
class RefError<StandardError
@@id_map = {} # obj -> [ref,...]
@@id_rev_map = {} # ref -> obj
__old_status = Thread.critical
@@id_map.delete(rid) if @@id_map[rid].empty?
Thread.critical = __old_status
# Create a new WeakRef from +orig+.
# Return the object this WeakRef references. Raises RefError if the object
# has been garbage collected. The object returned is the object to which
# method calls are delegated (see Delegator).
unless @@id_rev_map[self.__id__] == @__id
raise RefError, "Illegal Reference - probably recycled", caller(2)
ObjectSpace._id2ref(@__id)
raise RefError, "Illegal Reference - probably recycled", caller(2)
__old_status = Thread.critical
unless @@id_rev_map.key?(self)
ObjectSpace.define_finalizer obj, @@final
ObjectSpace.define_finalizer self, @@final
@@id_map[@__id] = [] unless @@id_map[@__id]
Thread.critical = __old_status
@@id_map[@__id].push self.__id__
@@id_rev_map[self.__id__] = @__id
# Returns true if the referenced object still exists, and false if it has
# been garbage collected.
@@id_rev_map[self.__id__] == @__id
p foo.to_s # original's class
p foo.to_s # should be same class
ObjectSpace.garbage_collect
p foo.to_s # should raise exception (recycled)