Skip to content

kawamuray/wasmtime-java

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
December 13, 2022 19:23
July 24, 2020 01:53
July 24, 2020 01:15
July 24, 2020 01:53
July 24, 2020 01:53
October 8, 2020 00:58
July 24, 2020 01:53

wasmtime-java

Java (or any JVM) language binding for Wasmtime.

Some basic examples are working, but many API implementations are work in progress.

Declaring Dependencies

Gradle example:

repositories {
    mavenCentral()
}

dependencies {
    implementation "io.github.kawamuray.wasmtime:wasmtime-java:$LATEST_VERSION"
}

An artifact (JAR) of wasmtime-java ships along with prebuilt JNI libraries for some major platforms, so just adding the above dependency provides you a self-contained wasmtime runtime on supported platforms:

OS Arch
Linux (ELF) x86_64
Mac OS x86_64
Windows x86_64

Example

See examples for the full example.

public class HelloWasm {
    public static void main(String[] args) {
        try (Store store = new Store();
             Engine engine = store.engine();
             Module module = Module.fromFile(engine, "./hello.wat");
             Func helloFunc = WasmFunctions.wrap(store, () -> {
                 System.err.println(">>> Calling back...");
                 System.err.println(">>> Hello World!");
             })) {
            Collection<Extern> imports = Arrays.asList(Extern.fromFunc(helloFunc));
            try (Instance instance = new Instance(store, module, imports)) {
                try (Func f = instance.getFunc("run").get()) {
                    WasmFunctions.Consumer0 fn = WasmFunctions.consumer(f);
                    fn.accept();
                }
            }
        }
    }
}

Run example:

$ ./gradlew -Pmain=examples.HelloWorld examples:run

How to build

$ ./gradlew build

License

Apache License Version 2.0