Quick Start
This page goes from an empty project to calling a function exported by a Wasm module.
Endive requires Java 11 or later.
Install
Add the runtime to your project:
<dependency>
<groupId>run.endive</groupId>
<artifactId>runtime</artifactId>
<version>${endive.version}</version>
</dependency>
See Installation for Gradle and the BOM.
Load and instantiate a module
Download a test module that computes a factorial, from the link or with curl:
curl https://raw.githubusercontent.com/bytecodealliance/endive/main/wasm-corpus/src/main/resources/compiled/iterfact.wat.wasm > factorial.wasm
Parse it and create an instance:
import run.endive.runtime.ExportFunction;
import run.endive.wasm.types.Value;
import run.endive.wasm.Parser;
import run.endive.runtime.Instance;
import java.io.File;
// point this to your path on disk
var module = Parser.parse(new File("./factorial.wasm"));
Instance instance = Instance.builder(module).build();
The module is inert code; the instance is a virtual machine ready to execute it.
Instance and Store are not thread-safe. Create separate instances per thread, or synchronize access externally. Memory operations (used by the Wasm threads proposal) are thread-safe.
Call a function
The module exports a function called iterFact. Get a handle to it with Instance#export(String):
ExportFunction iterFact = instance.export("iterFact");
Call it with apply(). Arguments and results are raw longs, so map Java types yourself on both sides:
var result = iterFact.apply(5)[0];
System.out.println("Result: " + result); // should print 120 (5!)
Wasm functions can return multiple values, hence the array. This one returns a single value, so we take the first.