2012-06-11 02:58:12 -04:00
|
|
|
class V8::Conversion
|
|
|
|
module Identity
|
|
|
|
def to_ruby(v8_object)
|
2012-06-11 06:06:07 -04:00
|
|
|
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)
|
2012-06-11 02:58:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def v8_idmap
|
|
|
|
@v8_idmap ||= V8IDMap.new
|
|
|
|
end
|
|
|
|
|
2012-06-11 06:06:07 -04:00
|
|
|
def rb_idmap
|
|
|
|
@ruby_idmap ||= RubyIDMap.new
|
|
|
|
end
|
|
|
|
|
|
|
|
class IDMap
|
2012-06-11 02:58:12 -04:00
|
|
|
def initialize
|
2012-06-11 06:06:07 -04:00
|
|
|
@map = {}
|
2012-06-11 02:58:12 -04:00
|
|
|
end
|
|
|
|
|
2012-06-11 06:06:07 -04:00
|
|
|
def [](object)
|
|
|
|
weakref = @map[to_key(object)]
|
|
|
|
if weakref && weakref.weakref_alive?
|
|
|
|
weakref.__getobj__
|
2012-06-11 02:58:12 -04:00
|
|
|
end
|
|
|
|
end
|
2012-06-11 06:06:07 -04:00
|
|
|
|
|
|
|
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
|
2012-06-11 02:58:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|