|
19 | 19 | */
|
20 | 20 | package org.itk.wasm;
|
21 | 21 |
|
22 |
| -import io.github.kawamuray.wasmtime.Engine; |
23 | 22 | import io.github.kawamuray.wasmtime.Extern;
|
24 | 23 | import io.github.kawamuray.wasmtime.Func;
|
25 | 24 | import io.github.kawamuray.wasmtime.Instance;
|
| 25 | +import io.github.kawamuray.wasmtime.Linker; |
| 26 | +import io.github.kawamuray.wasmtime.Memory; |
26 | 27 | import io.github.kawamuray.wasmtime.Module;
|
27 | 28 | import io.github.kawamuray.wasmtime.Store;
|
28 | 29 | 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; |
29 | 33 |
|
30 | 34 | import java.io.ByteArrayOutputStream;
|
31 | 35 | import java.io.FileInputStream;
|
32 | 36 | import java.io.IOException;
|
33 | 37 | import java.io.InputStream;
|
34 | 38 | import java.util.Arrays;
|
35 | 39 | import java.util.Collection;
|
| 40 | +import java.util.Optional; |
36 | 41 |
|
37 | 42 | public class Main {
|
38 | 43 | public static void main(String... args) throws IOException {
|
39 | 44 | // Configure the initial compilation environment, creating the global
|
40 | 45 | // `Store` structure. Note that you can also tweak configuration settings
|
41 | 46 | // with a `Config` and an `Engine` if desired.
|
42 | 47 | 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..."); |
66 | 57 |
|
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(); |
76 | 65 | }
|
77 | 66 | }
|
78 | 67 |
|
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)) { |
82 | 71 | ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
83 | 72 | int nRead;
|
84 | 73 | byte[] buf = new byte[16384];
|
|
0 commit comments