diff --git a/ext/v8/v8_obj.cpp b/ext/v8/v8_obj.cpp index 45e6d35..4e02475 100644 --- a/ext/v8/v8_obj.cpp +++ b/ext/v8/v8_obj.cpp @@ -77,13 +77,7 @@ void rr_init_obj() { VALUE rr_reflect_v8_object(Handle value) { Local object(Object::Cast(*value)); Local peer = object->GetHiddenValue(String::New("TheRubyRacer::RubyObject")); - if (peer.IsEmpty()) { - VALUE obj = V8_Ref_Create(rr_cV8_C_Object, object); - rb_iv_set(obj, "@context", rr_v82rb(Context::GetEntered())); - return obj; - } else { - return (VALUE)External::Unwrap(peer); - } + return peer.IsEmpty() ? rr_v8_ref_create(rr_cV8_C_Object, object) : (VALUE)External::Unwrap(peer); } diff --git a/lib/v8/array.rb b/lib/v8/array.rb index b19f1f3..2d0dc09 100644 --- a/lib/v8/array.rb +++ b/lib/v8/array.rb @@ -3,7 +3,7 @@ module V8 class Array < V8::Object def each - for i in 0..(@native.Length() - 1) + for i in 0..(@native.Length() - 1) yield To.ruby(@native.Get(i)) end end diff --git a/lib/v8/context.rb b/lib/v8/context.rb index 712a0c0..d7e27b7 100644 --- a/lib/v8/context.rb +++ b/lib/v8/context.rb @@ -2,12 +2,13 @@ require 'stringio' module V8 class Context - attr_reader :native + attr_reader :native, :scope def initialize(opts = {}) @native = C::Context::New(opts[:with]) + @scope = V8::Object.new(@native.Global(), @native) yield(self) if block_given? end - + def open @native.enter do yield(self) diff --git a/lib/v8/object.rb b/lib/v8/object.rb index f4ffe76..7c2ffb0 100644 --- a/lib/v8/object.rb +++ b/lib/v8/object.rb @@ -3,26 +3,28 @@ module V8 class Object include Enumerable - def initialize(native) + def initialize(native, context = nil) @native = native + @context = context || C::Context::GetEntered() + raise ScriptError, "V8::Object.new called without an open V8 context" unless @context end def [](key) - @native.context.enter do + @context.enter do To.ruby(@native.Get(To.v8(key))) end end def []=(key, value) value.tap do - @native.context.enter do + @context.enter do @native.Set(To.v8(key), To.v8(value)) end end end def to_s - @native.context.enter do + @context.enter do To.rb(@native.ToString()) end end diff --git a/lib/v8/to.rb b/lib/v8/to.rb index dc84066..37a5280 100644 --- a/lib/v8/to.rb +++ b/lib/v8/to.rb @@ -4,12 +4,7 @@ module V8 class << self def ruby(value) case value - when V8::C::Function - V8::Function.new(value).tap do |f| - f.instance_eval do - @native.instance_variable_set(:@context, C::Context::GetEntered()) - end - end + when V8::C::Function then V8::Function.new(value) when V8::C::Array then V8::Array.new(value) when V8::C::Object then V8::Object.new(value) when V8::C::String then value.Utf8Value()