lib/v8/to.rb in therubyracer-0.7.1 vs lib/v8/to.rb in therubyracer-0.7.2.pre

- old
+ new

@@ -1,34 +1,36 @@ +require 'weakref' module V8 module To class << self - def ruby(value) + 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()) + when V8::C::Value then nil if value.IsEmpty() else value end end - alias_method :rb, :ruby - def v8(value) case value when V8::Object value.instance_eval {@native} - when String, Symbol + when String C::String::New(value.to_s) + when Symbol + C::String::NewSymbol(value.to_s) when Proc,Method template = C::FunctionTemplate::New() do |arguments| rbargs = [] for i in 0..arguments.Length() - 1 - rbargs << To.ruby(arguments[i]) + rbargs << To.rb(arguments[i]) end V8::Function.rubycall(value, *rbargs) end return template.GetFunction() when ::Array @@ -43,35 +45,29 @@ o.Set(To.v8(key), To.v8(value)) end end when ::Time C::Date::New(value) + when ::Class + Constructors[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 - rubyobject = C::ObjectTemplate::New() - rubyobject.SetNamedPropertyHandler( - NamedPropertyGetter, - NamedPropertySetter, - nil, - nil, - NamedPropertyEnumerator - ) - obj = nil - unless C::Context::InContext() - cxt = C::Context::New() - cxt.Enter() - begin - obj = rubyobject.NewInstance() - obj.SetHiddenValue(C::String::New("TheRubyRacer::RubyObject"), C::External::Wrap(value)) - ensure - cxt.Exit() - end - else - obj = rubyobject.NewInstance() - obj.SetHiddenValue(C::String::New("TheRubyRacer::RubyObject"), C::External::Wrap(value)) - end + args = C::Array::New(1) + args.Set(0, C::External::New(value)) + obj = Access[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 camel_case(str) str.to_s.gsub(/_(\w)/) {$1.upcase} \ No newline at end of file