2012-06-20 04:35:40 -05:00
|
|
|
require 'ref'
|
|
|
|
|
2012-06-11 01:58:12 -05:00
|
|
|
class V8::Conversion
|
|
|
|
module Identity
|
|
|
|
def to_ruby(v8_object)
|
2012-06-20 04:35:40 -05:00
|
|
|
if v8_object.class <= V8::C::Object
|
|
|
|
v8_idmap[v8_object.GetIdentityHash()] || super(v8_object)
|
|
|
|
else
|
|
|
|
super(v8_object)
|
|
|
|
end
|
2012-06-11 05:06:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_v8(ruby_object)
|
2012-06-20 15:30:34 -05:00
|
|
|
return super(ruby_object) if ruby_object.is_a?(String) || ruby_object.is_a?(Primitive)
|
2012-06-20 04:35:40 -05:00
|
|
|
rb_idmap[ruby_object.object_id] || super(ruby_object)
|
2012-06-11 05:06:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def equate(ruby_object, v8_object)
|
2012-06-20 04:35:40 -05:00
|
|
|
v8_idmap[v8_object.GetIdentityHash()] = ruby_object
|
|
|
|
rb_idmap[ruby_object.object_id] = v8_object
|
2012-06-11 01:58:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def v8_idmap
|
2012-06-20 04:35:40 -05:00
|
|
|
@v8_idmap ||= Ref::WeakValueMap.new
|
2012-06-11 01:58:12 -05:00
|
|
|
end
|
|
|
|
|
2012-06-11 05:06:07 -05:00
|
|
|
def rb_idmap
|
2012-06-20 04:35:40 -05:00
|
|
|
@ruby_idmap ||= Ref::WeakValueMap.new
|
2012-06-11 01:58:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|