
TeaVM – Ahead-of-time transpiler of Java bytecode to JavaScript or WebAssembly - entelechy
http://teavm.org/
======
kodablah
For the reverse, I wrote a WASM-to-JVM compiler:
[https://github.com/cretz/asmble](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
> 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.

~~~
sitkack
If only the JVM had included unsigned math.

~~~
pjmlp
Legend goes Gosling did not found anyone at his department that could answer
all his questions about unsigned arithmetic, hence why he left it out from
Java's initial design.

However since Java 8, the java.lang numeric classes do support unsigned
arithmetic.

Yes it is a bummer that byte is signed, requiring extra math to simulate
unsigned.

~~~
kodablah
Yup, I use those unsigned methods, e.g.
[https://github.com/cretz/asmble/blob/3cb439e887245f23bf876e8...](https://github.com/cretz/asmble/blob/3cb439e887245f23bf876e8be7c68aa2ef1c920f/compiler/src/main/kotlin/asmble/compile/jvm/FuncBuilder.kt#L247).
Luckily WASM only has 32/64 bit integer arithmetic.

------
taxreform
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.

~~~
kodablah
See the GC proposal:
[https://github.com/WebAssembly/gc/blob/master/proposals/gc/O...](https://github.com/WebAssembly/gc/blob/master/proposals/gc/Overview.md).
Here you get structs, arrays, and tuples (that can be used for multiple
returns). I am not disappointed that none of these features made the MVP
personally.

------
acqq
“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](http://teavm.org/docs/intro/overview.html)

------
billsix
That word,"Transpile". Lol. GCC transpiles C to assembly.

~~~
PyComfy
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 ..._ )

~~~
pjmlp
So classical UNIX C compilers are now transpilers, because they always did
text to text (C to Assembly), before forking to the assembler.

~~~
shakna
The first AIX C compiler called itself a "transcompiler" in it's manual, so
yes, they are.

~~~
pjmlp
Any digitalized version of it available online?

~~~
shakna
Not that I know of.

But you can see the term of the day, "translator", here[0][1], and it isn't
much of a leap to see how that evolved into translator-compiler, and then
shortened itself.

[0] XLT86, 8080 to 8086 Translator
[http://www.s100computers.com/Software%20Folder/Assembler%20C...](http://www.s100computers.com/Software%20Folder/Assembler%20Collection/Digital%20Research%20XLT86%20Manual.pdf)

[1] Pg. 47, Trans Z80 to 8086 Translator
[http://www.patersontech.com/dos/Docs/86_Dos_usr_03.pdf](http://www.patersontech.com/dos/Docs/86_Dos_usr_03.pdf)

------
olingern
> _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
Kotlin compiles to javascript already...why wouldn't you go that route?

~~~
konsoletyper
Kotlin/JS does not allow to reuse Java code and Java libraries. That's the
main difference.

------
rubyn00bie
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?

~~~
austincheney
If you are compiling to the JVM the output is probably nothing related to a
web app.

~~~
rubyn00bie
One isn't compiling to the JVM with this, they're compiling to WebAssembly.
And I think the point of this is to compile JVM code to WebAssembly, right? Am
I missing something even larger than that?!

Should someone want another VM to run their code in; how, good is this at
reducing the size of the std lib or the JVM itself? Can I depend on useful
packages with this, or will they immediately shit the bed? Will the payload be
something like 150mb?

~~~
austincheney
In that case it will be interesting to observe this in practice. My fear (and
prediction) is a lot of continued frustration and failure because the web
environment is a different context than the JVM target with many different
technical requirements. If the underlying technical differences in the
environments were so trivial in the first place then simply swapping languages
from Java to JavaScript wouldn't be so challenging.

------
ianlevesque
Does this support GC?

~~~
glibgil
[https://github.com/konsoletyper/teavm/blob/d882fd14a63cc1bd7...](https://github.com/konsoletyper/teavm/blob/d882fd14a63cc1bd76060b3abf34f85d3b013692/core/src/main/java/org/teavm/runtime/GC.java)

------
exception_e
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
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)

~~~
konsoletyper
As for 0.5.x version, see this: [http://teavm.org/jcl-
support/0.5.x/jcl.html](http://teavm.org/jcl-support/0.5.x/jcl.html)

And even more in 0.6.x (including Stream API)

------
dizon
In the end, how will this differ from a Java applet?

~~~
grzm
You don't need to have Java enabled in your browser.

~~~
dizon
The problem with that is you'll essentially re-download the entire JVM (and
libraries) on each page load. If you don't want to use a Java applet, you
would need to reimplement GC, JNI, and so on. Java didn't work on the web for
a reason.

~~~
C14L
The browser would cache that automatically. And in many cases, you'd probably
use a Service Worker to implement your own custom cache and "install" the
application locally in the browser, so that is usable offline and only version
updates are downloaded in the background.

~~~
oblio
Weren't applets also cached?

To be honest, I think that hate of applets comes from a time when 56kbps
download speeds were considered very high, the average CPU had 1 core at 200
Mhz and the average webpage weighed 20k and executed 5-10 lines of Javascript.

I haven't used applets in the time, but if the user experience would be
improved (nice looking UI, respecting modern UX conventions, etc.), I imagine
your average applet from circa 1999 would run circles around Gmail, for
example :)

~~~
mping
Not only that, swing applets are notoriously fugly. One reason electron is
successful is because it kinda looks like native. Swing Look and Feels all
seem alien.

~~~
oblio
Swing can be styled, it's that not a lot of people really bothered. Jetbrains
products use Swing and they're decent.

Not amazing, but decent.

------
simfoo
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
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.

------
amelius
But how does garbage collection work?

Is it of the stop-the-world kind?

~~~
MaxBarraclough
Looks like simple mark-and-sweep, yes.

[https://github.com/konsoletyper/teavm/blob/d882fd1/core/src/...](https://github.com/konsoletyper/teavm/blob/d882fd1/core/src/main/java/org/teavm/runtime/GC.java#L116)

~~~
BatFastard
So basically useless for browsers

~~~
konsoletyper
Why do you think so?

------
xg15
I think, by now "transpile" just means "compile, but in web development".

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

~~~
diegoperini
What is the difference between cross-compile and transpile? (Honest question,
no sarcasm)

~~~
Touche
Doesn't "cross-compile" mean to compile a binary meant to run on a different
platform than the host? Like, compiling a Linux binary on a Mac... If I'm
right then that's nothing like what "transpile" means.

~~~
progman
"Transpile" is an artificial buzzword, just meaning source-to-source
translation, from Pascal to C for instance.

Actually, there is no such thing as a "transpiler". There are only _compilers_
which are programs which translate from one regular language A into another
regular language B. It doesn't matter whether B is a high-level language or
low-level machine code.

~~~
Touche
Yes, it's another word for source-to-source translation, you are right. It is
not, however, cross-compilation, which is what I was responding to. So I'm not
sure why you replied to _me_ with this statement.

In any event, is it so bad to have another word for source-to-source
compilation? What is it that angers you about it?

~~~
progman
> is it so bad to have another word for source-to-source compilation?

If you compile C to Assembler then you compile from one code (C) to another
code (Asm, or machine code which is the compact form of Asm). What's the
difference to a "transpiler"?

Wikipedia states that the difference is just the (almost) equal level of
abstraction. I would call such a thing a high-level source-to-source compiler.
The word "transpiler" is uncertain and barely known, hence the people here who
ask what a transpiler is.

Cross-compilation is another thing since it means compiling on one platform
for another platform. In all three cases we deal with _compilers_ from one
regular language A into another regular language B.

------
frik
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.

~~~
abiox
> 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.

~~~
sitkack
And a sandbox so powerful that all the browser vendors just turned off
SharedArrayBuffer.

[https://www.chromium.org/Home/chromium-
security/ssca](https://www.chromium.org/Home/chromium-security/ssca)

[https://blog.mozilla.org/security/2018/01/03/mitigations-
lan...](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-
class-timing-attack/)

WASM is portable binaries.

~~~
abiox
well, i guess in the same way that javascript or anything else is, when there
are sandbox escapes. bugs are a thing.

