mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
embed constructors into javascript
This commit is contained in:
parent
d6ef0d97a4
commit
9a47fee91f
4 changed files with 26 additions and 11 deletions
|
@ -45,6 +45,6 @@ void rr_init_v8_array() {
|
|||
|
||||
VALUE rr_reflect_v8_array(Handle<Value> value) {
|
||||
Local<Array> array(Array::Cast(*value));
|
||||
Local<Value> peer = array->GetHiddenValue(String::New("TheRubyRacer::RubyObject"));
|
||||
Local<Value> peer = array->GetHiddenValue(String::NewSymbol("TheRubyRacer::RubyObject"));
|
||||
return peer.IsEmpty() ? rr_v8_ref_create(ArrayClass, value) : (VALUE)External::Unwrap(peer);
|
||||
}
|
|
@ -22,7 +22,10 @@ namespace {
|
|||
return Qnil;
|
||||
}
|
||||
}
|
||||
|
||||
VALUE _Value(VALUE self) {
|
||||
HandleScope scope;
|
||||
return (VALUE)V8_Ref_Get<External>(self)->Value();
|
||||
}
|
||||
void GCWeakReferenceCallback(Persistent<Value> object, void* parameter) {
|
||||
// printf("V8 GC!!!!\n");
|
||||
Local<External> external(External::Cast(*object));
|
||||
|
@ -37,7 +40,7 @@ void rr_init_v8_external() {
|
|||
rb_define_const(ExternalClass, "OBJECTS_REFERENCED_FROM_WITHIN_V8", references);
|
||||
rr_define_singleton_method(ExternalClass, "New", New, 1);
|
||||
rr_define_singleton_method(ExternalClass, "Unwrap", Unwrap, 1);
|
||||
// rr_define_method(ExternalClass, "Value", _Value, 0);
|
||||
rr_define_method(ExternalClass, "Value", _Value, 0);
|
||||
}
|
||||
|
||||
VALUE rr_reflect_v8_external(Handle<Value> external) {
|
||||
|
|
|
@ -78,7 +78,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"));
|
||||
Local<Value> peer = object->GetHiddenValue(String::NewSymbol("TheRubyRacer::RubyObject"));
|
||||
return peer.IsEmpty() ? rr_v8_ref_create(rr_cV8_C_Object, object) : (VALUE)External::Unwrap(peer);
|
||||
}
|
||||
|
||||
|
|
26
lib/v8/to.rb
26
lib/v8/to.rb
|
@ -5,9 +5,9 @@ module V8
|
|||
class << self
|
||||
def rb(value)
|
||||
case value
|
||||
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::Function then peer(value) {V8::Function}
|
||||
when V8::C::Array then peer(value) {V8::Array}
|
||||
when V8::C::Object then peer(value) {V8::Object}
|
||||
when V8::C::String then value.Utf8Value()
|
||||
when V8::C::Date then Time.at(value.NumberValue())
|
||||
else
|
||||
|
@ -44,17 +44,29 @@ module V8
|
|||
end
|
||||
when ::Time
|
||||
C::Date::New(value)
|
||||
when ::Class
|
||||
To.class_template(value).GetFunction().tap do |f|
|
||||
f.SetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyObject"), C::External::New(value))
|
||||
end
|
||||
when nil,Numeric,TrueClass,FalseClass, C::Value
|
||||
value
|
||||
else
|
||||
# obj = To.template.NewInstance()
|
||||
args = C::Array::New(1)
|
||||
args.Set(0, C::External::New(value))
|
||||
obj = To.class_template(value.class).GetFunction().NewInstance(args)
|
||||
return obj
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def peer(value)
|
||||
external = value.GetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyObject"))
|
||||
if external && !external.IsEmpty()
|
||||
external.Value()
|
||||
else
|
||||
yield.new(value)
|
||||
end
|
||||
end
|
||||
|
||||
def template
|
||||
@rubyobject ||= C::ObjectTemplate::New().tap do |t|
|
||||
t.SetNamedPropertyHandler(
|
||||
|
@ -78,7 +90,7 @@ module V8
|
|||
end
|
||||
else
|
||||
class_template = C::FunctionTemplate::New() do |arguments|
|
||||
if arguments.Length() > 0 && arguments[0].IsExternal()
|
||||
if arguments.Length() > 0 && arguments[0].kind_of?(C::External)
|
||||
wrapper = arguments[0]
|
||||
else
|
||||
rbargs = []
|
||||
|
@ -89,7 +101,7 @@ module V8
|
|||
wrapper = C::External::New(instance)
|
||||
end
|
||||
arguments.This().tap do |this|
|
||||
this.SetHiddenValue(C::String::New("TheRubyRacer::RubyObject"), wrapper)
|
||||
this.SetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyObject"), wrapper)
|
||||
end
|
||||
end
|
||||
class_template.InstanceTemplate().SetNamedPropertyHandler(
|
||||
|
|
Loading…
Add table
Reference in a new issue