2009-12-18 02:48:06 -05:00
|
|
|
|
|
|
|
module V8
|
|
|
|
module To
|
|
|
|
class << self
|
|
|
|
def ruby(value)
|
|
|
|
case value
|
2010-05-22 21:02:10 -04:00
|
|
|
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
|
2010-05-17 11:30:31 -04:00
|
|
|
when V8::C::Array then V8::Array.new(value)
|
2010-04-21 19:09:13 -04:00
|
|
|
when V8::C::Object then V8::Object.new(value)
|
2010-05-17 07:39:29 -04:00
|
|
|
when V8::C::String then value.Utf8Value()
|
2009-12-18 02:48:06 -05:00
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
2010-05-22 02:23:22 -04:00
|
|
|
|
|
|
|
alias_method :rb, :ruby
|
|
|
|
|
2009-12-18 02:48:06 -05:00
|
|
|
def v8(value)
|
|
|
|
case value
|
2010-05-22 02:23:22 -04:00
|
|
|
when V8::Object
|
|
|
|
value.instance_eval {@native}
|
|
|
|
when String, Symbol
|
|
|
|
C::String::New(value.to_s)
|
2010-05-19 08:55:11 -04:00
|
|
|
when Proc,Method
|
2010-05-22 02:23:22 -04:00
|
|
|
template = C::FunctionTemplate::New() do |arguments|
|
2010-05-19 08:55:11 -04:00
|
|
|
rbargs = []
|
|
|
|
for i in 0..arguments.Length() - 1
|
|
|
|
rbargs << To.ruby(arguments[i])
|
|
|
|
end
|
|
|
|
To.v8(value.call(*rbargs))
|
2010-05-22 02:23:22 -04:00
|
|
|
end
|
|
|
|
return template.GetFunction()
|
2010-05-22 22:31:36 -04:00
|
|
|
when ::Array
|
|
|
|
C::Array::New(value.length).tap do |a|
|
|
|
|
value.each_with_index do |item, i|
|
|
|
|
a.Set(i, To.v8(item))
|
|
|
|
end
|
|
|
|
end
|
2010-05-22 02:23:22 -04:00
|
|
|
when nil,Numeric
|
2009-12-18 02:48:06 -05:00
|
|
|
value
|
2010-05-22 02:23:22 -04:00
|
|
|
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
|
|
|
|
return obj
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-17 18:12:23 -05:00
|
|
|
def camel_case(str)
|
2010-01-10 04:37:51 -05:00
|
|
|
str.to_s.gsub(/_(\w)/) {$1.upcase}
|
2010-01-17 17:25:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def perl_case(str)
|
|
|
|
str.gsub(/([A-Z])([a-z])/) {"_#{$1.downcase}#{$2}"}.downcase
|
|
|
|
end
|
2009-12-18 02:48:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|