Ruby and Java Running in JavaScript 220
John Resig is reporting on his blog that a recent trip to Tokyo opened up some very interesting JavaScript projects to him that haven't met with widespread popularity outside of Japan yet. "One project, in particular, really caught my eye. It's called Orto 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." In a separate post he also detailed how the HotRuby project is allowing a Ruby VM to run in a browser using JavaScript or even indirectly using ActionScript in Flash.
Awesome! (Score:4, Funny)
Re: (Score:3, Interesting)
Cynicism was my initial response as well, but reading TFA shows a pretty cool demo [accelart.jp]. The fact that they are able to convert Java's user input events, GUI, and multithreading to Javascript is pretty cool. Probably has no practical use, but still cool.
If nothing else it means that the next time (in about 3 minutes if today is a normal day) somebody gets Java and Javascript confused, I can say they really ARE "basical
Re:Awesome! (Score:5, Funny)
Fantastic. Somebody's found a way to make the morons of the world slightly more correct without them even knowing it.
Re:Awesome! (Score:4, Informative)
Java is an object-oriented programming language originally released by Sun Microsystems in 1995. JavaScript is a functional scripting language originally derived from Ecmascript. Java requires a Java Virtual Machine to be installed on your computer or some other device like a mobile phone. Javascript runs right in your web browser. Javascript was originally named LiveScript, but through a marketing deal between Netscape and Sun, became named JavaScript, even though the two languages are unrelated, thus dooming those in the know to have to constantly correct people who refer to JavaScript as Java, assuming that "Java" is just like a nickname or something. However, they are both based on syntax that looks a good deal like C.
Re: (Score:2)
$ echo "JavaScript" > JavaScript
$ diff Java JavaScript
1c1
< Java
---
> JavaScript
HTH
Re: (Score:2)
In actuality, I can think of several applet-type projects that suffer significant problems in Java, which I'd like to try to "cross-compile" to Javascript... One that we've been trying to get to work for a long time works just fine in every browser we have tested it on. Unless, of course, you try to run it from a page loaded by HTTPS! If it is, the success of it running depends entirely upon the browser used, AND whether or not it was previously run from the same page, loaded via a non-TLS connection.
Attem
Re: (Score:2)
I can say they really ARE "basically the same thing" now!
That's like saying Python and C are basically the same thing, since the CPython interpreter is written in C. Writing an interpreter for language A in language B doesn't mean languages A and B are basically the same thing.
Plus this isn't even the Java programming language we're talking about, it's Java bytecode, which could have been produced from something other than the Java programming language (like Groovy or Scala).
Re: (Score:2)
Finally, a way to combine the feature-rich capabilities of Javascript with the speed of Java!
You joke, but I'm sure it's the other way around. The myth that Java is slow persists to this day, though it hasn't been true for many years.
The ugly thing about this hack is going to be the slowness of javascript execution, especially when doing this bizzare translation. I'm all for replacing javascript with a less sucky language. But this doesn't seem like the solution.
Re: (Score:2)
It's the two primary runtime environments' gross annoying differences, and weaknesses with little syntaxy things like variable declaration and adding strings - or did you mean integers? and how parseInt('08') blows up etc... things like THAT make it a pain.
Re: (Score:2)
Doing things the slow way (Score:3, Insightful)
Re: (Score:3, Interesting)
I'm with you, I read the summary and almost choked on my coffee.
This is like writing a Cray emulator for your TI 99/4a -- I don't know what it buys you.
I'm sure some clever person will put out a demo showing something completely amazing, and I'd
Re: (Score:2)
Hey, I used to have a TI 99/4A. Great little machine, back then!
Re: (Score:2)
I'm not disparaging the TI 99/4a, I have very fond memories of that machine.
:-P
... making a scale model of a dump truck from a Yugo. Sure, it kinda mostly looks like a dump truck. But, it can't do any of the things you'd actually want a dump truck for. :-P
But, let's face it, that's just as odd a scenario as running Java on a javascript interpreter.
It's like
Cheers
Re: (Score:2)
Bah, give me enough monkeys and enough time, I'll give you all of the analogies you could ever want.
I've made the computer analogy, the car analogy, and now I've alluded to monkeys
Cheers
Re:Doing things the slow way (Score:5, Informative)
The article suggests that the speed was not bad. (The sample Tetris [accelart.jp] clone loaded very quickly for me.) And the article's commenters note that this runs on an iPhone. In other words, Orto could be a route to port Java apps to be iPhone aps.
Re:Doing things the slow way (Score:5, Funny)
iPhone -> Orto -> Javascript -> Java -> C64 Emulator -> VIC-20 Emulator -> Zork I
Exciting New ways to be eaten by a grue!
Re: (Score:3, Informative)
Re:Doing things the slow way (Score:4, Insightful)
Tetris performed better on my Gameboy (an 8-bit, 4.2MHz x80 CPU with 8KB RAM) than this clone does on my 32-bit 1.4GHz Athlon. And it had sound. Tetris shouldn't load "quickly" it should load instantly.
This is a very clever hack, and I admire the work -- but it is in no way practical for anything. I find the idea of using it for "real work" apalling -- and I code in Java by choice!
Re: (Score:2)
I think your complaint here is with Java, not Javascript or Orto. I guarantee it would take longer for this Tetris to load up were it directly in Java if you didn't already have the Java runtime loaded up in memory -- and maybe even if you did.
So, I'd grade Orto as "damn impressive".
Re: (Score:2)
Even after it loaded, it was sluggish with poor response to the controls. In native Java there'd be a load time, but if done well it would be quite snappy after that.
Don't get me wrong, I'm quite impressed. But I'm impressed in the "damn that's neat" sense, not in the sense that I'd ever contemplate using it for anything.
Re: (Score:3, Interesting)
Why wouldn't I code in Java? For most things I code, I see about a 10-20% performance penalty vs C, plus a modest load time (that I don't care about usually). I also see a noticeably reduced development time; for me that's a trade worth making under most circumstances. Now, I'm not coding user interfaces, and I am paying attention to performance of my Java code (unlike many Java developers). I'm also emphatically not claiming it's a good environment for everything (but I'd say the same about any languag
Re: (Score:3, Insightful)
Re: (Score:2)
what i can't stand is java's complete lack of accuracy in basic math such as
int upper = (int) (value * 100.0);
double newVal = ((double)upper) / 100.0;
value can start as an int/double/float your choice.
in the end newValue does not alwasy equal value.. even though it should.. i understand floating point errors but i first saw this cause a problem with value being a double 8.12
oddly it isn't consitant from one
Re: (Score:2)
Floating point numbers are expected to behave like that. It's an inherent limitation of the base conversion combined with finite precision and any exact equality test. 8.12 gets rounded off to a number slightly less than 8.12 when stored as a float; when multiplied and cast it gets rounded down to 811.
#include <stdio.h>
int main(int argc, char* argv) {
double val;
int upper;
double newval;
val = 8.12;
upper = (int)(val * 100.0);
newval = ((double)upper)/(100.0);
printf("val: %e\nupper: %d\nnewval: %e\ndiff: %e\n", val, upper, newval, (val - newval));
return 0;
}
evand@Desktop:~/test$ gcc --version
./a.out
gcc (GCC) 4.2.3 (Debian 4.2.3-3)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
evand@Desktop:~/test$ gcc test.c
evand@Desktop:~/test$
val: 8.120000e+00
upper: 811
newval: 8.110000e+00
diff: 1.000000e-02
Java and C are both behaving exactly how they're supposed to. First rule of working with floating point numbers: never do an exact equality test on the results of calculations. That includes testing t
Re: (Score:2)
Replying twice, bad form I know, but I forgot to mention:
If you really care about floating point precision, it's trivially easy to get in java: use the strictfp keyword on the relevant class or method, and java will generate code that *exactly* implements IEEE 754 arithmetic on all hardware. I don't know off hand how to do that in other languages; I imagine some make it easy and some make it hard to impossible. I can't see it being much easier than specifying a single keyword with the appropriate scope,
Re:Doing things the slow way (Score:5, Informative)
What language can you stand, then?
Re: (Score:2)
Tetris is not apt to use a great deal of java's vast libraries, so if you're smart about loading just the parts you need, when you need them, why shouldn't Java on Javascript Tetris be fairly close to plain old Javascript Tetris, with a modest constant time hit. What I'd like to see
Re: (Score:2)
I think the article was likely talking about web-based iPhone games, which presently is the only Apple-sanctioned way of playing a game on your iPhone. Unfortunately, the web-based games are pretty limited compared to what can be played on, say, a Jailbroke
Re: (Score:2)
Okay, but that's a pretty trivial example indeed -- Tetris was developed in the mid-'80s on a Soviet PDP-11 clone. Tetris has been implemented as simple state machines that run on wristwatches and keychains.
While novel, I don't think JVM-implemented-in-Javascript is going to be a practical path for anything very complex. This isn't going to enable me to run Eclipse as a web app.
Re: (Score:2)
Probably, it will be slow.
But, in general, just because something is interpreted doesn't mean it has to be slow. There are various ways to implement an interpreter, and interpreters can have either very fast start-up time or very fast execu
Re: (Score:2)
However, except in rare and unusual cases (e.g., using an interpreter that does realtime execution analysis and modifies code on the fly), interpreted code will always be at least a little bit slower than compiled code.
Re: (Score:2)
Re: (Score:2)
Is anyone here clear on the actual amount of processing power necessary to run a game of Tetris? Hint: It's not a lot. Especially if you're not using a fancy blitter with alpha-transparencies and pipelined transforms.
But... (Score:5, Interesting)
Does it run Linux? ;)
In all seriousness, though... I'm struggling to see how this is truly beneficial. Aren't most pages already hopelessly clogged with mounds of JavaScript? Is it that difficult to expect a user to have a Java interpreter already installed when they visit the page such that having your Java "emulated" in JavsScript is the better solution?
Just seems like a solution needing a problem to me.
Re: (Score:2, Insightful)
If you can run it under JavaScript, you can run it on an iPhone.
I believe that you're missing the point (Score:2)
Re: (Score:2)
Re: (Score:2, Insightful)
Re: (Score:3, Informative)
Ruby and Java, and also Python (Score:4, Informative)
Google Web Toolkit (Score:5, Interesting)
Re: (Score:2)
I don't think you can get stand-alone applications with GWT, and even applications that just run in the browser and are loaded from a file appear unlikely, they'll always need a browser with javascript support and a webserver that can execute Java.
Actually no, you can build an application in Java with no client/server communications and have GWT generate the client javascript code and run it stand-alone or serve it as a standard web page. It really does generate (compile) a javascript version of the java code for the client.
Strange (Score:5, Interesting)
Why does everyone hate Javascript so? If you're going of cut-n-paste examples from the web yes it looks like an ugly language. Check out how the OO stuff works, or some JQuery code, and you'll be pleasantly surprised.
Re:Strange (Score:4, Informative)
Re: (Score:2)
Things have matured a lot lately, and you can almost get by without writing any browser specific hacks.
Umm kinda. Tons of browser specific Javascript (and CSS!!!) hacks are still needed to write an app that plays nice across all platforms. The only difference is that developers of libraries such as JQuery, Prototype, and ExtJS have done an excellent job of encapsulating all the hacks into a library with a consistent API for you to use. They give you normalized event handling, keycode input, DOM element lookup etc. However if you tried to fly solo and develop without one of these libraries, Javascript would
Re: (Score:2)
This doesn't make me hate JavaScript though... it just makes me hate browsers that don't work. If I tell you to update the contents of an object you should update it without me having to move the focus to that o
Re: (Score:2, Insightful)
Javascript is inherently multithreaded and embedded in a highly asynchronous environment, het has no threading API whatsoever. To me, this is mind-boggling. Every AJAX project I've been involved in has had to build in ugly workarounds or just drop features to prevent race conditions.
Re: (Score:2)
Re: (Score:2)
Pointers to async JS would be nice, since my RSS reader pegs my CPU for minutes at a time while fetching updates to my 100+ feeds. When this happens every 20min you can see where this might become frustrating.
Re: (Score:2)
Not really, the netscape.javascript.* package for interacting with Javascript and the DOM has been a standard part of Java (even the notoriously incompatible MS JVM) since the early days. Perhaps it isn't as well known as it could be due to being outside the java.* namespace.
The Octoparrot of the programming world (Score:5, Funny)
I just need to get this out of my system (Score:5, Funny)
Re: (Score:3, Funny)
Re: (Score:2, Informative)
Re: (Score:2, Informative)
A wordplay (Score:2)
Re: (Score:2)
No Perl? (Score:4, Informative)
Re: (Score:2)
Re: (Score:2)
PerlScript (Score:2)
Neat, in a way. I recall playing a very VERY small bit with this ~5ish years ago, I believe. Not sure how useful it is, but it still warms my perl-loving heart.
Re: (Score:2)
No, wait. I think it was get_php_script('today'). Or was it get_today('script', _PHP_)...?
crazy japanese (Score:3, Funny)
that's pretty cool but man, talk about a daunting tedious task. I'd rather bail 600 acres with a weed wacker and twisty-ties.
Language stacks galore! (Score:5, Funny)
Now all we need to get is a C compiler to output python code. If someone can then write an x86 VM in python we could then run Linux in Firefox!
Re: (Score:3, Funny)
Re: (Score:3, Funny)
Re: (Score:3, Interesting)
Re: (Score:2)
Anybody seriously wanting to run Python in web browsers should look to IronMonkey [mozilla.org].
Haskell, too (Score:2)
Re: (Score:2)
* Urk * (Score:3, Funny)
Re: (Score:2)
I know I twitched a little when I read the "Java in Javascript" part. A really neat hack, tho.
Thing is Internet Explorer has supported lots of languages - even those not meant for a web browser! - for writing full-featured client-server code. They're called viruses.
As a side note... (Score:3, Funny)
google Web Toolkit? (Score:2)
Re: (Score:2)
GWT is an API. You write your server side java code to use the GWT library to do its display instead of one of the alternatives like AWT, SWT or Swing. GWT then takes care of sending the right Javascript to the client to do the display. The client never gets sent any Java.
This is a byte code interpreter written in Javascript. You send it compiled Java, and it interprets it using Javascript. Presumably it has limited support for one of the aforementioned graphics APIs, probably AWT, and probably only enoug
Why? (Score:2)
Re: (Score:2)
Re: (Score:2)
More important languages (Score:2)
Number #1 app (Score:3, Funny)
This is just a prototype! (Score:4, Funny)
oh, goodie (Score:2)
Development time-line... (Score:2)
Months 2-6: Developer A continues to write no documentation concerning his work in Month 1.
Month 7: Developer A quits, resigns, is fired, or otherwise disappears.
Month 8: Developer B -- Developer A's replacement -- is asked to fix several bugs in the company's web pages. Developer B opens u
Who wants to bet (Score:2)
Re: (Score:2)
However, it is running with the aid of Ruby 1.9 on the server, so I suspect that actual Ruby 1.9 is faster.
java on javascript (Score:2)
1. Google Web Toolkit compiles java to javascript.
2. XML11 project actually implements the JVM in javascript. That's right, their script *interprets* and *executes* java bytecode. They let you do things like run existing AWT applications unmodified, by emulating AWT in HTML. Of course, this was pretty slow the last time I looked.
Re: (Score:2)
Re:Java running under Javascript... (Score:4, Insightful)
By the way, you can obviously rewrite critical portions of Java code in C as well... Being able to call out to C is not an excuse for the current Python and Ruby implementations being complete trash. There are lots of languages that provide a similar level of abstraction that have far better performance and a threading model that isn't a complete joke.
Re: (Score:2)
When I first started messing around with Java (fairly shortly after it was released), it really was much, much slower than compiled code of just about any language. I wasn't a big Perl user at the time, so I really couldn't compare, and the joy of Python wasn't even a twinkle in my eye.
Re: (Score:2)
Yes, in the early days, the byte code interpreter was just that - an interpreter, and the garbage collection was naive and blocked all threads when it kicked in, leading to the notorious GC pauses at random times (great for all those Java games that sprung up in the early days).
After Java 1.2 was released, Sun released a drop in replacement "Server VM" which had "hotspot" compilation, where programs were profiled at runtime and parts of the code that were frequently called were compiled to native code. In
Re: (Score:2)
Version 6 of Sun's JVM now regularly outperf
Re: (Score:2)
It seems like Java really has a bad case of "first impression syndrome." At the time, the only thing that it really seemed to have going for it was wide compatibility--and even that was not always true (I've had to deal with incompatibility issues between JVM versions in some applications that we support.) It sounds like Java is pretty nice now, so I may have to give it another look.
Re: (Score:2)
Pardon my ignorance (I've never played with either Java OR virtual machines...) but has anybody ever created some sort of platform where each C++ app runs in its own VM? Wouldn't that give you nearly full c++ performance, while also giving nearly Java's security and memory protection?
Re: (Score:2, Informative)
Java is slow when done inefficiently, like any other programming language. In Java code that is implemented correctly, speed will be one your lesser worries. In fact, under certain conditions, Java can rival, and even surpass, native code for speed. The implemented correctly is a much bigger one.
Re: (Score:2)
Exactly. JIT'ed code performs quite well.
For anyone trying to make a Java program run faster, the first thing I always do is look at the profiler output (java -Xprof Foo) and see how much time is spent in the GC. If it's more than a couple %, you've got too many new's and too much garbage collection going on -- and 5% time in the GC represents far more than a 5% performance hit, thanks to thread switching overhead and thrashing the CPU caches. Basically, never ever have a new in an inner loop -- you wo
Re: (Score:2)
Okay, we do multi-threaded number cruching and that involves wait()ing, but is that high amount of waiting normal? Could it be an indicator of using too many worker threads (which would make sense as I tested with a small data set and thread-happy settings)?
Oh, and we spendi upwards
Re: (Score:2)
I don't know of an easy way to figure out what the GC is cleaning up. -Xloggc and such may help, or may not. I would say that much waiting is a bad thing -- you should likely be doing less of it, though how to accomplish that I can't say without seeing the code.
You say you're doing text crunching; I'd look for string ops as possible culprits for the GC activity. Remember, for Strings, a = b + c involves a hidden new -- and therefore a GC operation if a has been initialized before (all string objects ar
Re: (Score:2)
Re: (Score:2)