Build-time Compiler
The build-time compiler translates Wasm functions to JVM bytecode during your build and stores them as .class files that you package in your application.
The compiler generates JVM bytecode without a separate verification pass. Only compile modules you trust. See Security Model.
Compared to the runtime compiler:
- nothing is compiled at instantiation
- no reflection and no ASM at runtime, so it works with
native-image - the module ships as a self-contained jar that needs only
run.endive:runtimeat runtime
Modules that use SIMD (v128) instructions cannot be compiled and must run in the interpreter.
Maven
Configure the plugin:
<build>
<plugins>
<plugin>
<groupId>run.endive</groupId>
<artifactId>endive-compiler-maven-plugin</artifactId>
<executions>
<execution>
<id>compiler-gen</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<!-- Translate the Wasm binary `add` into bytecode -->
<wasmFile>src/main/resources/add.wasm</wasmFile>
<!-- Name of the generated class to be used -->
<name>org.acme.wasm.Add</name>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then use the generated class as the machine factory:
import run.endive.runtime.Instance;
// load the bundled module
var module = Add.load();
// instantiate the module with the pre-compiled code
var instance = Instance.builder(module).
withMachineFactory(Add::create).
build();
Parameters
The ones you are likely to set:
| Parameter | Description |
|---|---|
wasmFile | The Wasm module to compile. Required. |
name | Fully qualified name of the generated class. Required. |
moduleInterface | Fully qualified name for which to generate typed _ModuleExports and _ModuleImports wrappers. See below. |
interpreterFallback | What to do with a function too large for a JVM method: FAIL (default), WARN or SILENT. With WARN or SILENT the function runs in the interpreter. |
interpretedFunctions | Comma-separated function indexes to interpret instead of compile. |
methodPrefixer | Set to run.endive.compiler.NameSectionMethodPrefixer to name the compiled methods after the module's name section, so stack traces and profiler output show the Wasm function names. Any MethodPrefixer with a public no-argument constructor on the plugin's classpath is accepted. |
methodSplitting | Set to true to split functions too large for the JVM to JIT-compile into several smaller methods. Experimental, see Method splitting. |
Print the full list with:
mvn help:describe -DgroupId=run.endive -DartifactId=endive-compiler-maven-plugin -Ddetail
Generating module exports and imports
The build-time compiler can also generate typed Java wrappers for a module's exports and imports,
eliminating the need for the @WasmModuleInterface annotation and the annotation processor setup.
Add the moduleInterface parameter to the plugin configuration:
<configuration>
<wasmFile>src/main/resources/demo.wasm</wasmFile>
<name>org.acme.wasm.DemoModule</name>
<moduleInterface>org.acme.wasm.Demo</moduleInterface>
</configuration>
This generates Demo_ModuleExports and Demo_ModuleImports classes alongside the compiled module.
You can then use them directly in your code without any annotation:
var instance = Instance.builder(DemoModule.load()).
withMachineFactory(DemoModule::create).
build();
var exports = new Demo_ModuleExports(instance);
IDE shortcomings
In some IDEs the sources generated under the standard folder target/generated-sources are not automatically recognized.
To overcome this limitation you can use an additional Maven Plugin for a smoother IDE experience:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>addSource</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/endive-compiler</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Gradle [community]
Gradle users can leverage the wasm2class-gradle-plugin, a third-party plugin that serves as an alternative to the Maven plugin, running the build-time compiler and enabling the use of pre-compiled Wasm code in Java, Kotlin, and Android projects.
To set it up, make sure MavenCentral is listed as a repository in the pluginManagement block of your settings.gradle.kts:
pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}
}
Configuration example in the build.gradle.kts file for the module:
plugins {
id("at.released.wasm2class.plugin") version "<latest version>"
}
wasm2class {
modules {
// Target package for the generated classes
targetPackage = "org.acme.wasm"
// Use "Add" as the base name for generated classes
create("Add") {
// Translate `add.wasm` into bytecode
wasm = file("src/main/resources/add.wasm")
}
}
}
This generates the class org.acme.wasm.Add, which you can use to instantiate the module just like shown earlier
in the Maven example.