Running Java in JavaScript


Welcome Waxy.org and Slashdot readers. I blog about JavaScript, like it’s my job, feel free to subscribe for a ton more posts like this.

Related Posts:

When I was in Tokyo this past fall speaking at the Shibuya.JS user group I had the opportunity to see a number of interesting JavaScript projects that have yet to make it outside of Japan.

One project, in particular, really caught my eye. It’s called Orto [PDF, Japanese] and is an implementation of the Java Virtual Machine (JVM) in JavaScript. This means that you can take an existing Java application, compile it to bytecode, run it through Orto (which produces the JavaScript, and embed it in a web page. While it doesn’t provide the full capabilities of most Java code it does provide enough to make for some interesting demos.

The one demo that was presented was that of a real-time, interactive, Tetris game:

If you take a peak at the generated source code you can see some interesting tidbits (obviously all of the code is meant to be ‘machine readable’ – being just converted bytecode):

  1. "java/lang/Thread 1316742099":function(){var orto333=orto245[0];
  2. var orto336=orto350(orto333);
  3. if(orto336.orto340!=orto310){orto223("java/lang/IllegalThreadStateException",null);
  4. return ;
  5. }
  1. case 117:orto246[orto247-2]={high:(~orto246[orto247-2].high)
  2.   &0xffffffff,low:(~orto246[orto247-2].low+1)&0xffffffff};
  3. if(orto246[orto247-2].low==0){orto246[orto247-2].high++;
  4. orto246[orto247-2].high&=0xffffffff;
  5. orto246[orto247-2].low=0;
  6. }break;
  1. case "CHECKBOX":orto171=orto188["orto/ui/CheckBox"];
  2. break;
  3. case "IMAGE":orto171=orto188["orto/ui/ImageButton"];
  4. break;
  5. case "RADIO":orto171=orto188["orto/ui/RadioButton"];
  6. break;

Unfortunately, the information on the subject is a little scarce (considering that both orto.accelart.jp and orto.jp seem to be missing any relevant information). There are a couple of things that are apparent, however:

  • The result is able to handle threaded application code (translating the threads into a series of yields with setTimeout (mentioned in the presentation and demonstrated in the Tetris example).
  • The application can use regular Java conventions for designing and constructing the UI (as shown here, as well). User Interface components are translated to, similar, HTML ones. It’s not apparent to what extent functionality is implemented, but it is to a certain degree.
  • Keyboard interactions are able to be handled and translated to normal Java callbacks.

I was especially pleased with the resulting performance of the demo application. I wasn’t expecting a game (translated multiple layers deep) to still be as playable as it is. Obviously the culmination of JavaScript optimization and increases in browser performance have served these types of endeavors well.

Posted: April 28th, 2008


If you particularly enjoy my work, I appreciate donations given with Gittip.

25 Comments (Show Comments)



Comments are closed.
Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.


Secrets of the JavaScript Ninja

Secrets of the JS Ninja

Secret techniques of top JavaScript programmers. Published by Manning.

Ukiyo-e Database and Search

Ukiyo-e.org

Japanese woodblock print database and search engine.


John Resig Twitter Updates

@jeresig

Infrequent, short, updates and links.


via Ad Packs