JWebAssembly is a Java bytecode to WebAssembly compiler. It uses Java class files as input. That it can compile any language that compile to Java bytecode like Clojure, Groovy, JRuby, Jython, Kotlin and Scala. As output it generates the binary format (.wasm file) or the text format (.wat file). The target is to run Java natively in the browser with WebAssembly.
The difference to similar projects is that not a complete VM with GC and memory management should be ported. It's more like a 1: 1 conversion. The generated WebAssembly code is similar in size to the original Java class files.
Contributions are very welcome. This large project will not work without contributions.
The documentation can be found in the wiki.
The compiler is feature complete for milestone 1. There is a release candidate. Please test it and report bugs if the compiled code has the same behavior as Java.
Also the current browsers (Chrome, Edge, Firefox, Opera, ...) supports the needed features.
- Java byte code parser
- test framework
- Public API of the Compiler see class JWebAssembly
- Gradle Plugin
- Emulator
- Binary format file writer and Text format file writer
- Support for native methods #2
- Memory Management - currently with a polyfill on JavaScript side
- invoke static method calls
- invoke instance method calls
- invoke interface method calls
- invoke dynamic method calls (lambdas)
- invoke default method calls
- String support
- Simple Class object support
- static constructors
- Enum support
- Optimizer - Optimize the WASM output of a single method after transpiling before writing to output
- Hello World sample (live), (source code)
- Collection framework compile
- Full featured library for accessing the DOM.
- Embbedding of Resources (properties, images, etc.)
- Exception handling - required the next version of WebAssembly
- Multiple threads - required the next version of WebAssembly
- Memory Management with built-in GC without JavaScript polyfill
- Reflection
- More optimize like tail calls, removing from asserts, inlining of functions, etc
The following table shows the status of future WebAssembly features required by JWebAssembly in nightly builds in various implementations. These features are already used by the trunk version of JWebAssembly. If you want know the status of your current browser then look for your browser support.
Feature | Importance | V8/Chrome | SpiderMonkey/FF | WABT |
---|---|---|---|---|
Garbage collection | medium | - | partly | - |
Exceptions | low | partly | - | partly |
Threads | low | yes | ? | yes |
Tail call | very low | yes | ? | yes |
- For V8 it based on the V8 - node.js integrations builds.
- For SpiderMonkey it based on the nightly build of jsshell.
- For WABT it based on libwabt.js via node module wabt@nightly.
To use it also some flags and switches are currently needed.
Importance: All with high marked features are required for a hello word sample. For a first version that can be used for production.
The JWebAssembly compiler requires Java SE 8 or higher. It is tested with OpenJDK 8 on travis-ci.com.
To export a Java function to make it accessible from JavaScript, you must add the annotation de.inetsoftware.jwebassembly.api.annotation.Export.
import de.inetsoftware.jwebassembly.api.annotation.Export;
@Export
public static int add( int a, int b ) {
return a + b;
}
To import a JavaScript function to make it accessible from Java, you must add the annotation de.inetsoftware.jwebassembly.api.annotation.Import. The method can be declared native or can have a Java implementation which will be ignored on compiling.
import de.inetsoftware.jwebassembly.api.annotation.Import;
@Import( module = "global.Math", name = "max" )
static int max( int a, int b) {
return Math.max( a, b );
}
@Export
public static void main() {
Document document = Window.document();
HTMLElement div = document.createElement("div");
Text text = document.createTextNode("Hello World, this text come from WebAssembly.");
div.appendChild( text );
document.body().appendChild( div );
}
If you use it in the browser then only things can work that also work in the browser sandbox. This means file access or sockets will never work. Another problem are native parts of the Java VM. If there are no replacements via pure Java or JavaScript import then it will not work. Contributions are wellcome.
If you want to develop some tools like plugins for a build system or an IDE, then you need
- to include the full contents of the packages de.inetsoftware.jwebassembly and de.inetsoftware.classparser and its subpackages.
- Create an instance of de.inetsoftware.jwebassembly.JWebAssembly class and use its API.