More ECMA Awesomeness

Posted by – May 22, 2007

I’ve been adding more tender loving javascript features to RKelly (my Javascript to Ruby converter). You can now call to_ruby on an RKelly object and get back your Javascript as Ruby. For example:

puts RKelly.process(<<END
function c() {
  alert('asdfasdf');
}
var a = {};
foo['b'] = c;
END
).to_ruby

And that will out put the following ruby code:

def c
  alert("asdfasdf")
end
a = lambda do
  s = OpenStruct.new
  return s
end.call
class << foo
  def b
    alert("asdfasdf")
  end
end

Implicit object declaration is now supported too:

puts RKelly.process(<<END
var s = {
  x: function () { alert("blh"); },
  y: "foo"
};
END
).to_ruby

Which will output this:

s = lambda do
  s = OpenStruct.new
  class << s
    def x
      alert("blh")
    end
  end
  s["y"] = "foo"
  return s
end.call

One thing I’ve found while implementing RKelly is that javascript tends to pass around function pointers a lot. I will probably have to convert RKelly to declare lambdas for all functions instead of actual functions. The problem is that I’ll need to get it so the lambdas will be executed in the context of the object, but I’m sure that will be pretty easy.

1 Comment on More ECMA Awesomeness

Respond

  1. @Aaron:

    I salute the great work you’re doing with rkelly. I downloaded a copy and was looking at using it in a Rails application. However, I noticed that there was no gem on RubyForge, and there was no *.gemspec within /trunk/. Any chance you can add either of these two things in the near future?

    In addition, is there a way I can run it as is in my app? I tried moving it to /lib/ and then requiring it in environment.rb and that caused mongrel to choke:

    [davies] ruby script/server -u
    Exiting
    /usr/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/servers/mongrel.rb:16: warning: already initialized constant OPTIONS
    /usr/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/servers/mongrel.rb:19: undefined method options' for []:Array (NoMethodError)
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:32:in
    gemoriginalrequire’
    from /usr/local/lib/siteruby/1.8/rubygems/customrequire.rb:32:in require'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in
    require’
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/activesupport/dependencies.rb:342:in new_constants_in'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in
    require’
    from /usr/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/server.rb:39
    from /usr/local/lib/site
    ruby/1.8/rubygems/custom_require.rb:27:in gem_original_require'
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in
    require’
    from script/server:3
    [davies]

    If you have any ideas, please email me back.

Respond

Comments

Comments