Skip to content

Commit 059a6a8

Browse files
committed
feat: update main code to call itk-wasm code
We call stdout-stderr-test.wasi.wasm's unnamed function.
1 parent 61eae3f commit 059a6a8

File tree

1 file changed

+25
-36
lines changed
  • packages/core/java/src/main/java/org/itk/wasm

1 file changed

+25
-36
lines changed

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

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,66 +19,55 @@
1919
*/
2020
package org.itk.wasm;
2121

22-
import io.github.kawamuray.wasmtime.Engine;
2322
import io.github.kawamuray.wasmtime.Extern;
2423
import io.github.kawamuray.wasmtime.Func;
2524
import io.github.kawamuray.wasmtime.Instance;
25+
import io.github.kawamuray.wasmtime.Linker;
26+
import io.github.kawamuray.wasmtime.Memory;
2627
import io.github.kawamuray.wasmtime.Module;
2728
import io.github.kawamuray.wasmtime.Store;
2829
import io.github.kawamuray.wasmtime.WasmFunctions;
30+
import io.github.kawamuray.wasmtime.WasmFunctions.Consumer0;
31+
import io.github.kawamuray.wasmtime.wasi.WasiCtx;
32+
import io.github.kawamuray.wasmtime.wasi.WasiCtxBuilder;
2933

3034
import java.io.ByteArrayOutputStream;
3135
import java.io.FileInputStream;
3236
import java.io.IOException;
3337
import java.io.InputStream;
3438
import java.util.Arrays;
3539
import java.util.Collection;
40+
import java.util.Optional;
3641

3742
public class Main {
3843
public static void main(String... args) throws IOException {
3944
// Configure the initial compilation environment, creating the global
4045
// `Store` structure. Note that you can also tweak configuration settings
4146
// with a `Config` and an `Engine` if desired.
4247
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);
48+
try (
49+
WasiCtx wasi = new WasiCtxBuilder().inheritStdout().inheritStderr().build();
50+
Store<Void> store = Store.withoutData(wasi);
51+
Linker linker = new Linker(store.engine());
52+
Module module = Module.fromBinary(store.engine(), readBytes("../python/itkwasm/test/input/stdout-stderr-test.wasi.wasm")))
53+
{
54+
// Here we handle the imports of the module, which in this case is our
55+
// `HelloCallback` type and its associated implementation of `Callback.
56+
System.err.println("Creating callback...");
6657

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-
}
58+
WasiCtx.addToLinker(linker);
59+
//linker.define("xyz", "poll_word", Extern.fromFunc(pollWordFn));
60+
String moduleName = "instance1";
61+
linker.module(store, moduleName, module);
62+
Extern extern = linker.get(store, moduleName, "").get();
63+
Consumer0 doWork = WasmFunctions.consumer(store, extern.func());
64+
doWork.accept();
7665
}
7766
}
7867

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)) {
68+
private static byte[] readBytes(String filename) throws IOException {
69+
//try (InputStream is = Main.class.getResourceAsStream(filename)) {
70+
try (InputStream is = new FileInputStream(filename)) {
8271
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
8372
int nRead;
8473
byte[] buf = new byte[16384];

0 commit comments

Comments
 (0)