Warning
This article was automatically translated by OpenAI (gpt-4o).It may be edited eventually, but please be aware that it may contain incorrect information at this time.
Chicory is a WebAssembly Runtime on the JVM.
It is implemented purely in Java, and can be used without native libraries or JNI.
Note
As a Wasm Runtime on the JVM using JNI, there is wasmtime-java.
By adding the following dependency, you can load and execute wasm on a Java application.
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>runtime</artifactId>
<version>0.0.10</version>
</dependency>
Create a sample wasm file from a WAT file. Create the following file SumSquared.wat.
It adds two i32 parameters and returns the squared value.
(module
(func (export "SumSquared")
(param $value_1 i32) (param $value_2 i32)
(result i32)
(local $sum i32)
(i32.add (local.get $value_1) (local.get $value_2))
local.set $sum
(i32.mul (local.get $sum) (local.get $sum))
)
)
Convert it to wasm using wat2wasm.
wat2wasm SumSquared.wat
SumSquared.wasm will be generated. Load and execute this wasm in Java.
package org.example;
import com.dylibso.chicory.runtime.ExportFunction;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.runtime.Module;
import com.dylibso.chicory.wasm.types.Value;
public class Main {
public static void main(String[] args) {
Module module = Module.builder("SumSquared.wasm").build();
Instance instance = module.instantiate();
ExportFunction iterFact = instance.export("SumSquared");
Value result = iterFact.apply(Value.i32(2), Value.i32(3))[0];
System.out.println("Result: " + result.asInt()); // should print "Result: 25" (= (2 + 3)^2)
}
}
Run the Java application.
$ mvn -q compile exec:java -Dexec.mainClass=org.example.Main
Result: 25
The full source code is here.
It seems to be a good way to incorporate a plugin mechanism into a Java application.