Links
Tags
apache
armenia
books
bsd
c
c++
chips
cinema
concurrency
cooking
database
dragonfly
erlang
filesystem
freebsd
fun
hardware
java
javascript
json
languages
linux
lyric
mac_osx
mail
math
misc
music
personal
poems
presentation
programming
python
references
ruby
rubyjs
scm
software
spiking_neural_net
study
sysadm
sysarch
technology
testing
travel
virtualization
web
wee
windows
O’Browser is an Objective Caml bytecode interpreter written in Javascript. It is capable of running a stripped down version of OCaml’s standard library. Some example applications can be seen here.
The implementation of the runtime counts around 6000 lines of Javascript code, plus another 7000 lines of OCaml code for the standard library. That’s quite a lot when compared against the approx. 4000 lines of Ruby code, including inline Javascript code of the Ruby core classes, of RubyJS. But it’s definitively a great way to run little OCaml applications within the browser, for those that use OCaml. Unfortunately I never made it to write any larger application in OCaml, I only ever used SML (MLTon) in a more extensive way.
Before I started to work on RubyJS (that’s now around 2 years ago), I also thought about compiling OCaml down to Javascript. My idea was to emit Javascript code instead of running OCaml bytecode within Javascript, and I think together with OCaml’s object model, this approach could be a viable alternative to Google’s Java to Javascript compiler when it comes to performance and static typing. RubyJS can’t simply give you all those static type guarantees and due to it’s dynamic typing system of Ruby where everything is a method call, it can’t be all that fast. But then, we all use Ruby or a similar dynamic language and it is just fast enough, so why stick with Java?
Christopher Nelson talked about Ruby in the Browser at this years’ RubyConf 2008. He mentioned RubyJS, my Ruby to Javascript compiler, as well as a new library called Red, which also translates Ruby to Javascript, but in a slightly different way.
There are lots of interesting articles about RubyJS and rubyjs_on_rails on his blog.
I am pleased to announce the first public release of RubyJS, a Ruby to Javascript compiler. It is now available as a Rubygem, so that you can install it with a single command:
gem install rubyjs
To see what it compiles, and whether the generated Javascript code runs in your browser, take a look at it’s test suite. Watch out for the color red! If everything is “green”, it runs fine within your browser.
Right now there are only two examples, the obvious Hello World example (Ruby code here) and another example (Ruby code here) that demonstrates some preliminary support for Ruby Web Toolkit (an unfinished port of Google Web Toolkit), which btw will hopefully be completed by a this years Google Summer of Code project.
In this morning I made a benchmark that compares an example written in Javascript using Prototype against the equivalent version in RubyJS (which uses plain Ruby code).
The RubyJS version:
sum = 0
500_000.times {|i| sum += i}
The Javascript/Prototype version:
var sum = 0;
(500000).times(function(i) {sum += i;});
Not only is the Ruby(JS) version more readable (IMHO), but it is also faster.
This morning I came across HotRuby, a virtual machine for YARV (Ruby 1.9) bytecode for Javascript and Flash. When I started implementing RubyJS I also considered this approach (also using Ocaml bytecode :), but then abandoned it due to performance issues.
I quickly implemented the benchmark they give using RubyJS. It takes 11.57 seconds in HotRuby but only 4 seconds in RubyJS. The same benchmark executed in Ruby 1.8 takes around 24 seconds!!! But only 0.12 seconds if you replace the “+=” by ”«”, so I consider this a very bad benchmark because it only benchmarks the performance of String allocation in the underlying VM/interpreter and not that of RubyJS/HotRuby. A benchmark with a lot of method calls, as is usual in a regular Ruby application, would be a much better benchmark. I think this would show even better results for RubyJS.
I finally made it to put the slides of my RubyConf 07 talk RubyJS - Efficient Ruby to Javascript Compilation online.
I am looking for someone to sponsor me, so that I can continue work on RubyJS, my Ruby to Javascript compiler and finish the Google Web Toolkit port, called RWT. Interested? Then read my proposal and contact me.
During the last two days I made huge progress on completing RubyJS. The last 30 commits added tons of test-cases and features:
- Implement Exceptions and begin, rescue, ensure statements
- Implement case statement and the “===” method for serveral classes.
- Complete implementation of iterators and break, next
- Implement String interpolation
- Produce HTML containing the Javascript code of all test-cases suitable for running easily inside a browser. Test cases succeed in IE6, Firefox, Opera and Konqueror.
- Implement Range class
- Optimize conditionals and unless
- Add useful methods like “gsub” to class String and fix “inspect”.
- Implement send, kind_of?, instance_of? and respond_to? methods
- Implement zsuper (super without arguments)
The only thing that is now missing is a more complete core library and method_missing. The latter is a huge benefit against plain old Javascript.
I really dislike Javascript. That’s why I wrote RubyJS some time ago. With RubyJS you can basically translate your Ruby scripts to Javascript. Of course there are some minor problems due to their incompatible object models etc. For example Ruby has integers and floats, while Javascript only has numbers. Then Ruby has mutable strings while Javascript has not. Of course all this can be simulated by RubyJS easily but of course this comes not for free. For immutable strings there are two choices, either restrict RubyJS to use immutable operations on strings or box every Javascript string into a RubyString object. On the other hand, Ruby’s Arrays perfectly fit to Javascript arrays. Hashes do not!
Today I took a look at other languages. For example, would it be possible to convert Ocaml to Javascript? Haskell? For Haskell there even exists an approach using YHC (York Haskell Compiler). But, while Haskell is a very nice language, it lacks some OO support, that can be found in Ocaml. How about Erlang? It should be possible using the BEAM bytecode and translating it to Javascript. There is only one problem. Lists. There are not native in Javascript, but are used heavily in Erlang. After some time, I took a deeper look at Ocaml. It shouldn’t be that hard to translate Ocaml’s bytecode or it’s lambda-terms (-dlambda option) to Javascript. The advantage would be that you have a strongly typed langauge. No more ugly Javascript errors. But then, you don’t have open classes (or open modules). That’s a pain if you are coming from Ruby (or Javascript).
Read this. Having discovered it today, I have to say, I really like it :)