2012-06-07 11:09:13 -05:00
|
|
|
class V8::Object
|
2012-06-07 11:40:53 -05:00
|
|
|
include Enumerable
|
2012-06-07 11:09:13 -05:00
|
|
|
attr_reader :native
|
2012-06-11 01:58:12 -05:00
|
|
|
alias_method :to_v8, :native
|
2012-06-07 11:09:13 -05:00
|
|
|
|
2012-06-07 11:47:46 -05:00
|
|
|
def initialize(native = nil)
|
|
|
|
@context = V8::Context.current or fail "tried to initialize a #{self.class} without being in an entered V8::Context"
|
|
|
|
@native = native || V8::C::Object::New()
|
2012-06-11 05:06:07 -05:00
|
|
|
@context.link self, @native
|
2012-06-07 11:09:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def [](key)
|
|
|
|
@context.enter do
|
|
|
|
@context.to_ruby @native.Get(@context.to_v8(key))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def []=(key, value)
|
|
|
|
@context.enter do
|
|
|
|
@native.Set(@context.to_v8(key), @context.to_v8(value))
|
|
|
|
end
|
|
|
|
return value
|
|
|
|
end
|
2012-06-07 11:40:53 -05:00
|
|
|
|
|
|
|
def each
|
|
|
|
@context.enter do
|
|
|
|
names = @native.GetPropertyNames()
|
2012-06-11 01:58:12 -05:00
|
|
|
0.upto(names.Length() - 1) do |i|
|
2012-06-07 11:40:53 -05:00
|
|
|
name = names.Get(i)
|
|
|
|
yield @context.to_ruby(name), @context.to_ruby(@native.Get(name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-06-11 01:58:12 -05:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
@context.enter do
|
|
|
|
"#{self.class}#{@native.ToString().Utf8Value()}"
|
|
|
|
end
|
|
|
|
end
|
2012-06-07 11:09:13 -05:00
|
|
|
end
|