Hacker News new | ask | show | jobs
by naikrovek 2646 days ago
Java inside the browser doesn't exist. That's fine. That's not what I'm talking about or making comparisons to.

Java runs just fine outside of the browser. You don't even need a browser installed to run Java applications.

Javascript and WASM run fine inside the browser, and that's not what I'm talking about or making comparisons to.

I'm talking about running WASM binaries outside of the browser. Similar to how Java runs outside of the browser. Very similar, actually. Java needs a runtime to be installed or available, so will WASI. Java is a write-once-run-anywhere type of platform, so is WASI.

Why does WASI need to exist if Java already exists for the intended uses?

3 comments

> Why does WASI need to exist if Java already exists for the intended uses?

IMO, the reason is that the market showed that Java never got used like that. I can only speculate on the reasons, but as far as I can tell it comes down to:

1) The sandbox isn't good enough. See my sibling comment -- the separation between WASM and WASI makes a lot of sense, and I don't think Java had that.

2) The fact that you can't port C code to the JVM easily. In fact WASI addresses exactly that -- the API is more like POSIX than a brand "new" bunch of JVM APIs, which makes it easier to port existing native apps (C, C++, Objective C, Rust, etc.)

In other words, there's still a lot more "value" in native code apps than JVM apps. And it's too hard to port native code applications to the JVM.

For example, Photoshop, Illustrator, Word, Excel, etc. were never ported to the JVM. There are actually better analogs in the browser than on the JVM.

I ran windows for 15 years and Linux for 10-15 years. On neither of those platforms have I ever needed a JVM. I used to play online chess in a Java applet, and that's about it. Most common desktop apps avoid dependencies on the JVM. Probably the only reason I could think of installing one is to use Eclipse or IntelliJ.

The browser is the main reason why anybody had a JVM installed in the first place. Without that hook, the JVM becomes much less important on the client. It's still very important on the server.

Java is important and successful, but it empirically did not succeed at some of its design goals. Of course, WASM and WASI may also fail -- that remains to be seen. But to me they look like a good attempt, based on significantly different technical foundations.

What is blocking C -> JVM compilation?

There seems to be such a compiler here: https://github.com/bedatadriven/renjin/blob/master/tools/gcc...

I would say it's:

1) There's a large difference between a compiler that exists and an efficient one. The JVM is designed for Java and not C, which makes efficient compilation hard / impossible. Graal as mentioned is an extremely unusual research technique that may make it feasible.

This comment is related:

https://news.ycombinator.com/item?id=19065829

Basically people underestimate how hard it is to make a VM that can run multiple languages. JRuby and Jython work, but it takes heroic efforts.

2) The interface to the OS, as mentioned. You can maybe run some algorithms like image processing, but try running the native sqlite code on the JVM, or something even hairier involving networking. It's not straightforward.

WASI provides something that's closer to what C programs expect than what Java provides. As I said, the market voted with its feet. All the friction adds up. If you try hacking on that compiler or manually porting code, you'll probably get a sense of why that is.

The JVM doesn't run languages that weren't designed for it, like C or C++ or Rust. WebAssembly does.

This is important for a lot of reasons- huge amounts of existing code you can now use without JNI or whatever, a higher ceiling for optimization, more freedom to implement new kinds of languages.

The core WebAssembly standard is also much smaller than Java, as a consequence of this design. This makes it easier and/or more feasible to deploy in more scenarios, even without subsetting things the way mobile/embedded Java does.

> The JVM doesn't run languages that weren't designed for it, like C or C++ or Rust.

You can run all those languages on top of the JVM!

The JVM also runs many other languages which weren't designed for it, like Ruby and Python.

True, I forgot about Graal. IIUC though that's somewhat different from what WebAssembly has done- Graal doesn't define any sort of stable compiler target for C, or produce JVM bytecode from C, but instead (via Truffle) JITs C source or LLVM IR via partial evaluation, right?

That would suggest (again IIUC) Graal/Truffle as a mechanism for using the JVM as a WebAssembly runtime. The WebAssembly format and its associated environment/binding system are a portable way to encode C binaries, with significant benefits over JVM bytecode- that's probably the comparison I should have made.

No that's right, the stable compiler target is someone else's job.

But I wasn't comparing to WebAssembly, so you'll need to argue that point with someone else.

You could build a WebAssembly interpreter on Truffle, yes. I'm not sure anyone's tried it yet so that project is available.

> You can run all those languages on top of the JVM!

Do you have a source for this? I'm sure it's theoretically possible in a Turing-completeness sense, but considering Java lacks any concept whatsoever of a pointer, and considering that most (if not effectively all) C code uses pointers pretty extensively, I find it unlikely that this statement is meaningfully true without some severe efficiency penalties.

For example Sulong runs C, C++, Rust https://llvm.org/devmtg/2016-01/slides/Sulong.pdf.

I use it to run unmodified C extensions for the Ruby programming language on top of the JVM.

Truffle C is another example https://www.manuelrigger.at/downloads/trufflec_thesis.pdf.

Thanks to GraalVM. Well, that wasn't always the case and it prompted the need for WASM.
Webassembly came from asm.js, which came from Emscripten. These 3 are a lineage of the same c compilation model targeting a JS Uint8Array as the c heap. Emscripten was published a long time ago, probably 10 years? So, JVM has byte arrays too...
Python, Ruby and Common Lisp were designed for the JVM?
To the extent that their object and memory models match the JVM's, you might say they were. To the extent that they don't, their JVM implementations wind up with warts and inconsistencies.

(But this is pedantry, you clearly know what I meant.)

No, I did not know what you meant. There is a difference between guessing and knowing.
Because Java exists for a different purpose, namely to run Java. (Ever tried to run your C or rust code on the JVM?)