1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00
therubyracer/lib/v8/conversion/object.rb

104 lines
2.5 KiB
Ruby
Raw Normal View History

class V8::Conversion
module Object
2012-06-12 04:18:17 -05:00
include V8::Util::Weakcell
def to_v8
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
end
end
def to_ruby
self
end
2012-06-11 05:06:07 -05:00
class Get
2012-06-14 22:34:38 -05:00
extend V8::Error::Protect
2012-06-11 05:06:07 -05:00
def self.call(property, info)
2012-06-14 22:34:38 -05:00
protect do
context = V8::Context.current
access = context.access
object = info.Data().Value()
name = property.Utf8Value()
dontintercept = proc do
return V8::C::Value::Empty
end
context.to_v8 access.get(object, name, &dontintercept)
2012-06-11 05:06:07 -05:00
end
end
end
2012-06-11 16:21:06 -05:00
class Set
2012-06-14 22:34:38 -05:00
extend V8::Error::Protect
2012-06-11 16:21:06 -05:00
def self.call(property, value, info)
2012-06-14 22:34:38 -05:00
protect do
context = V8::Context.current
access = context.access
object = info.Data().Value()
name = property.Utf8Value()
dontintercept = proc do
return V8::C::Value::Empty
end
access.set(object, name, 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-14 22:34:38 -05:00
extend V8::Error::Protect
2012-06-12 04:18:17 -05:00
def self.call(index, info)
2012-06-14 22:34:38 -05:00
protect do
context = V8::Context.current
access = context.access
object = info.Data().Value()
dontintercept = proc do
return V8::C::Value::Empty
end
context.to_v8 access.iget(object, index, &dontintercept)
2012-06-12 04:18:17 -05:00
end
end
end
class ISet
2012-06-14 22:34:38 -05:00
extend V8::Error::Protect
2012-06-12 04:18:17 -05:00
def self.call(index, value, info)
2012-06-14 22:34:38 -05:00
protect do
context = V8::Context.current
access = context.access
object = info.Data().Value()
dontintercept = proc do
return V8::C::Value::Empty
end
access.iset(object, index, context.to_ruby(value), &dontintercept)
2012-06-12 04:18:17 -05:00
end
end
end
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
end
end
end