|
| 1 | +package org.itk.wasm; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | + |
| 6 | +import java.io.FileInputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.ByteBuffer; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +public class Pipeline { |
| 15 | + private Config config; |
| 16 | + private Engine engine; |
| 17 | + private Store store; |
| 18 | + private Linker linker; |
| 19 | + private Module module; |
| 20 | + private Memory memory; |
| 21 | + private Function inputArrayAlloc; |
| 22 | + private Function inputJsonAlloc; |
| 23 | + private Function outputArrayAddress; |
| 24 | + private Function outputArraySize; |
| 25 | + private Function outputJsonAddress; |
| 26 | + private Function outputJsonSize; |
| 27 | + |
| 28 | + public Pipeline(Path pipeline) throws IOException { |
| 29 | + config = new Config(); |
| 30 | + config.wasmBulkMemory(true); |
| 31 | + config.wasmSimd(true); |
| 32 | + config.wasmMemory64(true); |
| 33 | + engine = new Engine(config); |
| 34 | + store = new Store(engine); |
| 35 | + linker = new Linker(engine); |
| 36 | + linker.allowShadowing(true); |
| 37 | + |
| 38 | + byte[] wasmBytes = new byte[(int) pipeline.toFile().length()]; |
| 39 | + try (FileInputStream fis = new FileInputStream(pipeline.toFile())) { |
| 40 | + fis.read(wasmBytes); |
| 41 | + } |
| 42 | + |
| 43 | + module = Module.create(engine, wasmBytes); |
| 44 | + store.setWasi(WasiConfig.inheritStdin().inheritStdout().inheritStderr().build()); |
| 45 | + |
| 46 | + linker.defineWasi(); |
| 47 | + Instance instance = linker.instantiate(store, module); |
| 48 | + |
| 49 | + memory = instance.exports(store).getMemory("memory"); |
| 50 | + inputArrayAlloc = instance.exports(store).getFunction("itk_wasm_input_array_alloc"); |
| 51 | + inputJsonAlloc = instance.exports(store).getFunction("itk_wasm_input_json_alloc"); |
| 52 | + outputArrayAddress = instance.exports(store).getFunction("itk_wasm_output_array_address"); |
| 53 | + outputArraySize = instance.exports(store).getFunction("itk_wasm_output_array_size"); |
| 54 | + outputJsonAddress = instance.exports(store).getFunction("itk_wasm_output_json_address"); |
| 55 | + outputJsonSize = instance.exports(store).getFunction("itk_wasm_output_json_size"); |
| 56 | + |
| 57 | + Function initialize = instance.exports(store).getFunction("_initialize"); |
| 58 | + initialize.call(store); |
| 59 | + } |
| 60 | + |
| 61 | + public List<PipelineOutput> run(List<String> args, List<PipelineOutput> outputs, List<PipelineInput> inputs) { |
| 62 | + WasiConfig wasiConfig = WasiConfig.inheritEnv() |
| 63 | + .inheritStdin() |
| 64 | + .inheritStdout() |
| 65 | + .inheritStderr() |
| 66 | + .args(args) |
| 67 | + .build(); |
| 68 | + |
| 69 | + List<String> preopenDirectories = new ArrayList<>(); |
| 70 | + for (int index = 0; index < inputs.size(); index++) { |
| 71 | + PipelineInput input = inputs.get(index); |
| 72 | + if (input.getType() == InterfaceTypes.TextFile || input.getType() == InterfaceTypes.BinaryFile) { |
| 73 | + Path parentPath = input.getData().getPath().getParent(); |
| 74 | + if (parentPath != null) { |
| 75 | + preopenDirectories.add(parentPath.toString()); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + for (int index = 0; index < outputs.size(); index++) { |
| 80 | + PipelineOutput output = outputs.get(index); |
| 81 | + if (output.getType() == InterfaceTypes.TextFile || output.getType() == InterfaceTypes.BinaryFile) { |
| 82 | + Path parentPath = output.getData().getPath().getParent(); |
| 83 | + if (parentPath != null) { |
| 84 | + preopenDirectories.add(parentPath.toString()); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + String[] preopenDirectoriesArray = preopenDirectories.toArray(new String[0]); |
| 89 | + wasiConfig.setPreopenDirectories(preopenDirectoriesArray); |
| 90 | + store.set |
0 commit comments