Tag: rkelly

Clean it up! RKelly::Nodes::Node#to_ecma

Posted by – January 23, 2008

Last night at Nerd Club, I decided to add a “to_ecma” method to RKelly. This means you can turn your ECMA AST back in to ECMAScript, with the added bonus of properly indenting your code. For example:

require 'rkelly'

parser = RKelly::Parser.new
puts parser.parse(DATA.read).to_ecma

__END__
function yo(a,b,a) { this.be.some(); if(nasty) { code();} }

will output this:

function yo(a, b, a){
  this.be.some();
  if(nasty) {
    code();
  }
}

Now you should be able to modify your AST and generate javascript. Next I want to add finders to easily find nodes in the tree.

パンツないな興奮 – Javascript stuff

Posted by – January 19, 2008

While writing RKelly (a pure ruby javascript interpreter), I’ve run across weird cases in the ECMA spec that I didn’t know about before. For example, when coercing an object into a number, first the “valueOf” property is checked. That seemed to make sense to me. But then the spec says that if there is no “valueOf” property, to check the “toString” property and try to coerce that in to a number.

So, this bit of code:

var x = new Object;
x.valueOf = function() { return 11; };
x++;

is equivalent to this bit of code:

var x = new Object;
x.stringValue = function() { return "11"; }
x++;

That is some underpants-free excitement.

Happy New Year! (RKelly Progress report)

Posted by – January 3, 2008

I’ve just started getting the runtime working with RKelly. Its working well enough at this point that I was able to execute my earlier Fibonacci example. I’ve added a method to the runtime that allows you to define ruby functions that may be called from inside javascript. For example, the alert function in the following example is defined in ruby and delegates to puts.

runtime = RKelly::Runtime.new

runtime.define_function(:alert) do |*args|
  puts(*args)
end

runtime.execute(<<END
function f(n) {
  var s = 0;
  if(n == 0) return(s);
  if(n == 1) {
    s += 1;
    return(s);
  } else {
    return(f(n - 1) + f(n - 2));
  }
}
alert(f(20));
END
)

Here is the execution time with ruby 1.8.6 on my machine:

[aaron@mac-mini rkelly]$ time ~/.multiruby/install/1.8.6-p111/bin/ruby -I lib test.rb
6765

real 0m54.332s
user 0m53.913s
sys 0m0.336s
[aaron@mac-mini rkelly]$
[/sourcecode]

Same code, same machine, but with ruby 1.9.0:


[aaron@mac-mini rkelly]$ time ~/.multiruby/install/1.9.0-0/bin/ruby -I lib test.rb
6765

real 0m20.863s
user 0m20.678s
sys 0m0.142s
[aaron@mac-mini rkelly]$
[/sourcecode]

I need to get loops working next!