JWebAssembly/README.md

156 lines
8.8 KiB
Markdown
Raw Permalink Normal View History

2020-03-08 12:18:31 +01:00
# JWebAssembly
2017-03-19 11:54:29 +01:00
2023-05-01 13:33:30 +02:00
[![Build with Java 8](https://github.com/i-net-software/JWebAssembly/actions/workflows/build8.yml/badge.svg)](https://github.com/i-net-software/JWebAssembly/actions/workflows/build8.yml)
[![Build with Java 11](https://github.com/i-net-software/JWebAssembly/actions/workflows/build11.yml/badge.svg)](https://github.com/i-net-software/JWebAssembly/actions/workflows/build11.yml)
2023-02-13 21:42:41 +01:00
[![Travis Build Status](https://api.travis-ci.com/i-net-software/JWebAssembly.svg)](https://app.travis-ci.com/github/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)
2021-10-24 18:15:46 +02:00
[![Maven](https://img.shields.io/maven-central/v/de.inetsoftware/jwebassembly-compiler.svg)](https://mvnrepository.com/artifact/de.inetsoftware/jwebassembly-compiler)
[![JitPack](https://jitpack.io/v/i-net-software/jwebassembly.svg)](https://jitpack.io/#i-net-software/jwebassembly/master-SNAPSHOT)
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.
2021-11-16 12:25:15 +01:00
## Contribution
[Contributions](https://github.com/i-net-software/JWebAssembly/blob/master/CONTRIBUTING.md) are very welcome. This large project will not work without [contributions](https://github.com/i-net-software/JWebAssembly/blob/master/CONTRIBUTING.md).
2020-03-08 12:18:31 +01:00
## Documentation
2022-04-10 10:56:52 +02:00
The [documentation](https://github.com/i-net-software/JWebAssembly/wiki) can be found in the [wiki](https://github.com/i-net-software/JWebAssembly/wiki).
2019-12-31 13:44:55 +01:00
2020-03-08 12:18:31 +01:00
## Roadmap
2022-04-03 13:46:35 +02:00
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](https://wasm-feature-detect.surma.technology/).
2019-01-26 19:36:48 +01:00
2020-03-08 12:18:31 +01:00
### Version 1.0 (Milestone 1)
#### Desired Features
2019-10-14 21:50:32 +02:00
* [x] Java byte code parser
* [x] test framework
* [x] Public API of the Compiler see [class JWebAssembly](src/de/inetsoftware/jwebassembly/JWebAssembly.java)
* [x] [Gradle Plugin](https://github.com/i-net-software/JWebAssembly-Gradle)
2020-03-08 12:18:31 +01:00
* [x] [Emulator](https://github.com/i-net-software/JWebAssembly/wiki/Debugging)
* [x] Binary format file writer and Text format file writer
2019-10-14 21:50:32 +02:00
* [x] Support for native methods [#2](https://github.com/i-net-software/JWebAssembly/issues/2)
* [x] Memory Management - currently with a polyfill on JavaScript side
* [x] invoke static method calls
* [x] invoke instance method calls
2020-05-21 16:30:37 +02:00
* [x] invoke interface method calls
2021-05-30 20:40:02 +02:00
* [x] invoke dynamic method calls (lambdas)
2020-01-12 17:13:52 +01:00
* [x] invoke default method calls
2020-01-25 21:38:25 +01:00
* [x] String support
2020-03-21 10:03:11 +01:00
* [x] Simple Class object support
* [x] static constructors
* [x] Enum support
2019-10-14 21:50:32 +02:00
* [x] Optimizer - Optimize the WASM output of a single method after transpiling before writing to output
2019-12-31 13:12:34 +01:00
* [x] Hello World sample [(live)](https://i-net-software.github.io/JWebAssembly/samples/HelloWorld/HelloWorld.html), [(source code)](https://github.com/i-net-software/JWebAssembly/blob/master/docs/samples/HelloWorld/HelloWorld.java)
* [x] Collection framework compile
2019-01-13 11:33:36 +01:00
2020-03-08 12:18:31 +01:00
### Version 2.0 (Milestone 2)
#### Desired Features
* [ ] Full featured [library for accessing the DOM](https://github.com/i-net-software/JWebAssembly-API/tree/master/src/de/inetsoftware/jwebassembly/web).
2021-11-16 12:25:15 +01:00
* [ ] Embbedding of Resources (properties, images, etc.)
2020-03-08 12:18:31 +01:00
### Version 3.0 (Milestone 3)
#### Desired Features
* [ ] Exception handling - required the next version of WebAssembly
* [ ] Multiple threads - required the next version of WebAssembly
2021-12-28 02:30:17 +03:00
* [ ] Memory Management with built-in GC without JavaScript polyfill
2021-11-16 12:25:15 +01:00
* [ ] Reflection
* [ ] More optimize like tail calls, removing from asserts, inlining of functions, etc
2020-03-08 12:18:31 +01:00
#### Status of Required WebAssembly Features
2022-04-03 13:46:35 +02:00
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](https://wasm-feature-detect.surma.technology/).
2020-03-08 12:18:31 +01:00
| Feature | Importance | V8/Chrome | SpiderMonkey/FF | WABT |
| ----------------------- |----------- | --------- | --------------- | ------ |
| [Garbage collection][7] | medium | - | [partly][11] | - |
| [Exceptions][8] | low | partly | - | partly |
| [Threads][9] | low | yes | ? | yes |
| [Tail call][10] | very low | yes | ? | yes |
2022-04-03 13:46:35 +02:00
- 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 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.
2017-03-19 11:54:29 +01:00
Required Java Version
----
The JWebAssembly compiler requires Java SE 8 or higher. It is tested with OpenJDK 8 on [travis-ci.com](https://app.travis-ci.com/github/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 );
}
```
2021-11-16 12:25:15 +01:00
### Hello world sample in the browser with the DOM wrapper API
```java
@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 );
}
```
2018-07-09 17:16:57 +02:00
2017-04-11 21:40:53 +02:00
### Java Limits
2021-11-16 12:25:15 +01:00
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](https://github.com/i-net-software/JWebAssembly/blob/master/CONTRIBUTING.md) are wellcome.
2019-01-26 19:36:48 +01:00
2017-04-14 11:06:57 +02:00
### Alternatives
* [TeaVM](https://github.com/konsoletyper/teavm)
2020-04-26 20:42:06 +02:00
* [CheerpJ](https://www.leaningtech.com/pages/cheerpj.html)
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.
2019-11-14 20:29:20 +01:00
[1]: https://github.com/WebAssembly/mutable-global
[2]: https://github.com/WebAssembly/nontrapping-float-to-int-conversions
[3]: https://github.com/WebAssembly/sign-extension-ops
[4]: https://github.com/WebAssembly/reference-types
2019-11-14 20:29:20 +01:00
[5]: https://github.com/WebAssembly/JS-BigInt-integration
[6]: https://github.com/WebAssembly/multi-value
[7]: https://github.com/webassembly/gc
[8]: https://github.com/WebAssembly/exception-handling
[9]: https://github.com/WebAssembly/threads
[10]: https://github.com/webassembly/tail-call
[11]: https://github.com/lars-t-hansen/moz-gc-experiments