Async support is now opt-in
Coroutine transformation (the thing that lets Thread.sleep, Object.wait, @Async methods etc. suspend execution in JS and Wasm GC) is now disabled by default. It has a noticeable cost for the whole application, not only for the code that uses threads, while most applications don't need it at all.
When async support is disabled and a reachable method requires it, TeaVM reports a compilation error pointing to that method, together with the call stack that leads to it. synchronized blocks still work without async support.
If your application relies on threads or other blocking APIs, enable async support explicitly:
teavm.js {
asyncEnabled = true
}In Maven, use the asyncEnabled parameter (teavm.asyncEnabled property). In tests, annotate classes or methods that need threads with @AsyncEnabled.
See Coroutines for details and Troubleshooting for advice on how to find what pulls in async code.
New code generator for Wasm GC
Previously, Wasm GC backend reused the pipeline of JavaScript backend: methods were decompiled from TeaVM's intermediate representation to another intermediate representation, used by JS backend, which in turn was translated into Wasm. This used to work for older (pre-MVP) versions of Wasm back in 2016, but as Wasm became a stack machine, this approach did not allow to produce efficient code. A lot of time has passed and we finally got an opportunity to fix this gap. The new generator translates TeaVM's intermediate representation directly into Wasm instructions.
As a result, Wasm GC modules produced by TeaVM are now smaller and faster. It will also allow to easily add more features, like generation of full debug info for Wasm modules.
Bootstrapping
TeaVM has been able to compile itself for a while. Together with (slightly patched) javac, the compiler is built into a Wasm GC module that runs in the browser and turns Java source code into Wasm GC modules, with no server involved. This is done by the
teavm-javac project, which also powers our Playground.
Previously, the TeaVM-specific parts of this setup lived in teavm-javac. In this release, they were moved to the main TeaVM repository, and TeaVM now publishes the bootstrap compiler as an artifact, which teavm-javac consumes. This allowed to run TeaVM's own test suite against the bootstrap compiler (i.e. the part that translates bytecode to Wasm GC), which revealed and fixed a lot of issues.
This does not change anything for most users, but it's an important milestone: the ability to bootstrap is commonly considered an indicator of compiler maturity. Both javac and TeaVM are large codebases which make heavy use of collections, generics, lambdas and streams, so the fact that TeaVM compiles them correctly, and that the result passes TeaVM's own tests, shows that the compiler and the class library are capable of handling complex real-world code.
For now, the bootstrap compiler is available for Wasm GC backend only.
System.exit support
System.exit and Runtime.exit are now supported on all backends. After the call, no more Java code runs. In Node.js, process.exit is called automatically. In the browser, you can set a handler to learn that the module has terminated:
- in JavaScript, via
setExitHandlerof theteavmRuntimeobject, exported by the module, see JavaScript modules; - in Wasm GC, via
onExitoption of the loader, see Wasm GC loader.
Any subsequent call from JavaScript into the terminated module throws ExitError.
Assertion removal
New assertionsRemoved option (Gradle and Maven) strips assert statements from generated code. Classes and methods that are only reachable from assertions are not included in the output either, so this option is recommended for production builds.
JUnit 5 support
TeaVM test runner now supports JUnit Jupiter API (JUnit 5/6), including parameterized tests. Annotate a test class with @TeaVMTest to run it with TeaVM:
@TeaVMTest
class MyTest {
@Test
void works() {
assertEquals(4, 2 + 2);
}
}JUnit 4 is still supported. See Testing for details.
Extension point for transforming class files and resources
New ResourceTransformer extension point allows to patch class files (or any other resources) before TeaVM reads them. This is useful when a third-party library contains code that TeaVM can't handle. TeaVM does not parse class files for you, so use any bytecode library (like ASM) you prefer. See Compiler extensions for details.
Performance
TeaVM now has its own benchmarking tool, which runs JMH-style benchmarks compiled by TeaVM in JS, Wasm GC and C and compares the results with JVM. Also, a few benchmarks for the compiler itself were added. This made it possible to measure the improvements in this release, both in generated code and in the compiler. If you are interested in contributing, see Performance benchmarks.
Generated code:
- regular expressions are now matched by state machines where possible;
Pattern.compileandStringmethods likesplit,replaceAll,matcheswith a string literal regex are compiled at build time instead of parsing regex at run time;- faster
longarithmetic in JavaScript (now usesBigIntliterals;JSBodyscripts support them as well); - faster formatting and parsing of
double; - several
Mathmethods are now translated directly into native JS/Wasm/C operations; - faster
String.splitwith a single-char separator andString.replace(CharSequence, CharSequence); - fewer redundant runtime checks (array bounds,
null, division by zero).
Compiler itself became faster and consumes less memory.
See Performance tips on how to write code that benefits from these optimizations.
Class library
New APIs:
System.exitandRuntime.exitString.linesCharacter.toString(int)Math.fmaandStrictMath.fmaChecksum.update(byte[])andChecksum.update(ByteBuffer)- more
Collections.unmodifiable*methods (sequenced and sorted collections, sets and maps) ReferenceQueue.removein Wasm GC
Bug fixes
Object.wait/notify: fixed waking up of multiple threads and behaviour after timeout or interrupt.- Several issues with
WeakReferencein JS and Wasm GC. - Floating-point to integer conversion in JS and C backends; a lot of non-conformances in
Math(round,rint,ulp,hypot,scalb,nextAfter), as well as wrongFloat/Doubleconstants. - Class library:
String.isBlank,Formatterwith%nand%%,BitSet.toLongArray,ByteBuffer,FileInputStream.read,SimpleDateFormatwith numeric time zones,ConcurrentMapdefaults,TimeUnitconversions,Stream.distinct, lookups in empty immutable maps,System.outflushing in JS and Wasm GC. - JSO: every
@JSBodyImportis now respected, not just the first one; fixed unwrapping of Java objects passed through JS back to Java. - Several bugs in coroutine transformation (both C and Wasm GC) and in the optimizer.
- Alignment of heap objects in C backend on 32-bit targets.
- JUnit 4: inherited
@Before/@Aftermethods now run once per test.
Other improvements
- Compiler output is now reproducible (no longer depends on hash table iteration order).
- Gradle plugin is compatible with isolated projects.
- External compiler process (used by Gradle and Maven) no longer relies on RMI.
Contributors
- @ethanresnick
- @abanchev
- @SahilRajputdotexe
- @cstainton
- @Allsimon
- @huberty89
- @seo-rii
- @electrum
- @zufarfakhurtdinov
- @jvanzyl
Sponsors
A big thank you to Oki{ine and @shannah, for sponsoring the project as well as other sponsors (see full list). You can join these people and start supporting the project.