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

add basic conversions for V8::Object and V8::Array

This commit is contained in:
Charles Lowell 2012-06-07 11:09:13 -05:00
parent 73bf43d5bb
commit b32060c925
4 changed files with 73 additions and 3 deletions

View file

@ -2,4 +2,6 @@ require "v8/version"
require 'v8/init'
require 'v8/conversion'
require 'v8/context'
require 'v8/context'
require 'v8/object'
require 'v8/array'

13
lib/v8/array.rb Normal file
View file

@ -0,0 +1,13 @@
class V8::Array < V8::Object
def each
@context.enter do
for i in 0..(@native.Length() - 1)
yield @context.to_ruby(@native.Get(i))
end
end
end
def length
@native.Length()
end
end

View file

@ -4,14 +4,20 @@ module V8::Conversion
end
def to_v8(ruby_object)
ruby_object.respond_to?(:to_v8) ? ruby_object.to_v8 : case ruby_object
when Numeric then ruby_object
if ruby_object.respond_to?(:to_v8)
ruby_object.method(:to_v8).arity == 1 ? ruby_object.to_v8(self) : ruby_object.to_v8
else
V8::C::Object::New()
end
end
end
class Numeric
def to_v8
self
end
end
class V8::C::String
def to_ruby
self.Utf8Value()
@ -36,3 +42,31 @@ class Time
end
end
class V8::C::Object
def to_ruby
V8::Object.new(self)
end
end
class V8::Object
def to_v8
self.native
end
end
class V8::C::Array
def to_ruby
V8::Array.new(self)
end
end
class Array
def to_v8(context)
array = V8::C::Array::New(length)
each_with_index do |item, i|
rputs "i: #{item}"
array.Set(i, context.to_v8(item))
end
return array
end
end

21
lib/v8/object.rb Normal file
View file

@ -0,0 +1,21 @@
class V8::Object
attr_reader :native
def initialize(native)
@native = native
@context = V8::Context.current
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
end