From eb9b7b74906adc642cd63a4eaba318a42c1d0f94 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Fri, 8 Jun 2012 09:10:49 -0500 Subject: [PATCH] DRY up array conversion --- lib/v8/array.rb | 11 +++++++++++ lib/v8/conversion.rb | 7 +++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/v8/array.rb b/lib/v8/array.rb index ea17a12..dd445ec 100644 --- a/lib/v8/array.rb +++ b/lib/v8/array.rb @@ -1,4 +1,15 @@ class V8::Array < V8::Object + + def initialize(native_or_length = nil) + if native_or_length.is_a?(Numeric) + super V8::C::Array::New(native.to_i) + elsif native_or_length.is_a?(V8::C::Array) + super native_or_length + else + super V8::C::Array::New() + end + end + def each @context.enter do 0.upto(@native.Length() - 1) do |i| diff --git a/lib/v8/conversion.rb b/lib/v8/conversion.rb index b7e3721..377b0a9 100644 --- a/lib/v8/conversion.rb +++ b/lib/v8/conversion.rb @@ -64,12 +64,11 @@ end class Array def to_v8 - context = V8::Context.current - array = V8::C::Array::New(length) + array = V8::Array.new(length) each_with_index do |item, i| - array.Set(i, context.to_v8(item)) + array[i] = item end - return array + return array.to_v8 end end