mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
make the global javascript object available on each context as Context#scope
This commit is contained in:
parent
117876a04d
commit
f81cc92fca
5 changed files with 12 additions and 20 deletions
|
@ -77,13 +77,7 @@ void rr_init_obj() {
|
|||
VALUE rr_reflect_v8_object(Handle<Value> value) {
|
||||
Local<Object> object(Object::Cast(*value));
|
||||
Local<Value> 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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -2,9 +2,10 @@ 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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue