Re: More about "extensible conversion framework"

18 views
Skip to first unread message

Charles Lowell

unread,
Jun 26, 2012, 10:11:15 AM6/26/12
to therub...@googlegroups.com


On Monday, June 25, 2012 11:24:12 AM UTC-5, koops wrote:
In the 0.11 changelog, there's a line about a new value conversion framework. Is this demonstrated or documented anywhere? I'm wondering if the "unboxing" as used in https://github.com/sstephenson/execjs/blob/master/lib/execjs/ruby_racer_runtime.rb may now be redundant.

I have not written any documentation for it yet, but the source can be found here. https://github.com/cowboyd/therubyracer/blob/master/lib/v8/conversion.rb

I wouldn't say that it's redundant in that the standard v8 conversions are not sufficient for what they want which is apparently to nil out functions, but it could be written more elegantly now. i.e.

conversion = @v8_context.conversion
def conversion.to_ruby(v8_object)
  v8_object.is_a?(V8::C::Function) ? nil : super
end

or in a perhaps more module manner:

module NoFunctions
  def to_ruby(v8_object)
    v8_object.is_a?(V8::C::Function) ? nil : super
  end
end

module ForceUtf8
  def to_ruby(v8_object)
    super.tap do |object|
      object.force_endoding('UTF-8') if object.respond_to?(:force_encoding)
    end
  end
end

@v8_context.conversion.extend NoFunctions
@v8_context.conversion.extend ForceUtf8

As far as forcing the string encoding, this is just for show since as of 0.11, therubyracer will always properly encode utf8 strings when fetched from the underlying C++ object: https://github.com/cowboyd/therubyracer/blob/master/ext/v8/string.cc#L22-29


--koops
Reply all
Reply to author
Forward
0 new messages