Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: weakref.rb
require "delegate"
[0] Fix | Delete
[1] Fix | Delete
# WeakRef is a class to represent a reference to an object that is not seen by
[2] Fix | Delete
# the tracing phase of the garbage collector. This allows the referenced
[3] Fix | Delete
# object to be garbage collected as if nothing is referring to it. Because
[4] Fix | Delete
# WeakRef delegates method calls to the referenced object, it may be used in
[5] Fix | Delete
# place of that object, i.e. it is of the same duck type.
[6] Fix | Delete
#
[7] Fix | Delete
# Usage:
[8] Fix | Delete
#
[9] Fix | Delete
# foo = Object.new
[10] Fix | Delete
# foo = Object.new
[11] Fix | Delete
# p foo.to_s # original's class
[12] Fix | Delete
# foo = WeakRef.new(foo)
[13] Fix | Delete
# p foo.to_s # should be same class
[14] Fix | Delete
# ObjectSpace.garbage_collect
[15] Fix | Delete
# p foo.to_s # should raise exception (recycled)
[16] Fix | Delete
class WeakRef<Delegator
[17] Fix | Delete
[18] Fix | Delete
# RefError is raised if an object cannot be referenced by a WeakRef.
[19] Fix | Delete
class RefError<StandardError
[20] Fix | Delete
end
[21] Fix | Delete
[22] Fix | Delete
@@id_map = {} # obj -> [ref,...]
[23] Fix | Delete
@@id_rev_map = {} # ref -> obj
[24] Fix | Delete
@@final = lambda{|id|
[25] Fix | Delete
__old_status = Thread.critical
[26] Fix | Delete
Thread.critical = true
[27] Fix | Delete
begin
[28] Fix | Delete
rids = @@id_map[id]
[29] Fix | Delete
if rids
[30] Fix | Delete
for rid in rids
[31] Fix | Delete
@@id_rev_map.delete(rid)
[32] Fix | Delete
end
[33] Fix | Delete
@@id_map.delete(id)
[34] Fix | Delete
end
[35] Fix | Delete
rid = @@id_rev_map[id]
[36] Fix | Delete
if rid
[37] Fix | Delete
@@id_rev_map.delete(id)
[38] Fix | Delete
@@id_map[rid].delete(id)
[39] Fix | Delete
@@id_map.delete(rid) if @@id_map[rid].empty?
[40] Fix | Delete
end
[41] Fix | Delete
ensure
[42] Fix | Delete
Thread.critical = __old_status
[43] Fix | Delete
end
[44] Fix | Delete
}
[45] Fix | Delete
[46] Fix | Delete
# Create a new WeakRef from +orig+.
[47] Fix | Delete
def initialize(orig)
[48] Fix | Delete
super
[49] Fix | Delete
__setobj__(orig)
[50] Fix | Delete
end
[51] Fix | Delete
[52] Fix | Delete
# Return the object this WeakRef references. Raises RefError if the object
[53] Fix | Delete
# has been garbage collected. The object returned is the object to which
[54] Fix | Delete
# method calls are delegated (see Delegator).
[55] Fix | Delete
def __getobj__
[56] Fix | Delete
unless @@id_rev_map[self.__id__] == @__id
[57] Fix | Delete
raise RefError, "Illegal Reference - probably recycled", caller(2)
[58] Fix | Delete
end
[59] Fix | Delete
begin
[60] Fix | Delete
ObjectSpace._id2ref(@__id)
[61] Fix | Delete
rescue RangeError
[62] Fix | Delete
raise RefError, "Illegal Reference - probably recycled", caller(2)
[63] Fix | Delete
end
[64] Fix | Delete
end
[65] Fix | Delete
[66] Fix | Delete
def __setobj__(obj)
[67] Fix | Delete
@__id = obj.__id__
[68] Fix | Delete
__old_status = Thread.critical
[69] Fix | Delete
begin
[70] Fix | Delete
Thread.critical = true
[71] Fix | Delete
unless @@id_rev_map.key?(self)
[72] Fix | Delete
ObjectSpace.define_finalizer obj, @@final
[73] Fix | Delete
ObjectSpace.define_finalizer self, @@final
[74] Fix | Delete
end
[75] Fix | Delete
@@id_map[@__id] = [] unless @@id_map[@__id]
[76] Fix | Delete
ensure
[77] Fix | Delete
Thread.critical = __old_status
[78] Fix | Delete
end
[79] Fix | Delete
@@id_map[@__id].push self.__id__
[80] Fix | Delete
@@id_rev_map[self.__id__] = @__id
[81] Fix | Delete
end
[82] Fix | Delete
[83] Fix | Delete
# Returns true if the referenced object still exists, and false if it has
[84] Fix | Delete
# been garbage collected.
[85] Fix | Delete
def weakref_alive?
[86] Fix | Delete
@@id_rev_map[self.__id__] == @__id
[87] Fix | Delete
end
[88] Fix | Delete
end
[89] Fix | Delete
[90] Fix | Delete
if __FILE__ == $0
[91] Fix | Delete
require 'thread'
[92] Fix | Delete
foo = Object.new
[93] Fix | Delete
p foo.to_s # original's class
[94] Fix | Delete
foo = WeakRef.new(foo)
[95] Fix | Delete
p foo.to_s # should be same class
[96] Fix | Delete
ObjectSpace.garbage_collect
[97] Fix | Delete
p foo.to_s # should raise exception (recycled)
[98] Fix | Delete
end
[99] Fix | Delete
[100] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function