2012-06-11 01:58:12 -05:00
|
|
|
class V8::Conversion
|
|
|
|
module Object
|
2012-06-12 04:18:17 -05:00
|
|
|
include V8::Util::Weakcell
|
|
|
|
|
2012-06-11 01:58:12 -05:00
|
|
|
def to_v8
|
2012-06-12 03:44:50 -05:00
|
|
|
object = to_v8_template.NewInstance()
|
|
|
|
V8::Context.link self, object
|
|
|
|
return object
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_v8_template
|
2012-06-12 04:18:17 -05:00
|
|
|
weakcell(:v8_template) do
|
|
|
|
V8::C::ObjectTemplate::New().tap do |template|
|
|
|
|
data = V8::C::External::New(self)
|
|
|
|
template.SetNamedPropertyHandler(Get, Set, nil, nil, nil, data)
|
|
|
|
template.SetIndexedPropertyHandler(IGet, ISet, nil, nil, nil, data)
|
|
|
|
end
|
2012-06-12 03:44:50 -05:00
|
|
|
end
|
2012-06-11 01:58:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_ruby
|
|
|
|
self
|
|
|
|
end
|
2012-06-11 05:06:07 -05:00
|
|
|
|
2012-06-15 05:33:18 -05:00
|
|
|
module Accessor
|
|
|
|
include V8::Error::Protect
|
|
|
|
def intercept(info, key, &block)
|
|
|
|
context = V8::Context.current
|
|
|
|
access = context.access
|
|
|
|
object = info.Data().Value()
|
|
|
|
handles_property = true
|
|
|
|
dontintercept = proc do
|
|
|
|
handles_property = false
|
|
|
|
end
|
|
|
|
protect do
|
|
|
|
result = block.call(context, access, object, context.to_ruby(key), dontintercept)
|
|
|
|
handles_property ? context.to_v8(result) : V8::C::Value::Empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-11 05:06:07 -05:00
|
|
|
class Get
|
2012-06-15 05:33:18 -05:00
|
|
|
extend Accessor
|
2012-06-11 05:06:07 -05:00
|
|
|
def self.call(property, info)
|
2012-06-15 05:33:18 -05:00
|
|
|
intercept(info, property) do |context, access, object, key, dontintercept|
|
|
|
|
access.get(object, key, &dontintercept)
|
2012-06-11 05:06:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-06-11 16:21:06 -05:00
|
|
|
|
|
|
|
class Set
|
2012-06-15 05:33:18 -05:00
|
|
|
extend Accessor
|
2012-06-11 16:21:06 -05:00
|
|
|
def self.call(property, value, info)
|
2012-06-15 05:33:18 -05:00
|
|
|
intercept(info, property) do |context, access, object, key, dontintercept|
|
|
|
|
access.set(object, key, context.to_ruby(value), &dontintercept)
|
2012-06-11 16:21:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-06-12 04:18:17 -05:00
|
|
|
|
|
|
|
class IGet
|
2012-06-15 05:33:18 -05:00
|
|
|
extend Accessor
|
|
|
|
def self.call(property, info)
|
|
|
|
intercept(info, property) do |context, access, object, key, dontintercept|
|
|
|
|
access.iget(object, key, &dontintercept)
|
2012-06-12 04:18:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ISet
|
2012-06-15 05:33:18 -05:00
|
|
|
extend Accessor
|
|
|
|
def self.call(property, value, info)
|
|
|
|
intercept(info, property) do |context, access, object, key, dontintercept|
|
|
|
|
access.iset(object, key, context.to_ruby(value), &dontintercept)
|
2012-06-12 04:18:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-06-11 01:58:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
module NativeObject
|
|
|
|
def to_ruby
|
2012-06-12 07:06:25 -05:00
|
|
|
wrap = if IsArray()
|
|
|
|
::V8::Array
|
|
|
|
elsif IsFunction()
|
|
|
|
::V8::Function
|
|
|
|
else
|
|
|
|
::V8::Object
|
|
|
|
end
|
|
|
|
wrap.new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_v8
|
|
|
|
self
|
2012-06-11 01:58:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|