0

我试图以最基本的方式弄清楚如何使用 TeaVM 从 .java 文件编译 wasm 文件。如果我使用 TeaVM 入门页面上提供的 maven .pom 文件,我的 .java 代码相当简单并且可以成功编译为 Javascript:

package org.teavm;

public class Scorer {
    public static void main(String[] args) {
        Double score = testScore();
        
        //log(score);
    }
    
    //@JSBody(params = { "value" }, script = "console.log('Value is: ' + value)")
    //public static native void log(double value);
    
    public static double testScore(){
        Double score = 8.0085;
        return score;
    }
}

当我想做同样的事情,但将其编译为 .wasm 而不是 .js 时,我的问题就出现了。(这就是为什么从 .java 代码中注释掉 JSO 内容的原因)。我的 .pom 文件如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.teavm</groupId>
  <artifactId>teavm-maven-webapp</artifactId>
  <version>0.6.1 </version>
  <packaging>war</packaging>

  <properties>
    <java.version>1.8</java.version>
    <teavm.version>0.6.1</teavm.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <!-- Emulator of Java class library for TeaVM -->
    <dependency>
      <groupId>org.teavm</groupId>
      <artifactId>teavm-classlib</artifactId>
      <version>${teavm.version}</version>
      <scope>provided</scope>
    </dependency>

    <!-- JavaScriptObjects (JSO) - a JavaScript binding for TeaVM -->
    <dependency>
      <groupId>org.teavm</groupId>
      <artifactId>teavm-jso-apis</artifactId>
      <version>${teavm.version}</version>
      <scope>provided</scope>
    </dependency>

    <!-- Servlet 3.1 specification -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!-- Configure Java compiler to use Java 8 syntax  -->
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
        </configuration>
      </plugin>

      <!-- Configure WAR plugin to include JavaScript !AND WASM! files generated by TeaVM -->
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <webResources>
            <resource>
              <directory>${project.build.directory}/generated/js</directory>
            </resource>
            <resource>
              <directory>${project.build.directory}/generated/wasm</directory>
            </resource>
          </webResources>
        </configuration>
      </plugin>

      <!-- Configure TeaVM -->
      <plugin>
        <groupId>org.teavm</groupId>
        <artifactId>teavm-maven-plugin</artifactId>
        <version>${teavm.version}</version>
        <executions>
          <execution>
            <id>web-client</id>
            <goals>
              <goal>compile</goal>
            </goals>
            <configuration>
              <!-- Directory where TeaVM should put generated files. This configuration conforms to the settings
                   of the WAR plugin -->
              <targetDirectory>${project.build.directory}/generated/js/teavm</targetDirectory>

              <!-- Main class, containing static void main(String[]) -->
              <mainClass>org.teavm.Scorer</mainClass>

              <!-- Whether TeaVM should produce minified JavaScript. Can reduce JavaScript file size more than
                   two times -->
              <minifying>false</minifying>

              <!-- Whether TeaVM should produce debug information for its built-in debugger -->
              <debugInformationGenerated>true</debugInformationGenerated>

              <!-- Whether TeaVM should produce source maps file -->
              <sourceMapsGenerated>true</sourceMapsGenerated>

              <!-- Whether TeaVM should also put source files into output directory,
                   for compatibility with source maps -->
              <sourceFilesCopied>true</sourceFilesCopied>

              <!-- Optimization level. Valid values are: SIMPLE, ADVANCED, FULL -->
              <optimizationLevel>FULL</optimizationLevel>
              
              <!-- <targetType>WEBASSEMBLY</targetType> -->
            </configuration>
          </execution>
          <execution>
            <id>wasm-client</id>
            <goals>
              <goal>compile</goal>
            </goals>
            <configuration>
              <!-- Directory where TeaVM should put generated files. This configuration conforms to the settings
                   of the WAR plugin -->
              <targetDirectory>${project.build.directory}/generated/wasm/teavm-wasm</targetDirectory>

              <!-- Main class, containing static void main(String[]) -->
              <mainClass>org.teavm.Scorer</mainClass>
              
              <!-- Whether TeaVM should produce debug information for its built-in debugger -->
              <debugInformationGenerated>true</debugInformationGenerated>

              
              
              <targetType>WEBASSEMBLY</targetType>
              
              <!-- Optimization level. Valid values are: SIMPLE, ADVANCED, FULL -->
              <optimizationLevel>FULL</optimizationLevel>
              
              <minHeapSize>1</minHeapSize>
              <maxHeapSize>16</maxHeapSize>
              
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

这基本上是“入门”.pom 文件和来自位于此处的基准示例的 .pom 文件的组合:https ://github.com/konsoletyper/teavm/blob/master/samples/benchmark/pom.xml

我将第二个执行块添加到teavm 插件配置部分。基准 .pom 中的所有其他内容看起来都与 .wasm 文件生成无关。

当我尝试构建时(使用mvn clean package,与我用于仅 js 版本的相同),我收到以下错误:

[INFO] Running TeaVM
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.876 s
[INFO] Finished at: 2022-02-24T18:43:21-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.teavm:teavm-maven-plugin:0.6.1:compile (wasm-client) on project teavm-maven-webapp: Unexpected error occurred: Array index out of range: 0 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

有谁知道这可能是什么原因造成的?我检查了错误日志中的 apache wiki 页面 url,它并没有真正帮助。我以前从未使用过 TeaVM 或 Maven,这让我有点难过。

4

0 回答 0