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) {
|
VALUE rr_reflect_v8_object(Handle<Value> value) {
|
||||||
Local<Object> object(Object::Cast(*value));
|
Local<Object> object(Object::Cast(*value));
|
||||||
Local<Value> peer = object->GetHiddenValue(String::New("TheRubyRacer::RubyObject"));
|
Local<Value> peer = object->GetHiddenValue(String::New("TheRubyRacer::RubyObject"));
|
||||||
if (peer.IsEmpty()) {
|
return peer.IsEmpty() ? rr_v8_ref_create(rr_cV8_C_Object, object) : (VALUE)External::Unwrap(peer);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,10 @@ require 'stringio'
|
||||||
|
|
||||||
module V8
|
module V8
|
||||||
class Context
|
class Context
|
||||||
attr_reader :native
|
attr_reader :native, :scope
|
||||||
def initialize(opts = {})
|
def initialize(opts = {})
|
||||||
@native = C::Context::New(opts[:with])
|
@native = C::Context::New(opts[:with])
|
||||||
|
@scope = V8::Object.new(@native.Global(), @native)
|
||||||
yield(self) if block_given?
|
yield(self) if block_given?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -3,26 +3,28 @@ module V8
|
||||||
class Object
|
class Object
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
|
||||||
def initialize(native)
|
def initialize(native, context = nil)
|
||||||
@native = native
|
@native = native
|
||||||
|
@context = context || C::Context::GetEntered()
|
||||||
|
raise ScriptError, "V8::Object.new called without an open V8 context" unless @context
|
||||||
end
|
end
|
||||||
|
|
||||||
def [](key)
|
def [](key)
|
||||||
@native.context.enter do
|
@context.enter do
|
||||||
To.ruby(@native.Get(To.v8(key)))
|
To.ruby(@native.Get(To.v8(key)))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def []=(key, value)
|
def []=(key, value)
|
||||||
value.tap do
|
value.tap do
|
||||||
@native.context.enter do
|
@context.enter do
|
||||||
@native.Set(To.v8(key), To.v8(value))
|
@native.Set(To.v8(key), To.v8(value))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
@native.context.enter do
|
@context.enter do
|
||||||
To.rb(@native.ToString())
|
To.rb(@native.ToString())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,12 +4,7 @@ module V8
|
||||||
class << self
|
class << self
|
||||||
def ruby(value)
|
def ruby(value)
|
||||||
case value
|
case value
|
||||||
when V8::C::Function
|
when V8::C::Function then V8::Function.new(value)
|
||||||
V8::Function.new(value).tap do |f|
|
|
||||||
f.instance_eval do
|
|
||||||
@native.instance_variable_set(:@context, C::Context::GetEntered())
|
|
||||||
end
|
|
||||||
end
|
|
||||||
when V8::C::Array then V8::Array.new(value)
|
when V8::C::Array then V8::Array.new(value)
|
||||||
when V8::C::Object then V8::Object.new(value)
|
when V8::C::Object then V8::Object.new(value)
|
||||||
when V8::C::String then value.Utf8Value()
|
when V8::C::String then value.Utf8Value()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue