Skip to content

Commit 61eae3f

Browse files
committed
feat: update main class to run WASM code
Adapted from wasmtime-java example at: https://github.com/kawamuray/wasmtime-java/blob/v0.14.0/examples/src/main/java/examples/HelloWorld.java
1 parent 7f6ab63 commit 61eae3f

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

packages/core/java/src/main/java/org/itk/wasm/Main.java

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,73 @@
1919
*/
2020
package org.itk.wasm;
2121

22+
import io.github.kawamuray.wasmtime.Engine;
23+
import io.github.kawamuray.wasmtime.Extern;
24+
import io.github.kawamuray.wasmtime.Func;
25+
import io.github.kawamuray.wasmtime.Instance;
26+
import io.github.kawamuray.wasmtime.Module;
27+
import io.github.kawamuray.wasmtime.Store;
28+
import io.github.kawamuray.wasmtime.WasmFunctions;
29+
30+
import java.io.ByteArrayOutputStream;
31+
import java.io.FileInputStream;
32+
import java.io.IOException;
33+
import java.io.InputStream;
34+
import java.util.Arrays;
35+
import java.util.Collection;
36+
2237
public class Main {
23-
public static void main(String... args) {
24-
System.out.println("Hello world");
38+
public static void main(String... args) throws IOException {
39+
// Configure the initial compilation environment, creating the global
40+
// `Store` structure. Note that you can also tweak configuration settings
41+
// with a `Config` and an `Engine` if desired.
42+
System.err.println("Initializing...");
43+
try (Store<Void> store = Store.withoutData()) {
44+
// Compile the wasm binary into an in-memory instance of a `Module`.
45+
System.err.println("Compiling module...");
46+
try (Engine engine = store.engine();
47+
Module module = new Module(engine, readWAT("hello.wat")))
48+
{
49+
// Here we handle the imports of the module, which in this case is our
50+
// `HelloCallback` type and its associated implementation of `Callback.
51+
System.err.println("Creating callback...");
52+
try (Func helloFunc = WasmFunctions.wrap(store, () -> {
53+
System.err.println("CB!! Calling back...");
54+
System.err.println("CB!! > Hello World!");
55+
})) {
56+
// Once we've got that all set up we can then move to the instantiation
57+
// phase, pairing together a compiled module as well as a set of imports.
58+
// Note that this is where the wasm `start` function, if any, would run.
59+
System.err.println("Instantiating module...");
60+
Collection<Extern> imports = Arrays.asList(Extern.fromFunc(helloFunc));
61+
try (Instance instance = new Instance(store, module, imports)) {
62+
// Next we poke around a bit to extract the `run` function from the module.
63+
System.err.println("Extracting export...");
64+
try (Func f = instance.getFunc(store, "run").get()) {
65+
WasmFunctions.Consumer0 fn = WasmFunctions.consumer(store, f);
66+
67+
// And last but not least we can call it!
68+
System.err.println("Calling export...");
69+
fn.accept();
70+
71+
System.err.println("Done.");
72+
}
73+
}
74+
}
75+
}
76+
}
77+
}
78+
79+
private static byte[] readWAT(String filename) throws IOException {
80+
try (InputStream is = new FileInputStream("/home/curtis/code/kitware/itk-wasm/packages/core/java/src/main/resources/org/itk/wasm/" + filename)) {
81+
//try (InputStream is = Main.class.getResourceAsStream(filename)) {
82+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
83+
int nRead;
84+
byte[] buf = new byte[16384];
85+
while ((nRead = is.read(buf, 0, buf.length)) != -1) {
86+
buffer.write(buf, 0, nRead);
87+
}
88+
return buffer.toByteArray();
89+
}
2590
}
2691
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(module
2+
(func $hello (import "" "hello"))
3+
(func (export "run") (call $hello))
4+
)

0 commit comments

Comments
 (0)