Slashdot Mirror


JavaScript JVM Runs Java

mikejuk writes "The world of software is made slightly crazy because of the huge flexibility within any computer language. Once you have absorbed the idea of a compiler written in the language it compiles, what else is there left to gawp at? But... a Java Virtual Machine JVM written in JavaScript seems like another level of insanity. A lone coder, Artur Ventura, has implemented a large part of the standard JVM using JavaScript and you can check the code out on Github. Notice this isn't a Java to JavaScript translator but a real JVM that runs byte code. This means it could run any language that compiles to byte code." Bonus: on Ventura's website is a set of visual notes from a talk he gave titled "My Language Is Better Than Yours."

61 of 234 comments (clear)

  1. Javaception by embolalia · · Score: 5, Funny

    So you could write a browser that supports JavaScript in Java, and then run the browser in itself?

    1. Re:Javaception by linuxgeek64 · · Score: 5, Informative

      It doesn't implement that much of the standard JVM.

    2. Re:Javaception by DigiShaman · · Score: 4, Funny

      Ouroboros?

      --
      Life is not for the lazy.
    3. Re:Javaception by cb88 · · Score: 3, Informative

      Sun already did that with HotJava it supported javascript.

    4. Re:Javaception by afabbro · · Score: 4, Funny

      10 Write Browser in Java

      20 Write Javascript engine in Browser

      30 GOTO 10

      --
      Advice: on VPS providers
    5. Re:Javaception by game+kid · · Score: 4, Funny

      Now we just have to do all of that in a Minecraft map so the CPU collapses under its own virtual weight!

      --
      You can hold down the "B" button for continuous firing.
    6. Re:Javaception by Joce640k · · Score: 4, Funny

      Ummmm, Java != Javascript.

      I thought that bit of confusion was cleared up by now. I was wrong.

      --
      No sig today...
    7. Re:Javaception by TheInternetGuy · · Score: 5, Funny

      Think before posting please.

      Seriously, if we are going to put that kind of requirement on posters, I will probably have to cancel my slash dot account

      --
      If my comment didn't sound as good in your head as it did in mine, then I guess we all know who's to blame
    8. Re:Javaception by Anonymous Coward · · Score: 2, Insightful

      Not true! Since you're performing the execution of the byte code yourself you could implement the time slicing yourself. It wouldn't be easy sure - but it wouldn't be "virtually impossible". It'd all run within a single thread in the browser but emulating multiple threads in your JVM.

    9. Re:Javaception by sydneyfong · · Score: 4, Funny

      but it wouldn't be "virtually impossible"

      Exactly. It'd literally be virtually possible if you control the whole virtual machine...

      --
      Don't quote me on this.
    10. Re:Javaception by alexhs · · Score: 5, Interesting

      So you could [...] run the browser in itself?

      Old news. Try chrome://browser/content/browser.xul in Firefox (doesn't seem to work as a clickable link, though).
      See here for more options.

      --
      I have discovered a truly marvelous proof of killer sig, which this margin is too narrow to contain.
    11. Re:Javaception by StripedCow · · Score: 4, Funny

      This is modded "funny", but actually this would be very useful. Because you could send the browser along with your HTML and be done at once with all browser-compatibility problems. Plus you could make browsers supporting other languages (e.g., Python, Haskell, you name it).

      Of course javascript would not be the appropriate target-language for this (I guess, due to efficiency issues), but the idea in itself is very interesting. A better target-language would be closer to the machine (no closures, and no garbage collection); the NaCl project might actually be a better candidate.

      I'm betting that somewhere, somebody is already writing a browser in NaCl.

      --
      If Pandora's box is destined to be opened, *I* want to be the one to open it.
    12. Re:Javaception by DrXym · · Score: 2

      Well I suppose Java did come from that situation a long time ago - so called "green" threads. So I concede you could probably use some crappy timer / yield type model. The timeslice / yielding would be the issue and all the synchronization / deadlock issues would be fun too.

    13. Re:Javaception by Anonymous Coward · · Score: 3, Interesting

      If you don't care about the quality of your multithreading (and don't care if the whole thing just runs on one core of your multicore system), then writing threads into your JVM shouldn't be hard (assuming the hard part of writing the JVM has already been done, of course): Just have an interpreter for each thread (holding all thread-specific data), and then just cycle through all threads, letting each non-blocked one execute a fixed number of instructions before turning to the next (the interpreter class would have a method execute_instructions(number_of_instructions_to_execute) which would be called). Since, from the JVM's point of view, everything would run strictly synchronous, synchronization would not be an issue. Note that you'd not even use a timer.

    14. Re:Javaception by DrXym · · Score: 3, Interesting

      You'd have to use timers. The JS is running in a browser. If you let code for as long as it likes the browser won't be responsive and eventually will start complaining to the user about the fact. The JS would have to execute a bit of code, find some convenient point to halt, set a timer, drop out back to browser and then execute some more when the timer fired. Even if there was just one thread. When you have more than one thread you'd have to do the same for multiple threads via some kind of scheduler.

    15. Re:Javaception by Anonymous Coward · · Score: 2, Interesting

      A more interesting loop would be this:

      Write Editor with Lisp interpreter in C. (check)
      Write Browser with JavaScript interpreter in Lisp. (partial check -- I don't think w3 supports JavaScript)
      Write JVM in JavaScript. (check)
      Write x86 emulator in Java. (check)

      Now run the editor on your x86 system, run the browser in that editor, run the JVM in that browser, run the x86 emulator in that JVM, repeat.

    16. Re:Javaception by Zortrium · · Score: 5, Interesting

      Warning: shameless self-promotion ahead: I've also written my own JVM interpreter in JavaScript, which supports a substantial amount (perhaps most?) of the standard JVM, including threading, synchronization, reflection, rudimentary I/O, and most of the standard library classes (e.g., HashMap and Random). There's a lot of hackery involved but it's totally doable.

      Also, mine runs on Rhino, which is itself written in Java, so it's C (JVM) running Java (Rhino) running JavaScript (JSava, my interpreter) running Java (the user program). How's that for meta-execution?

    17. Re:Javaception by nschubach · · Score: 2

      For instance...

      See: http://nodejs.org/

      JavaScript outside the browser.

      --
      Every time I start to have faith in humanity, I ruin it by driving to work between 7 and 8 am.
    18. Re:Javaception by Rysc · · Score: 2

      How long before CPU benchmarks are measured in how deeply you can nest this and still achieve some fixed number of results per second with a computationally intensive task?

      "Doom3 is running at 30FPS... inside 8 levels of emulation."

      --
      I want my Cowboyneal
    19. Re:Javaception by Spudley · · Score: 2

      So you could write a browser that supports JavaScript in Java, and then run the browser in itself?

      And if you run it in a modern browser, it would still run faster than javascript in IE8.

      --
      (Spudley Strikes Again!)
  2. Anything which can be written in JavaScript ... by Musically_ut · · Score: 5, Funny

    ... will be eventually written in JavaScript. ~ Atwood's Law (circa 2007)

    --
    Never trust a spiritual leader who cannot dance -- Mr. Miyagi
  3. Not A New Concept by DERoss · · Score: 3, Interesting

    Forty years ago, a major software system for operating unmanned space satellites for the U.S. Air Force was written in a language called JOVIAL J4. The JOVIAL J4 compiler was itself written in JOVIAL J4.

    Originally intended for a lifetime of 10-15 years, the system was actually in use for 20 years.

    1. Re:Not A New Concept by bucky0 · · Score: 5, Interesting

      I feel like that sort of bootstrapping is normal. GCC's written in C, afterall.

      --

      -Bucky
    2. Re:Not A New Concept by afabbro · · Score: 3, Informative

      Forty years ago, a major software system for operating unmanned space satellites for the U.S. Air Force was written in a language called JOVIAL J4. The JOVIAL J4 compiler was itself written in JOVIAL J4.

      Hardly unusual. GCC is written in C.

      This is not quite the same thing.

      --
      Advice: on VPS providers
    3. Re:Not A New Concept by TheRaven64 · · Score: 4, Informative

      Most compilers work like that. The Java compiler is written in Java (as you discover when it crashes and you get a Java stack trace). Smalltalk was written in Smalltalk and even modern Smalltalk implementations like Pharo are written in Smalltalk. The typical trick is to write a small compiler for a subset of the language in another language and then use it to compile the rest of the compiler. For example, the core of the Squeak VM is written in a subset of Smalltalk that is fairly easy to translate to C. This is then translated to C and compiled with the target system's C compiler. You then have enough of the VM running for it to load the rest. Early Pascal compilers did the same. In the '70s, having the compiler written in the language was considered a test of whether something was a 'real' language (since then we've learned that languages that are good for writing compilers are not necessarily good for other things. Well, some of us have...)

      --
      I am TheRaven on Soylent News
    4. Re:Not A New Concept by lucian1900 · · Score: 2

      Even CoffeeScript is self-hosted.

    5. Re:Not A New Concept by Just+Some+Guy · · Score: 4, Interesting

      The JOVIAL J4 compiler was itself written in JOVIAL J4.

      Want something really mind-blowing? PyPy is a Python interpreter written in Python. It includes a tracing JIT compiler to optimize hotspots as it runs to get about 5 times faster than the native C Python. I've used it and I still can't quite believe it.

      --
      Dewey, what part of this looks like authorities should be involved?
  4. yo dawg by Anonymous Coward · · Score: 4, Funny

    So we heard you like java...

  5. Hey Bro... by bennomatic · · Score: 4, Funny

    ...I heard you like Java in your script so I wrote you a JVM in Javascript so you can run Java while you're scripting.

    Joking aside, this is not going to help the amount of confusion people have with regards to Java not being the same as Javascript *at all*.

    --
    The CB App. What's your 20?
    1. Re:Hey Bro... by Mitchell314 · · Score: 5, Funny

      Of course there's a difference. Java is defined as the language that runs on top of javascript. This is simple, it's just a compiled language that runs atop of an . . . interpreted . . . of an interpreted . . . it's a compiled . . . it's a compiled language that runs on an interpreted . . . okay guys, really, what the fuck? I think I just heard the sound of part of the universe and a good chunk of logic spontaneously imploding. I'm going to go cry now, and thanks to these dipshits my tears will probably fall sideways upwards now.

      --
      I read TFA and all I got was this lousy cookie
    2. Re:Hey Bro... by PCM2 · · Score: 2

      Hey, 1996 calling. Does our interpretation of how Java works still fit? Because if you're not using it anymore, we'd like it back.

      --
      Breakfast served all day!
    3. Re:Hey Bro... by TheRaven64 · · Score: 2

      What is this, 1998 appreciation day? They were working on JIT compilers for the first release of Java! Modern JVMs all use JIT compilation extensively. The only ones that don't are ones designed to run on very small embedded systems, which use Jazelle, which is basically a Java bytecode decoder on the front of an ARM chip that executes most bytecodes directly and traps on the difficult ones.

      --
      I am TheRaven on Soylent News
  6. DOM-Interface for byte code by maweki · · Score: 5, Interesting

    For years I've been saying that we need a DOM-Interface for byte code in Browsers and everytime I get downvoted. Nice to see people exploring in these directions now.

    "we do need DOM-Bindings for Bytecode now more than ever. It would be so great to write code in a language of my choice and compile it to Browser-Bytecode with DOM-Bindings. This would make it possible to deliver more proprietary code without making browser-plugins or something similar."

    "What we really need are DOM-Bindings for Bytecode. So you can use every language you want that is capable of compiling to bytecode and send it to a browser. This would make it easier for the developer and bytecode is easier and faster for the browser to execute."

    1. Re:DOM-Interface for byte code by devent · · Score: 4, Insightful

      Yes, would make sense, wouldn't it? Instead that every browser have to interpret the slow JS language (yes, JS language is slow to interpret, because of the design choices. Just watch some of Doug Crockford videos), we could all just agree on a byte code standard like the Java byte code standard.

      Then you could develop web applications in your choice of language and not just in JS. On the server you upload just the byte code, like the compiled Java source code (.class).

      For example, you could develop in Ruby, Python or Java, and compile the code to the byte code for browsers. Then you deploy the byte code on the server.

      But the web-developers are so stubborn with their JS language, I don't know why.

      --
      http://www.mueller-public.de - My site http://www.anr-institute.com/ - Advanced Natural Research Institute
    2. Re:DOM-Interface for byte code by martin-boundary · · Score: 4, Interesting

      I'd rather have source code than byte code. The web was built on visible code like HTML. Do you think it would have exploded if people couldn't look at the source to figure out how some neat web page was written?

    3. Re:DOM-Interface for byte code by devent · · Score: 2

      What have that do to with your argument? Libraries like JQuery will still be open source.
      I would rather think that Web exploded because it's build on open technologies and open source software.
      And if the Byte Code is a standard, it's as simple as a editor or disassembler to use to see the code.
      Also, I don't think you can read that code: http://code.jquery.com/jquery-1.7.min.js

      --
      http://www.mueller-public.de - My site http://www.anr-institute.com/ - Advanced Natural Research Institute
    4. Re:DOM-Interface for byte code by jimicus · · Score: 2

      Of course we had that with Java applets a few years ago - they didn't really take off.

      A number of reasons for this exist: first is there were a couple of implementations of the JVM and they weren't all compatible with each other. Second is that the same version of the same JVM (Sun's at the time) exhibited subtly different behaviour on different platforms - meaning it was fantastically easy to wind up with something that only worked well on one or two platforms. ISTR font handling in the UI was a big killer - there were a few ways to do it and unless you did it a particular way, you wound up with an applet where the widget text was only readable on Windows.

    5. Re:DOM-Interface for byte code by the+entropy · · Score: 3, Interesting

      How is what we have now with things like minified js any different than bytecode? Have a look at the source for gmail, or the minified version of jquery. You need analysis software to have any hope of making any sense of it and it's exactly the same then as a bytecode decompiler.

      jQuery is open source, which means you can get the non-minified version and read that to know how it works, but I would dare anyone to make sense of the 100s of KBs of obfuscated js that is the gmail interface(or quite a few other popular products and services for that matter).

    6. Re:DOM-Interface for byte code by TheRaven64 · · Score: 2

      Even sandboxed in a VM, I'd be inherently wary about executing server-compiled byte-code

      Why is this any different from executing server-supplied source code?

      --
      I am TheRaven on Soylent News
  7. Re:This is nonsense. by afabbro · · Score: 4, Insightful

    Javascript != Java. "Javascript" is just a naming rip-off. So what's the big deal?

    It's like writing a C compiler in Bourne shell. The point is less about the name than about the complexity and absurdity.

    --
    Advice: on VPS providers
  8. Interesting mind set by sgt+scrub · · Score: 3, Insightful

    The motivation for this effort is put very well in Artur's blog. He argues that rather than build JavaScript into web browsers they should have a virtual machine so that any language can be used. As well as this advantage, he also points out that with a JVM type approach you get automatic sandboxing and simply sending the JVM to the server provides browser independent persistence.

    I think it would have been easier to build a VM into a browser but I doubt it would have gotten this much attention. Still. It is a cool project.

    --
    Having to work for a living is the root of all evil.
  9. Re:This is nonsense. by Arancaytar · · Score: 2

    Names have nothing to do with it. They could have just as well written their JVM in Python, Perl or PHP - except that none of those languages have undergone ten years of browser vendors fighting head to head to shave off another few microseconds on benchmarks, so their interpreters would be too slow to run an effective virtual machine.

  10. Problem is speed by Anonymous Coward · · Score: 2, Interesting

    I doubt that Javascript accelerators are good at optimizing this, but it's not fundamentally impossible to run it close to JVM speeds. JS is a language that in nutshell is self modifying code, so it can act as a translational layer which in the end enables running Java in a Javascript engine (interpreter,VM or whatever). It could be compared to Dart which also runs another type of language in JS.

  11. Yo dawg, I heard you like bloat by wye43 · · Score: 4, Funny

    ... so I've put bloat in bloat, so you can wait while you wait.

  12. Really? by aglider · · Score: 5, Informative

    This

    This means it could run any language that compiles to byte code.

    shoud read as
      This means it could run any language that compiles to Java byte code.

    --
    Sent as ripples into the electromagnetic field. No single photon has been harmed in the process.
    1. Re:Really? by dkf · · Score: 2

      This

      This means it could run any language that compiles to byte code.

      shoud read as

        This means it could run any language that compiles to Java byte code.

      Technically, it's running anything that compiles to JVM bytecode. There are a number of languages that do that, one of which happens to be Java. (Yes, JVM stands for Java Virtual Machine and it would indeed be odd if Java didn't compile to it, but Java isn't JVM bytecode, just as C isn't native machine code.)

      --
      "Little does he know, but there is no 'I' in 'Idiot'!"
  13. Re:This is nonsense. by HJED · · Score: 2

    JVM is a run time environment not a compiler (although the environment includes a JIT compiler).
    Also I think the point was javascript is seen as a less powerful language

    --
    null
  14. Not mind blowing when ... by fsckmnky · · Score: 3, Insightful

    ... you understand that a computer language, is a mapping of human readable symbols -> cpu instructions, either direct or indirect. If the mapping results in a set of cpu instructions that implements another language, you get another language.

    It's not rocket science people, its just math. Wait, maybe it is rocket science.

  15. This is completely unnecessary. by DamnStupidElf · · Score: 5, Funny

    Fabrice Ballard already wrote an x86 emulator in javascript. Just install the standard x86 JVM inside of that and you're good to go.

    1. Re:This is completely unnecessary. by jenic · · Score: 4, Funny

      Fabrice Ballard already wrote an x86 emulator in javascript. Just install the standard x86 JVM inside of that and you're good to go.

      Yes, that's why this is completely unnecessary.

    2. Re:This is completely unnecessary. by Anonymous Coward · · Score: 3, Informative

      Dammit, Fabrice Bellard keeps making me feel like I am as dumb as a plate of chicken.

  16. Re:This is nonsense. by Shoe+Puppet · · Score: 2

    I can see a use case: Once we have a mostly complete JVM interpreter with satisfactory performance written in Javascript, we can use any language that supports the JVM instead of Javascript.

    --
    (+1, Disagree)
  17. Why now? by thegarbz · · Score: 4, Interesting

    Legit question from a programming novice: Why are all these discoveries coming up now? Hasn't JavaScript been around for 10+ years now? Is there something that has changed recently that makes people pursue these strange coding goals?

    1. Re:Why now? by sydneyfong · · Score: 4, Informative

      Javascript speeds have increased greatly due to the reheated competition by browser vendors (it wasn't too long ago that the only thing really existed was IE6). Thus in the past 10 years, nobody in their right mind would expect a x86 emulator, a JVM etc. to be implementable in Javascript at tolerable speed.

      In fact, few expect these "discoveries" to happen so soon and so quickly, but since somebody proved it possible to do crazy things on Javascript, everyone with too much time on their hands are jumping on board and having fun with these projects.

      --
      Don't quote me on this.
  18. Re:This is nonsense. by Adrian+Harvey · · Score: 3, Funny

    It's like writing a C compiler in Bourne shell. The point is less about the name than about the complexity and absurdity.

    Isn't it more like writing a C compiler in C-shell - at least name-wise :-)

  19. Re:This is nonsense. by deniable · · Score: 4, Funny

    More like writing a Python interpreter in Perl. It'll work, but you'll feel a little dirty afterwards.

  20. Re:First I was like D: .... by Lennie · · Score: 2

    Actually, if the engine to run javascript is loaded from the website where your java app is downloaded from there is a much higher chance it will work.

    It is what a lot of java programs already do, deliver their have their own JRE in a directory.

    --
    New things are always on the horizon
  21. From the notes by kikito · · Score: 2

    "Computers languages are how we tell computers what to do"

    That's so in the past.

    Nowadays, computer languages nowadays are (minus some exceptions) all about telling people what computers do.

  22. Other bytecodes by tepples · · Score: 2

    I think aglider's point is that there exist notable bytecodes other than JVM bytecode, such as Microsoft CIL, UCSD p-code, Infocom Z-code, SCUMM bytecode, LLVM bitcode (which is already handled by Emscripten), and more.

  23. LBA-complete, not Turing-complete by tepples · · Score: 2

    Provided JavaScript is Turing-complete (and there are very few useful languages that aren't)

    No language is Turing-complete because no machine has unbounded memory. A linear bounded automaton (LBA-complete) is a better model of computers that exist in the physical world.

    Note the keyword here is possible. Desirable and practical are totally different matters altogether.

    Especially because even once you've ignored speed, neither Turing completeness nor LBA completeness makes any guarantee about I/O capability.

  24. Re:This is nonsense. by Oligonicella · · Score: 4, Funny

    That's a byproduct of Perl. What you write has nothing to do with it.