/JWebAssembly

Java to WebAssembly compiler

Primary LanguageJavaApache License 2.0Apache-2.0

JWebAssembly

Build Status License Coverage Status

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.

Status of the project

The project is currently not production ready but you can run already some tests.

Finished Components

  • Java byte code parser
  • test framework
  • Public API of the Compiler
  • Gradle Plugin

Partially Finished

  • Binary format file writer and Text format file writer (195 of 201 Java byte code instructions)
  • Support for native methods #2
  • Exception handling - required the next version of WebAssembly
  • Multiple threads - required the next version of WebAssembly
  • Memory Management - required the next version of WebAssembly with GC
  • Optimizer - Optimize the WASM output of a single method after transpiling before writing to output

Open Features

  • Library for accessing the DOM

Status of Required WebAssembly Features

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.

Feature V8 SpiderMonkey WABT
float-to-int yes yes yes
Sign-extension yes yes yes
Multi-value yes - yes
Reference Types yes yes yes
Garbage collection - partly -
Exceptions partly - partly

To use it also some flags and switches are needed.

Required Java Version

The JWebAssembly compiler requires Java SE 8 or higher. It is tested with Java SE 8 on travis-ci.org.

Usage

Exporting functions

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;
}

importing functions

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 );
}

Java Limits

In version 1 of WebAssembly you can only compile:

  • static methods
  • use the data types int, long float and double

This is state of JWebAssembly 0.1.

Alternatives

For Tool Developer

If you want to develop some tools like plugins for a build system or an IDE, then you need