More progress on rb2js
千里之行,始於足下 ―― 道德經
After some productive coding this morning, I’ve now got a basic Ruby object system working in JavaScript. I’ve had a lot of help from the Ruby Hacking Guide (written in Japanese and partially translated into English) regarding how objects are implemented inside Ruby.
I’ve also implemented some basic String and Kernel methods so that the following JavaScript code actually works:
var s = RbString.box('Hello'); rbSendMessage(s, '<<', [' World']); rbSendMessage(RbRootObject.getConstant('Kernel'), 'puts', [s]);
Ugly, isn’t it? Just remember that the goal is not to produce idiomatic JavaScript code, but rather something that works like Ruby and that can be targeted by the rb2js compiler. It’s analogous to this in Ruby:
s = 'Hello' s << ' World' Kernel.puts(s)
Although it looks simple, it’s actually a really big step towards emulating Ruby’s behaviour in JavaScript. We can have mutable strings, for example, even though the underlying platform (JS) doesn’t support them natively.
I haven’t implemented metaclasses yet, so instantiation isn’t yet possible except in the special case of boxed strings. Still, getting that ‘Hello World’ printed makes me feel that I’ve got over the first hill on the journey.