2013-12-12 03:18:15 -08:00
|
|
|
TeaVM
|
|
|
|
=====
|
|
|
|
|
|
|
|
What is TeaVM?
|
2013-12-12 03:35:18 -08:00
|
|
|
--------------
|
2013-12-12 03:18:15 -08:00
|
|
|
|
|
|
|
In short, TeaVM gets a bytecode, running over JVM, and translates it to the JavaScript code,
|
|
|
|
which does exactly the same thing as the original bytecode does.
|
|
|
|
It is based on its cross-compiler which transforms `class` files to JavaScript.
|
|
|
|
But there is something more:
|
|
|
|
|
|
|
|
* a sophisticated per-method dependency manager, which greatly reduces the JavaScript output;
|
|
|
|
* an optimizer capable of things like devirtualization, inlining, constant propagation,
|
|
|
|
loop invariant motion and many other;
|
|
|
|
* implementation of subset of core Java library;
|
|
|
|
|
2013-12-18 04:58:49 -08:00
|
|
|
How to use
|
|
|
|
----------
|
|
|
|
|
2014-02-17 02:20:19 -08:00
|
|
|
There is no TeaVM artifacts in the central Maven repository yet.
|
|
|
|
So first you need to clone project and install it into the local repository.
|
|
|
|
In order to install project, just run `mvn install` when you are in the project's root directory.
|
|
|
|
|
2013-12-19 00:19:58 -08:00
|
|
|
There are several options of using TeaVM. One is the maven build. First, you write your code as if it were an
|
2013-12-18 04:58:49 -08:00
|
|
|
ordinary Java project:
|
|
|
|
|
2013-12-19 02:14:34 -08:00
|
|
|
```Java
|
|
|
|
package org.teavm.samples;
|
2013-12-18 04:58:49 -08:00
|
|
|
|
2013-12-19 02:14:34 -08:00
|
|
|
public class HelloWorld {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
System.out.println("Hello, world!");
|
2013-12-18 04:58:49 -08:00
|
|
|
}
|
2013-12-19 02:14:34 -08:00
|
|
|
}
|
|
|
|
```
|
2013-12-18 04:58:49 -08:00
|
|
|
|
2013-12-19 00:19:58 -08:00
|
|
|
Second, you include the following plugin in your `pom.xml` build section:
|
2013-12-18 04:58:49 -08:00
|
|
|
|
2013-12-19 02:14:34 -08:00
|
|
|
```XML
|
2013-12-19 00:19:58 -08:00
|
|
|
<plugin>
|
|
|
|
<groupId>org.teavm</groupId>
|
|
|
|
<artifactId>teavm-maven-plugin</artifactId>
|
2014-03-19 06:50:03 -07:00
|
|
|
<version>0.1</version>
|
2014-02-17 02:20:19 -08:00
|
|
|
<dependencies>
|
|
|
|
<dependency>
|
|
|
|
<groupId>org.teavm</groupId>
|
|
|
|
<artifactId>teavm-classlib</artifactId>
|
2014-03-19 06:50:03 -07:00
|
|
|
<version>0.1</version>
|
2014-02-17 02:20:19 -08:00
|
|
|
</dependency>
|
|
|
|
</dependencies>
|
2013-12-18 04:58:49 -08:00
|
|
|
<executions>
|
|
|
|
<execution>
|
|
|
|
<id>generate-javascript</id>
|
|
|
|
<goals>
|
|
|
|
<goal>build-javascript</goal>
|
|
|
|
</goals>
|
|
|
|
<phase>process-classes</phase>
|
|
|
|
<configuration>
|
2013-12-18 05:34:49 -08:00
|
|
|
<minifying>true</minifying>
|
2013-12-18 04:58:49 -08:00
|
|
|
<mainClass>org.teavm.samples.HelloWorld</mainClass>
|
2013-12-18 05:34:49 -08:00
|
|
|
<mainPageIncluded>true</mainPageIncluded>
|
2013-12-18 04:58:49 -08:00
|
|
|
</configuration>
|
|
|
|
</execution>
|
|
|
|
</executions>
|
|
|
|
</plugin>
|
2013-12-19 02:14:34 -08:00
|
|
|
```
|
2013-12-18 04:58:49 -08:00
|
|
|
|
2013-12-18 05:34:49 -08:00
|
|
|
Now you can execute `mvn clean package` and get the generated JavaScript files in `target/javascript` folder.
|
|
|
|
Just open `target/javascript/main.html` page in your browser, open developer's console and press *Refresh* and
|
|
|
|
see what happen.
|
2013-12-18 04:58:49 -08:00
|
|
|
|
2013-12-19 02:14:34 -08:00
|
|
|
There is [teavm-samples](teavm-samples) module,
|
2013-12-19 00:19:58 -08:00
|
|
|
containing a complete buildable and runnable example.
|
|
|
|
|
2013-12-12 03:18:15 -08:00
|
|
|
Advantages over GWT
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
You may notice that TeaVM idea is much similar to GWT. So why we need TeaVM instead of GWT?
|
|
|
|
|
|
|
|
Unlinke GWT, TeaVM gets the compiled bytecode, not Java sources.
|
|
|
|
Thereby it **does not depend on a specific language syntax**. Even not on a specific language.
|
|
|
|
So, when the next Java version gets a new feature, you can use it in your source code
|
|
|
|
and TeaVM compiler remains unbroken. Also you may want thigs Scala, Kotlin or Ceilon. TeaVM supports them.
|
|
|
|
|
|
|
|
To represent a source code, GWT uses abstract syntax trees (AST).
|
|
|
|
TeaVM uses control flow graph (CFG) of methods. CFG are much easier to optimize,
|
|
|
|
so TeaVM **applies aggressive optimizations** to you code to make it running faster.
|
|
|
|
|
2013-12-19 00:19:58 -08:00
|
|
|
TeaVM compiler is faster. And TeaVM does not produce permutations.
|
|
|
|
So with TeaVM you have no permutation explosion problem.
|
|
|
|
|
2013-12-12 03:18:15 -08:00
|
|
|
Advantages over JavaScript
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
JavaScript suffers of its dynamic typing. When you write a new code, dynamic typing accelerates
|
|
|
|
the development process, allowing you to write less boilerplate code.
|
|
|
|
But when you are to maintain a large code base, you may need static typing.
|
|
|
|
Also, it is not dynamic typing that really makes code short.
|
|
|
|
Good static typed languages can [infer variable types for you](http://en.wikipedia.org/wiki/Type_inference).
|
|
|
|
And they usually have a lot more useful features like [lambda functions](http://en.wikipedia.org/wiki/Lambda_function),
|
|
|
|
[lexical closures](http://en.wikipedia.org/wiki/Closure_%28computer_science%29),
|
|
|
|
[implicit type casting](http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion), etc.
|
2013-12-12 03:35:18 -08:00
|
|
|
|
|
|
|
With JavaScript you sometimes have to include large library for only one feature. Or you include many different
|
|
|
|
libraries for different purposes and your project size grows. TeaVM translates only the methods which
|
|
|
|
are really needed. So you can depend on as much libraries as you want and get
|
|
|
|
|
|
|
|
With JavaScript you are limited to one language. TeaVM supports many of the JVM languages.
|
|
|
|
|