Fork of TeaVM for compiling the EaglercraftX 1.8 WASM GC runtime, and any clients built on it
Go to file
2014-09-15 12:13:27 +04:00
teavm-chrome-rdp Refactor Eclipse plugin 2014-09-13 16:45:04 +04:00
teavm-classlib Fix minor issues 2014-09-11 17:12:53 +04:00
teavm-cli Fixes incremental build when running through CLI 2014-09-10 21:23:22 +04:00
teavm-core Add a minimal TeaVM builder that simply works 2014-09-15 12:13:27 +04:00
teavm-dom Refactoring and bugfixes 2014-08-30 23:29:24 +04:00
teavm-eclipse Add a minimal TeaVM builder that simply works 2014-09-15 12:13:27 +04:00
teavm-html4j Fix minor issues 2014-09-11 17:12:53 +04:00
teavm-jso Fixes problems with reading and writing a program 2014-09-09 12:18:46 +04:00
teavm-maven-plugin Add a minimal TeaVM builder that simply works 2014-09-15 12:13:27 +04:00
teavm-platform Refactor dependency checker 2014-09-03 21:08:19 +04:00
teavm-samples Adds support of incremental build into tools 2014-09-10 21:12:11 +04:00
teavm-scala-samples Switches to 0.2-SNAPSHOT version 2014-03-19 01:48:43 +04:00
.gitignore Adds root /target directory to .gitignore 2014-03-20 10:59:22 +04:00
checkstyle.xml Refactoring and bugfixes 2014-08-30 23:29:24 +04:00
LICENSE Applies apache license 2013-12-20 12:45:28 +04:00
license-regexp.txt Refactoring and bugfixes 2014-08-30 23:29:24 +04:00
NOTICE Applies apache license 2013-12-20 12:45:28 +04:00
pom.xml Fix minor issues 2014-09-11 17:12:53 +04:00
README.md Update README.md 2014-05-05 13:02:13 +04:00

TeaVM

What is TeaVM?

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;

How to use

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.

There are several options of using TeaVM. One is the maven build. First, you write your code as if it were an ordinary Java project:

package org.teavm.samples;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

Second, you include the following plugin in your pom.xml build section:

    <plugin>
      <groupId>org.teavm</groupId>
      <artifactId>teavm-maven-plugin</artifactId>
      <version>0.1</version>
      <dependencies>
        <dependency>
          <groupId>org.teavm</groupId>
          <artifactId>teavm-classlib</artifactId>
          <version>0.1</version>
        </dependency>
      </dependencies>
      <executions>
        <execution>
          <id>generate-javascript</id>
          <goals>
            <goal>build-javascript</goal>
          </goals>
          <phase>process-classes</phase>
          <configuration>
            <minifying>true</minifying>
            <mainClass>org.teavm.samples.HelloWorld</mainClass>
            <mainPageIncluded>true</mainPageIncluded>
          </configuration>
        </execution>
      </executions>
    </plugin>

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.

There is teavm-samples module, containing a complete buildable and runnable example.

DukeScript

DukeScript is a set of APIs that allows Java applications easily talk to JavaScript environment to (usually) animate an HTML page. While DukeScript has its own implementation of JVM, called Bck2Brwsr, TeaVM also provides support for running DukeScript applications, using teavm-html4j plugin.

Live examples

Thanks to Jaroslav Tulach, author of DukeScript, we have several DukeScript example applications. One is the minesweeper game. You can try its TeaVM-compiled version here, and then take a look at source code and HTML page.

Another example is avaialble here. It uses GraphHopper to build route in browser. Unlike original GraphHopper example it works completely in browser instead of querying server. Thanks to Peter Karich.

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.

TeaVM compiler is faster. And TeaVM does not produce permutations. So with TeaVM you have no permutation explosion problem.

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. And they usually have a lot more useful features like lambda functions, lexical closures, implicit type casting, etc.

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.