Live data from Hacker News

Opal - Ruby to Javascript compiler

opalrb.org

1–10 of 54 posts

Re: Opal - Ruby to Javascript compiler

#2
While very impressive it makes me nervous. The JS it outputs just in the basic tutorial looks extremely complex, I'd hate to debug that code! Compare with CoffeeScript, where the output is quite easy to follow, and therefore debug.

Re: Opal - Ruby to Javascript compiler

#3
Yup, that's some good brainfuck:

      (function() {
        var __opal = Opal, self = __opal.top, __scope = __opal, nil = __opal.nil, __breaker = __opal.breaker, __slice = __opal.slice, __klass = __opal.klass;
        var adam = nil, __a, __b;
        (__b = [1, 2, 3, 4], __b.$each._p = (__a = function(a) {

    
          if (a == null) a = nil;

          return this.$puts(a)
        }, __a._s = self, __a), __b.$each());
        (function(__base, __super){
          // line 5, (file), class Foo
          function Foo() {};
          Foo = __klass(__base, __super, "Foo", Foo);

          var Foo_prototype = Foo.prototype, __scope = Foo._scope;

          return Foo_prototype.$name = function() {
      
            if (this.name == null) this.name = nil;

            return this.name
          }, 
          Foo_prototype['$name='] = function(val) {
      
            return this.name = val
          }, nil
        })(self, null);
        adam = __scope.Foo.$new();
        adam['$name=']("Adam Beynon");
        return self.$puts(adam.$name());
      })();

Re: Opal - Ruby to Javascript compiler

#6

Yup, that's some good brainfuck: (function() { var __opal = Opal, self = __opal.top, __scope = __opal, nil = __opal.nil, __breaker = __opal.breaker, __slice = __opal.slice, __klass = __opal.klass; var adam = nil, __a, __b; (__b = [1, 2, 3, 4], __b.$each._p = (__a = function(a) { if (a == null) a = nil; return this.$puts(a) }, __a._s = self, __a), __b.$each()); (function(__base, __super){ // line 5, (file), class Foo…

If you look at this rationally, it's actually pretty sane. It's just very simple JavaScript OOP inline with translated code. Even more beautiful is the output of modules:

     module Run
       def run
         puts "I am running!"
       end
     end

     module Jump
       def jump
         puts "I am jumping!"
       end
     end

     class Foo
       include Run
       include Jump
     end

     mario = Foo.new
     mario.run
     mario.jump

translates to:

     (function() {
       var __opal = Opal, self = __opal.top, __scope = __opal, nil = __opal.nil, __breaker = __opal.breaker, __slice = __opal.slice, __module = __opal.module, __klass = __opal.klass;
  
     	// Define Mario
     	var mario = nil;
  
     	// Javascript "Run" class
     	(function(__base){
         // line 2, (file), module Run
         function Run() {};
         Run = __module(__base, "Run", Run);
         var Run_prototype = Run.prototype, __scope = Run._scope;

         Run_prototype.$run = function() {
      
           return this.$puts("I am running!");
         }
             ;Run._donate(["$run"]);
       })(self);

     	// Javascript "Jump" class
       (function(__base){
         // line 8, (file), module Jump
         function Jump() {};
         Jump = __module(__base, "Jump", Jump);
         var Jump_prototype = Jump.prototype, __scope = Jump._scope;

         Jump_prototype.$jump = function() {
      
           return this.$puts("I am jumping!");
         }
             ;Jump._donate(["$jump"]);
       })(self);

     	// JavaScript "Foo" class
       (function(__base, __super){
         // line 14, (file), class Foo
         function Foo() {};
         Foo = __klass(__base, __super, "Foo", Foo);

         var Foo_prototype = Foo.prototype, __scope = Foo._scope;

     	 // $include Run / Jump
         Foo.$include(__scope.Run);
         return Foo.$include(__scope.Jump);
       })(self, null);

     	// The ruby executing code translation
       mario = __scope.Foo.$new();
       mario.$run();
       return mario.$jump();
     })();

Re: Opal - Ruby to Javascript compiler

#7
Cool idea. Not the first attempt though:

http://hotruby.yukoba.jp/

https://github.com/whitequark/coldruby

http://rb2js.rubyforge.org/

http://www.ntecs.de/blog/articles/2007/01/08/rubyjs-javascri...

https://github.com/jessesielaff/red

http://www.playmycode.com/docs/quby

https://github.com/mattknox/8ball

Unfortunately, none of them seem to be to handle the mor dynamic features of ruby, like method_missing, which diminishes the possibility of reusing existing ruby code in the browser.

Re: Opal - Ruby to Javascript compiler

#8
post #4

What the use case for something like this? Am I missing something?

Porting existing code without having to re-implement. Keeping a single reference implementation of a particular method.

on related note, when and if node.js takes off, does it make sense to have a php-to-javascript compiler so people don't need to redo a lot of stuff?

Re: Opal - Ruby to Javascript compiler

#10
I really like this idea of compiling code to Javascript. Brenden Eich makes some good points for it in his series of slides (starting here: http://brendaneich.github.com/Strange-Loop-2012/#/21).

I'm working on a little programming language myself that compiles to Javascript. In most languages that do this, I wish there was better information on the mapping between the domain language and its Javascript codomain. I think this would be a good open source project idea: a tool to assist in the compilation of various languages to Javascript. Something to the effect of how source lines in C can be printed above their generated assembly. Anyone want to give it a shot?

Post reply on HN