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

57 lines
1.1 KiB
Ruby
Raw Normal View History

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)
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
def initialize
2012-06-11 06:06:07 -04:00
@map = {}
end
2012-06-11 06:06:07 -04:00
def [](object)
weakref = @map[to_key(object)]
if weakref && weakref.weakref_alive?
weakref.__getobj__
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
end
end
end