mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
make V8::Object Enumerable
This commit is contained in:
parent
b32060c925
commit
11dff1e592
3 changed files with 25 additions and 8 deletions
|
@ -1,7 +1,7 @@
|
||||||
class V8::Array < V8::Object
|
class V8::Array < V8::Object
|
||||||
def each
|
def each
|
||||||
@context.enter do
|
@context.enter do
|
||||||
for i in 0..(@native.Length() - 1)
|
0.upto(@native.Length() - 1) do |i|
|
||||||
yield @context.to_ruby(@native.Get(i))
|
yield @context.to_ruby(@native.Get(i))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,11 +4,7 @@ module V8::Conversion
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_v8(ruby_object)
|
def to_v8(ruby_object)
|
||||||
if ruby_object.respond_to?(:to_v8)
|
ruby_object.respond_to?(:to_v8) ? ruby_object.to_v8 : V8::C::Object::New()
|
||||||
ruby_object.method(:to_v8).arity == 1 ? ruby_object.to_v8(self) : ruby_object.to_v8
|
|
||||||
else
|
|
||||||
V8::C::Object::New()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -61,12 +57,22 @@ class V8::C::Array
|
||||||
end
|
end
|
||||||
|
|
||||||
class Array
|
class Array
|
||||||
def to_v8(context)
|
def to_v8
|
||||||
|
context = V8::Context.current
|
||||||
array = V8::C::Array::New(length)
|
array = V8::C::Array::New(length)
|
||||||
each_with_index do |item, i|
|
each_with_index do |item, i|
|
||||||
rputs "i: #{item}"
|
|
||||||
array.Set(i, context.to_v8(item))
|
array.Set(i, context.to_v8(item))
|
||||||
end
|
end
|
||||||
return array
|
return array
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Hash
|
||||||
|
def to_v8
|
||||||
|
object = V8::Object.new(V8::C::Object::New())
|
||||||
|
each do |key, value|
|
||||||
|
object[key] = value
|
||||||
|
end
|
||||||
|
return object.to_v8
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,4 +1,5 @@
|
||||||
class V8::Object
|
class V8::Object
|
||||||
|
include Enumerable
|
||||||
attr_reader :native
|
attr_reader :native
|
||||||
|
|
||||||
def initialize(native)
|
def initialize(native)
|
||||||
|
@ -18,4 +19,14 @@ class V8::Object
|
||||||
end
|
end
|
||||||
return value
|
return value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def each
|
||||||
|
@context.enter do
|
||||||
|
names = @native.GetPropertyNames()
|
||||||
|
0.upto(@native.Length() - 1) do |i|
|
||||||
|
name = names.Get(i)
|
||||||
|
yield @context.to_ruby(name), @context.to_ruby(@native.Get(name))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
Loading…
Reference in a new issue