
Java Virtual Machine in pure Node.js - binarymax
https://github.com/YaroslavGaponov/node-jvm#!
======
jvilk
Interesting! It looks like you've reimplemented portions of the Java Class
Library rather than use e.g the existing class files from OpenJDK.

My current research project, Doppio [1], implements the native portions of the
OpenJDK Java Class Library so it can use an unmodified copy of the OpenJDK
JCL. As a result, it can run a bunch of nontrivial programs
(javac/javap/Rhino/Kawa-Scheme).

One issue you will run into is with multithreading. Since JavaScript has no
true threading implementation with shared memory, you'll need to be able to
suspend and resume virtual JVM threads. For this reason, Doppio maintains an
explicit JVM stack representation.

Anyway, feel free to check out our code, reuse portions of it, or contribute
if you're interested; it's MIT Licensed and under active development. :)

[1] Demo: [http://doppiojvm.org/](http://doppiojvm.org/) Code:
[https://github.com/int3/doppio](https://github.com/int3/doppio)

~~~
drtse4
Nice experiment and a huge undertaking, do you already support the current
implementation of java.util.concurrent? (requires atomic CAS through
sun.misc.Unsafe and ability to park threads)

~~~
jvilk
Note that the demo is somewhat old; I do not know if it has park implemented.
You'll want to build it from GitHub, which we've made relatively
straightforward.

We are planning to update the demo (and perhaps post on HN) once I fix some IE
issues (since we strive to be compatible with IE9).

------
bane
Really cool hack, and yet I can't help but think that the mile-high software
stack between the developer and the CPU continues to grow meaning we're stuck
in a perpetual game of filling up spare CPU cycles with nothing in particular.

I'm sure I'll see a demo showing off something that barely runs on modern
hardware that we were more than capable of running with good performance in
1990.

 _sigh_ I feel like I'm being such a Debbie-downer even if this is a really
cool hack.

~~~
j_baker
Now we just need to wait for someone to run Rhino on this so we can have
Javascript hosting a JVM which hosts Javascript.

~~~
kodablah
Nashorn would be more appropriate once it is released. It leverages the JVM
dynamic invocation instructions better IIRC.

~~~
sitkack
And Nashorn should jit to bytecodes, which should jit back to JS. Like
realtime closure compiler.

~~~
noptic
VMception?

------
dlsym
18ms for calculating Fibonacci numbers from 1 to 10. This is definitily
bringing JAVA back to the good old days of enterprise execution speeds. :-)

Really cool proof of concept! I would really enjoy seeing more projects like
this!

~~~
mtdewcmu
Who doesn't wish it was still 1999? Hot stock market, millenium mania, cheap
gas, dot-coms. Really slow Java makes it all come rushing back.

~~~
ahoge
> _[1999 's] slow Java_

Java 1.2 (1998) used a JIT and a generational collector. It was probably
faster than today's Ruby, Python, or PHP.

What was slow was start up. Applets in particular were really horrible since
they completely froze the browser for several seconds.

If I remember correctly, this was finally fixed many years later with some
version of Java 6.0.

~~~
mtdewcmu
It didn't go full speed until Sun released HotSpot, which was probably a year
or two later. The farther back in time you go, naturally, the slower Java was,
sort of like the universe as you go back to the Big Bang. You also could have
made more in the stock market with each receding year. But 1999 was a
memorable year.

------
angersock
Finally, I can run JRuby on my node server!

~~~
epsylon
From poster with similar project above:

[https://github.com/int3/doppio/wiki/Successes](https://github.com/int3/doppio/wiki/Successes)

> We're also making progress on others, but we're not quite there yet: JRuby,
> Clojure, Scala REPL

I think I just had a micro heart attack reading that.

~~~
agumonkey
and clojurescript, circle is complete.

~~~
tomp
You mean Rhino (the JavaScript engine written in Java)? :)

------
tzaman
Now I'll have even bigger problem explaining what the difference between Java
and Javascript is! :)

~~~
golergka
It's like Car and Carpet. But it's also the kind of car that you can build out
of carpets.

~~~
egeozcan
...or the carpet that you can build out of cars, considering the Ringojs.

~~~
ahoge
Java 6.0 comes with JSR 223 ("scripting for the Java platform"). It's a
framework for hosting scripting engines. 6.0+ is shipped with a JavaScript
engine based on Mozilla's Rhino.

[http://en.wikipedia.org/wiki/Rhino_%28JavaScript_engine%29](http://en.wikipedia.org/wiki/Rhino_%28JavaScript_engine%29)

------
adultSwim
For anyone interested in the details:

-Reads in real .class files

-Uses the JS run-time to implement the Java run-time (e.g. there isn't a garbage collector written in JS, the JS collector is used)

-Only implemented part of java.lang and java.io

~~~
drtse4
A handful of classes with a few of their methods, in java.lang and java.io.

------
szatkus
I created something similar some time ago.

[https://github.com/szatkus/js2me](https://github.com/szatkus/js2me)

Although I targeted only J2ME subset. Also I found few similar projects:

[https://github.com/nurv/BicaVM](https://github.com/nurv/BicaVM)

[http://plasma-umass.github.io/doppio/about.html](http://plasma-
umass.github.io/doppio/about.html)

------
drtse4
Just a toy implementation[1], if someone is wondering.

[1]The basic .class file attribute parsers are located in libs/classfile,
while a simple bytecode interpreter can be found in jvm.js. It doesn't load a
real runtime library (classpath, apache harmony,openjdk,etc...) but it
partially implements a few java.* classes in pure javascript under libs/java.

~~~
danbruc
Can someone point out what it can do and what it can not do; I am curious what
2.000 lines of JavaScript can do. How much more code would it take to get a
full featured implementation?

~~~
drtse4
It has a java bytecode interpreter and an initial implementation of a
classfile loader. As said above it doesn't have a full implementation of the
java runtime, just a few classes to be able to print out to standard output
and su things with string and not much else (at least from what i've seen of
the content of libs/java).

Regarding the loader, it can load a few sections from a classfile, the one
containing methods bytecodes, the exceptions table and the constant pool (i.e.
where all your string are stored with some other "constants"). How much it
could take for a full implementation? It depends of what you mean by _full_.
Even using one of the opensource runtime library like
openjdk,classpath,harmony/android i'd say a few years done solo, and not much
less with more people, to build something complete and stable enough for
general use. Definitely not a simple project.

------
stevekinney
Atwood's Law: any application that can be written in JavaScript, will
eventually be written in JavaScript.

~~~
bsg75
JS.js

~~~
adam-a
[https://github.com/jterrace/js.js/](https://github.com/jterrace/js.js/)

~~~
cpeterso
And Mozilla's (mothballed) Narcissus:
[https://github.com/mozilla/narcissus](https://github.com/mozilla/narcissus)

------
cpeterso
Mozilla has written pdf.js to replace Adobe's PDF plugin and shumway.js to
replace Adobe's Flash plugin. When will we see a java.js that replaces Java
applets on the web?

~~~
film42
But really though, that could be amazing! Or fail! I can't decide.

------
mariusz79
I'm waiting for a JavaScript interpreter written in Javascript.

Edit: It's already here :)
[https://github.com/jterrace/js.js/](https://github.com/jterrace/js.js/)

~~~
bkurtz13
Atwood's Law ad extremum.

~~~
sitkack
Atwood's Ouroboros

------
antonpug
Now, my question is. WHY!? I might be crazy, but does anyone see a legitimate
use for this?

~~~
saraid216
This question on this kind of topic needs to stop being asked on Hacker News.
It's fair to ask whether or not there was a material usage intended for the
project, but if isn't one, we _really_ need to stop implying that there should
be.

~~~
aryastark
that's not a chance I'm willing to take. This is how PHP started. It was a
reject experiment that escaped the lab. Then some poor souls took it
seriously. Now all of humanity suffers.

~~~
saraid216
[http://php.net/manual/en/history.php.php](http://php.net/manual/en/history.php.php)

Um. No.

------
duiker101
Now you just need to run
Rhino([https://developer.mozilla.org/en/docs/Rhino](https://developer.mozilla.org/en/docs/Rhino))
on it and you will be totally meta.

~~~
jvilk
My current research project, Doppio, can already run Rhino in the browser
using JavaScript. :)

Demo: [http://doppiojvm.org/](http://doppiojvm.org/)

Code:[https://github.com/int3/doppio](https://github.com/int3/doppio)

------
mtdewcmu
I have a feeling James Gosling choked on his coffee this morning. :)

------
6ren
How hard is it to write a JVM? I'd think it pretty easy, as it's intended to
be small and easily portable.

You could even write one in Java (and I'm sure it's been done, many times).
_EDIT_ e.g.
[http://igormaznitsa.com/projects/mjvm/index.html](http://igormaznitsa.com/projects/mjvm/index.html)

Of course, doing all the tricky JIT etc of _the_ JVM is a different story...

~~~
diek
It depends on what you consider the JVM. If you're just talking about
something that can load class files and do some instruction interpretation
(like this project) then that's one thing... if you're talking about
implementing all of the JNI functions within the JVM standard library (awt,
net) along with the concurrency primitives (conforming to the Java memory
model?) then that is a significant multi-year, multi-developer project.

------
knappador
Is PyJNIus support coming anytime soon? The ability to use Python to script
Java calls into the JS run-time to access Cordova API's on top of Android
libraries is really important for developers who have Python, Cython, and Java
skills, but find P4A too daunting compared to Cordova. It's only three more
layers of nonsense.

------
big_lou
This is the worst thing that ever happened

~~~
cmbaus
Worse than the last worst thing that ever happened?

[https://news.ycombinator.com/item?id=6581323](https://news.ycombinator.com/item?id=6581323)

~~~
jenius
haha this is a really excellent callout. well done.

~~~
big_lou
Yep. Mad props.

------
tlrobinson
What does Node have to do with it (besides perhaps a little IO)? Isn't it just
JavaScript?

------
catwell
For comparison, here is a recent JVM in Lua:
[https://cowlark.com/luje/doc/stable/doc/index.wiki](https://cowlark.com/luje/doc/stable/doc/index.wiki)

------
jmpe
With GWT you can translate the Java application to standalone JavaScript files
... and with this implementation you can run Java in a Javascript environment.

Which one would perform best?

~~~
robmcm
GWT with out a doubt. It's running on a smaller stack and the translation is
done at compile time not runtime.

------
nonchalance
The example makefiles appear to use javac.

Next step: Java compiler in node

------
CmonDev
So now I can use one of those Java libraries that implement a JavaScript
engine and finally have JavaScript running on node.js?

------
zshprompt
I made a house out of butter yey!!!!

------
ilaksh
Would be interested in seeing a JVM written in Go.

~~~
bronxbomber92
Why? I must be missing the connection between Node and Go.

~~~
MrBuddyCasino
For 15 minutes of fame on HN, of course.

------
ghostdiver
What's the point of doing this, why not just use Mozilla Rhino?

~~~
chc
Rhino is the opposite of this.

~~~
ghostdiver
It is the right tool for mixed JS and Java codebase.

------
jokoon
I hate java, I hate javascript, I hate node.js, so basically, I just leave a
comment here, and you can make a conclusion about what I think about all this.

~~~
Osiris
Just out of curiosity, what are your preferred languages and server
environments?

~~~
WoodenChair
Based on his HN profile, my guess is Python.

