1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00
therubyracer/lib/v8/conversion/indentity.rb
Charles Lowell 87efe139e9 avoid potential race condition.
in between the weakref_alive? check and the actual
dereference via `__getobj__` the weak reference
could have been collected. It's better to just go
ahead and do the deref. If it throws an error,
then we know it didn't work.
2012-06-19 08:33:48 -05:00

57 lines
No EOL
1.1 KiB
Ruby

class V8::Conversion
module Identity
def to_ruby(v8_object)
v8_idmap[v8_object] || super
end
def to_v8(ruby_object)
rb_idmap[ruby_object] || super
end
def equate(ruby_object, v8_object)
v8_idmap.equate(v8_object, ruby_object)
rb_idmap.equate(ruby_object, v8_object)
end
def v8_idmap
@v8_idmap ||= V8IDMap.new
end
def rb_idmap
@ruby_idmap ||= RubyIDMap.new
end
class IDMap
def initialize
@map = {}
end
def [](object)
weakref = @map[to_key(object)]
weakref.__getobj__ if weakref
rescue WeakRef::RefError
nil #peer was garbage collected, so no current mapping.
end
def equate(key_object, value_object)
@map[to_key(key_object)] = WeakRef.new(value_object)
end
end
class RubyIDMap < IDMap
def to_key(object)
object.object_id
end
end
class V8IDMap < IDMap
def to_key(object)
object.GetIdentityHash()
end
def [](v8_object)
super if v8_object.is_a?(V8::C::Object)
end
end
end
end