top | item 16075933

TeaVM – Ahead-of-time transpiler of Java bytecode to JavaScript or WebAssembly

195 points| entelechy | 8 years ago |teavm.org | reply

126 comments

order
[+] kodablah|8 years ago|reply
For the reverse, I wrote a WASM-to-JVM compiler: https://github.com/cretz/asmble.

I think WASM has the ability for us to finally cross the language boundaries without marrying ourselves to C/C++ ABI. But at this early stage, there's no common stdlib for all languages to share.

[+] earenndil|8 years ago|reply
> I think WASM has the ability for us to finally cross the language boundaries without marrying ourselves to C/C++ ABI

Uhhhhh. This sounds like a kind of silly thing to say, when you just described the jvm. Except for the part where there is a common stdlib.

[+] pjmlp|8 years ago|reply
> I think WASM has the ability for us to finally cross the language boundaries without marrying ourselves to C/C++ ABI

That is how many maninframes work, and the original idea behind the CLR.

[+] benjaminjackman|8 years ago|reply
That's really interesting! How does calling WASM code on the JVM work?

And also in addition to a stdlib, maybe a great package management system. Is there a popular crate / maven / npm etc equivalent out there for wasm yet?

[+] taxreform|8 years ago|reply
I read some parts of the spec of WebAssembly recently, and there were several things that disappointed me. To name a few:

1) There is no way to allocate in a function's variable stack continuous memory space that spans more than 8 bytes. This implies that if a function has as a local variable compound data structure such as an array or a struct, you have to a) have a global virtual stack pointer, b) manipulate the stack pointer to allocate space for the data structure in the virtual stack on the memory, c) read from or write to the allocated memory space, and finally d) restore the stack pointer before the function exits. This is exactly like what resulting assembly code of C compiler does. Why do we have function locals as a WASM primitive, then, if we have to end up with manipulating the stack pointer?

2) Currently a function can only return one value even though it is trivial to add support to mutiple return values taking advantage of the fact that WASM is stack-based. And what puzzles me is that mutiple return values is actually mentioned as a possible feature in the future, in several places in the spec. WASM reached the MVP before adding it, which means that we will have to stick with one-return-value functions for quite a long time even if it is added in the future.

[+] acqq|8 years ago|reply
“TeaVM is a primarily web development tool. It’s not for getting your large existing codebase in Java or Kotlin and producing JavaScript.”

“TeaVM is for you, if:

- You are a Java developer and you are going to write web front-end from scratch.

- You already have Java-based backend and want to integrate front-end code tightly into your existing development infrastructure.

- You have some Java back-end code you want to reuse in front-end.

-You are ready to rewrite your code to work with TeaVM.“

http://teavm.org/docs/intro/overview.html

[+] billsix|8 years ago|reply
That word,"Transpile". Lol. GCC transpiles C to assembly.
[+] PyComfy|8 years ago|reply
I always understood it as this

Transpilation: text to text (eg: Scheme to C)

Compilation: anything to anything but usually it implies text to binary code

transpilation is compilation but compilation is not necessary transpilation (the all squares are rectangles but all rectangles ...)

[+] rusbus|8 years ago|reply
I guess it implies a 1-1 or similar conversion without optimizations or multiple levels of intermediate representation. GCC certainly doesn't "transpile" in that case...
[+] kitd|8 years ago|reply
You can think of it as "translate (but in the tool-chain role of a compiler)"
[+] abiox|8 years ago|reply
it's not a new word. i find it odd how some people react to it.
[+] olingern|8 years ago|reply
> If you are a Java (or Kotlin, or Scala) developer who used to write back-end code, TeaVM might be your choice. It’s true that a good developer (including Java developer) can learn JavaScript. However, to become an expert you have to spend reasonable amount of your time.

Oh, this is nice. I finally have a reason to give Kotlin a go. I have some time off and want to write something basic. Pretty excited about this, Rust, and all things wasm popping up.

I am, in no way, excited about debugging it, though.

[+] rufugee|8 years ago|reply
Kotlin compiles to javascript already...why wouldn't you go that route?
[+] rubyn00bie|8 years ago|reply
So... from reading the page linked (admitting my ignorance)... I really wonder how useful this is? Not to be rude, or to talk ill of the work, it’s far better than I could do... but!

With the release of other ahead of time compilers/VMs/features for the JVM/Java9, I cannot fathom this produces a small enough payload for a web app over a mobile connection.

Im probably totally missing something; could someone educate me on practical or even useful but novel applications of this?

[+] konsoletyper|8 years ago|reply
One of the most important features in TeaVM is its inteprocedural dead code elimination algorithm, which allows to produce very small code for simple Java programs. For example, Hello World produces about 40 kb JavaScript, TodoMVC (http://teavm.org/live-examples/todomvc/#/) is only 125 kb.
[+] crazyhatfish|8 years ago|reply
If you look at how much their demos in their gallery downloads, it's in the hundreds of kilobytes on average. Granted these are not full web apps but even still it's roughly on par with the latest web app framework's demos.

Performance is not too shabby either

[+] austincheney|8 years ago|reply
If you are compiling to the JVM the output is probably nothing related to a web app.
[+] exception_e|8 years ago|reply
For those that are wondering but skimmed the page: It looks like they're thinking about SPA framework (Angular/React/Etc) and have a subproject to support them.
[+] norswap|8 years ago|reply
Does this support (a subset of) the standard Java libraries? Where can I find what is supported? (I just saw mentions that reflection wasn't)
[+] gcb0|8 years ago|reply
my guess is no. otherwise it would be called out as a feature with lots of fanfare.
[+] dizon|8 years ago|reply
In the end, how will this differ from a Java applet?
[+] grzm|8 years ago|reply
You don't need to have Java enabled in your browser.
[+] simfoo|8 years ago|reply
The argument against the critics saying WebAssembly would make the web less open was, that you could always disassemble WebAssembly and that said disassembly would be much more readable than for example x86 assembly.

So - since a quick research did not turn up anything - is there a WASM to C "transpiler" that generates readable C?

[+] MaxBarraclough|8 years ago|reply
But that's not really the worry. It's always been possible to obfuscate JavaScript. All that changes with WebAssembly is that it might drive even more needless code, and complicate the web's architecture even further.

For example, web browsers for the blind become far harder to implement when ordinary websites are using heaps of boated JavaScript. (The web used to have HTML for content, and CSS for presentation. No longer. Now it's just a mess.) The problem would be compounded if WebAssembly+canvas were to catch on.

[+] konsoletyper|8 years ago|reply
Currently, if you run TeaVM with 'debug' (-g) option, and WASM target, you'll get both '.wasm', '.wast' and '.c' files. C file is compilable with GCC and you should write several trivial functions manually to get working binary, either '.so' or '.exe'.
[+] xg15|8 years ago|reply
I think, by now "transpile" just means "compile, but in web development".

Because, as we all know, web development you never compile things... /s

[+] stefano|8 years ago|reply
Compilation is for old people writing old programs that run on old things called "computers".

We're fresh and new and iterating quickly on our MVP, so we don't have time to wait for long compilation time. Instead we transpile our code and run it in a serverless environment, a docker container or a browser.

[+] cobbzilla|8 years ago|reply
very cute, i like your sarc.

for those who aren't familiar with transpiling, in this case it's kind of a legitimate use of the term: taking one flavor of compiled code (java bytecode, binary stuff that runs on a jvm) and translating that directly to another type of compiled code (wasm, binary stuff that runs in a browser)

[+] diegoperini|8 years ago|reply
What is the difference between cross-compile and transpile? (Honest question, no sarcasm)
[+] frik|8 years ago|reply
The last thing we need in hindsight to Meltdown and Spectre is running untrusted binaries in the browser aka WebAssembly.

Btw deactivating WebAssembly support in Chrome 63 (up-to-date) doesn't work anymore!!

  chrome://flags/#enable-webassembly
Setting it to "deactivated" does nothing, WebAssembly is still active.
[+] pjmlp|8 years ago|reply
I keep saying that WebAssembly is the revenge of Flash, Applets, Silverlight, ActiveX,...

Just wait until it gets a bit more mature.

It will be the same fun as when Ads moved away from Flash into HTML 5/JavaScript.

[+] abiox|8 years ago|reply
> untrusted binaries

afaik, wasm is not 'binaries' in that it's not an arbitrary blob of machine code fed right into the cpu. it's still running in a sandbox (a la javascript) including similar limitations wrt CORS etc.

[+] ant6n|8 years ago|reply

[deleted]

[+] burke|8 years ago|reply
I get that we've lost this battle, and some people seem to think there's a useful distinction being made, but man, does the word 'transpile' grate on me.
[+] gatmne|8 years ago|reply
Agreed especially given that "transpile" lacks a well-defined meaning.

Just ask folks how this term differentiates from Compile and you'll get wildly varying answers and definitions.