JWebAssembly/README.md

107 lines
5.2 KiB
Markdown
Raw Normal View History

2017-03-19 11:54:29 +01:00
JWebAssembly
======
2017-03-20 22:07:39 +01:00
[![Build Status](https://travis-ci.org/i-net-software/JWebAssembly.svg)](https://travis-ci.org/i-net-software/JWebAssembly)
2017-03-20 20:19:47 +01:00
[![License](https://img.shields.io/github/license/i-net-software/jwebassembly.svg)](https://github.com/i-net-software/jwebassembly/blob/master/LICENSE.txt)
2018-05-20 12:51:03 +02:00
[![Coverage Status](https://coveralls.io/repos/github/i-net-software/JWebAssembly/badge.svg?branch=master)](https://coveralls.io/github/i-net-software/JWebAssembly?branch=master)
2017-03-19 11:54:29 +01:00
2019-03-23 16:03:59 +01:00
JWebAssembly is a Java bytecode to [WebAssembly](http://webassembly.org/) 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.
2019-03-13 22:06:44 +01:00
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.
2017-03-19 11:54:29 +01:00
2019-01-26 19:36:48 +01:00
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.
2017-03-19 11:54:29 +01:00
Status of the project
----
2019-01-26 19:36:48 +01:00
The project is currently not production ready but you can run already some tests.
2018-03-28 20:09:35 +02:00
### Finished Components
* Java byte code parser
* test framework
* Public API of the Compiler
2018-10-07 19:04:04 +02:00
* [Gradle Plugin](https://github.com/i-net-software/JWebAssembly-Gradle)
2018-03-28 20:09:35 +02:00
### Partially Finished
2019-03-13 22:06:44 +01:00
* Binary format file writer and Text format file writer (191 of 201 Java byte code instructions)
2018-08-11 16:29:16 +02:00
* Support for native methods [#2](https://github.com/i-net-software/JWebAssembly/issues/2)
2018-03-31 19:26:22 +02:00
* Exception handling - required the next version of WebAssembly
* Multiple threads - required the next version of WebAssembly
2018-10-07 19:04:04 +02:00
* Memory Management - required the next version of WebAssembly with GC
2017-03-19 11:54:29 +01:00
2019-01-13 11:33:36 +01:00
### Open Features
* Optimizer - Optimize the WASM output of a single method after transpiling before writing to output
* 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 |
| ----------------------- | ------ | ------------ | ------ |
2019-04-03 08:53:11 -04:00
| [float-to-int][1] | yes | yes | yes |
| [Sign-extension][2] | yes | yes | yes |
| [Multi-value][3] | yes | - | yes |
| [Reference Types][4] | yes | yes | - |
| [Garbage collection][5] | - | partly | - |
| [Exceptions][6] | partly | - | partly |
- For V8 it based on the [V8 - node.js integrations builds](https://ci.chromium.org/p/v8/builders/luci.v8.ci/V8%20Linux64%20-%20node.js%20integration).
- For SpiderMonkey it based on the nightly build of [jsshell](https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central/).
- For WABT it based on [libwabt.js](https://github.com/WebAssembly/wabt/blob/master/demo/libwabt.js) via node module wabt@nightly.
To use it also some flags and switches are needed.
2017-03-19 11:54:29 +01:00
Required Java Version
----
2019-01-26 19:36:48 +01:00
The JWebAssembly compiler requires Java SE 8 or higher. It is tested with Java SE 8 on [travis-ci.org](https://travis-ci.org/i-net-software/jwebassembly).
2017-03-19 11:54:29 +01:00
2017-04-11 21:40:53 +02:00
## Usage
2017-03-19 11:54:29 +01:00
2017-04-11 21:40:53 +02:00
### Exporting functions
2019-01-26 19:36:48 +01:00
To export a Java function to make it accessible from JavaScript, you must add the annotation de.inetsoftware.jwebassembly.api.annotation.Export.
2017-04-11 21:40:53 +02:00
```java
2018-07-09 17:16:57 +02:00
import de.inetsoftware.jwebassembly.api.annotation.Export;
2017-04-11 21:40:53 +02:00
@Export
public static int add( int a, int b ) {
return a + b;
}
```
2018-07-09 17:16:57 +02:00
### importing functions
2019-01-26 19:36:48 +01:00
To import a JavaScript function to make it accessible from Java, you must add the annotation de.inetsoftware.jwebassembly.api.annotation.Import.
2018-07-09 17:16:57 +02:00
The method can be declared native or can have a Java implementation which will be ignored on compiling.
```java
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 );
}
```
2017-04-11 21:40:53 +02:00
### Java Limits
2017-04-14 11:06:57 +02:00
In version 1 of WebAssembly you can only compile:
2017-04-11 21:40:53 +02:00
* static methods
* use the data types int, long float and double
2017-04-14 11:06:57 +02:00
2019-01-26 19:36:48 +01:00
This is state of JWebAssembly 0.1.
2017-04-14 11:06:57 +02:00
### Alternatives
* [TeaVM](https://github.com/konsoletyper/teavm)
2018-05-06 11:39:50 +02:00
## For Tool Developer
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](https://github.com/i-net-software/JWebAssembly/tree/master/src/de/inetsoftware/jwebassembly) and [de.inetsoftware.classparser](https://github.com/i-net-software/JWebAssembly/tree/master/src/de/inetsoftware/classparser) and its subpackages.
* Create an instance of [de.inetsoftware.jwebassembly.JWebAssembly](https://github.com/i-net-software/JWebAssembly/blob/master/src/de/inetsoftware/jwebassembly/JWebAssembly.java) class and use its API.
[1]: https://github.com/WebAssembly/nontrapping-float-to-int-conversions
[2]: https://github.com/WebAssembly/sign-extension-ops
[3]: https://github.com/WebAssembly/multi-value
[4]: https://github.com/WebAssembly/reference-types
[5]: https://github.com/webassembly/gc
[6]: https://github.com/WebAssembly/exception-handling