Yu Kobayashi's HotRuby is a little different from other Ruby implementations, though. First off, it doesn't come with a Ruby parser - instead it executes opcodes of the Ruby 1.9 VM. Ruby 1.9, like Rubinius, compiles Ruby source code to opcodes which are then executed by it's opcode interpreter. These opcodes can also be stored on disk.
$n = $nativeIn this case,
$nativeis a global variable of type NativeEnvironment - as a matter of fact, it doesn't contain anything. In HotRuby, accessing members of objects of this type provides functionality. Eg.
@sprite = $n.Sprite.new
$n.Main.m_sprite = @sprite
@input = $n.Input.new @sprite
Another way to see HotRuby in action is the Do It Yourself page. This allows to type in Ruby code and run it with HotRuby. The way this works is that the Ruby source is sent to a server side service that compiles the source to opcodes which are then returned and executed by HotRuby in the browser.
One issue with HotRuby, at the moment, becomes obvious after trying to run a few bits of Ruby code, or simply by looking at the list of implemented classes/methods: the library support of HotRuby is minimal (actually, only a handful of methods of crucial classes are implemented). As a matter of fact, it's easy to see the implemented base classes, as their implementation can also be seen at the bottom of the HotRuby.js source file.
This is, however, a problem that might not be that hard to solve anymore - at least when it comes to functionality (performance is a different question). The Rubinius project is hard at work reimplementing a lot of basic Ruby classes in Ruby, even basic Ruby library functionality which is usually implemented in C (for CRuby), Java (JRuby) or C# (IronRuby). The idea to implement as much of a language in the language is often referred to as "Turtles All The Way Down" (after a popular blog post by Avi Bryant, although the expression is older).
Obviously, any parts interfacing with the outside (I/O, Operating System integration) would have to be ported and are specific to the underlying system. Also, unless the underlying runtime can optimize it, some base classes might need to be adapted to the underlying platform to allow for acceptable performance.
The Turtles All the Way Down approach has been used by many systems for a long time, one example being the Squeak Smalltalk, which is very portable. This was again demonstrated, when Dan Ingalls managed to get a Squeak image to run on the JVM (includes a link to a Java Web Start-able version).
Ruby libraries in pure Ruby also open the possibility of a standalone HotRuby in the future. One of the missing pieces is a full Ruby parser - but this is being created by Ryan Davis' in the form of 'ruby_parser', a project to write a Ruby parser in Ruby. Together with a Ruby based compiler that takes a Ruby AST (ruby_parser produces them in ParseTree notation) and returns Ruby 1.9 opcodes, HotRuby could then work stand alone and run Ruby sources directly. (Both parser and compiler would have to be pre-compiled into opcodes, which HotRuby would then load - as soon as this happens the first time, it would be self hosting).
Keith Adams Dec 06, 2013