|
| 1 | +package com.dylibso.chicory.tools.wasm; |
| 2 | + |
| 3 | +import static java.nio.file.Files.createDirectories; |
| 4 | + |
| 5 | +import com.dylibso.chicory.log.Logger; |
| 6 | +import com.dylibso.chicory.log.SystemLogger; |
| 7 | +import com.dylibso.chicory.runtime.ByteArrayMemory; |
| 8 | +import com.dylibso.chicory.runtime.ImportValues; |
| 9 | +import com.dylibso.chicory.runtime.Instance; |
| 10 | +import com.dylibso.chicory.wasi.WasiOptions; |
| 11 | +import com.dylibso.chicory.wasi.WasiPreview1; |
| 12 | +import com.dylibso.chicory.wasm.WasmModule; |
| 13 | +import io.roastedroot.zerofs.Configuration; |
| 14 | +import io.roastedroot.zerofs.ZeroFs; |
| 15 | +import java.io.File; |
| 16 | +import java.io.FileInputStream; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.UncheckedIOException; |
| 19 | +import java.nio.file.FileSystem; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.nio.file.StandardCopyOption; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +public final class Wast2Json { |
| 27 | + private static final Logger logger = |
| 28 | + new SystemLogger() { |
| 29 | + @Override |
| 30 | + public boolean isLoggable(Logger.Level level) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + }; |
| 34 | + private static final WasmModule MODULE = WasmToolsModule.load(); |
| 35 | + |
| 36 | + private final File input; |
| 37 | + private final File output; |
| 38 | + private final String[] options; |
| 39 | + |
| 40 | + private Wast2Json(File input, File output, String[] options) { |
| 41 | + this.input = input; |
| 42 | + this.output = output; |
| 43 | + this.options = options; |
| 44 | + } |
| 45 | + |
| 46 | + public static Builder builder() { |
| 47 | + return new Builder(); |
| 48 | + } |
| 49 | + |
| 50 | + public void process() { |
| 51 | + try (FileInputStream fis = new FileInputStream(input); |
| 52 | + FileSystem fs = |
| 53 | + ZeroFs.newFileSystem( |
| 54 | + Configuration.unix().toBuilder() |
| 55 | + .setAttributeViews("unix") |
| 56 | + .build())) { |
| 57 | + |
| 58 | + var wasiOpts = WasiOptions.builder(); |
| 59 | + |
| 60 | + wasiOpts.inheritSystem(); |
| 61 | + |
| 62 | + Path inputFolder = fs.getPath("input"); |
| 63 | + java.nio.file.Files.createDirectory(inputFolder); |
| 64 | + Path inputPath = inputFolder.resolve("spec.wast"); |
| 65 | + java.nio.file.Files.copy(fis, inputPath, StandardCopyOption.REPLACE_EXISTING); |
| 66 | + // copy(fis, inputPath, StandardCopyOption.REPLACE_EXISTING); |
| 67 | + wasiOpts.withDirectory(inputFolder.toString(), inputFolder); |
| 68 | + |
| 69 | + Path outputFolder = fs.getPath("output"); |
| 70 | + java.nio.file.Files.createDirectory(outputFolder); |
| 71 | + wasiOpts.withDirectory(outputFolder.toString(), outputFolder); |
| 72 | + java.nio.file.Files.createDirectory(outputFolder.resolve(output.getName())); |
| 73 | + |
| 74 | + List<String> args = new ArrayList<>(); |
| 75 | + args.add("wasm-tools"); |
| 76 | + args.add("json-from-wast"); |
| 77 | + args.add(inputPath.toString()); |
| 78 | + args.add("--wasm-dir"); |
| 79 | + args.add(outputFolder.resolve(output.getName()).toString()); |
| 80 | + args.add("--output"); |
| 81 | + args.add(outputFolder.resolve(output.getName()).resolve("spec.json").toString()); |
| 82 | + args.addAll(List.of(options)); |
| 83 | + logger.info("Running command: " + args.stream().collect(Collectors.joining(" "))); |
| 84 | + wasiOpts.withArguments(args); |
| 85 | + |
| 86 | + try (var wasi = |
| 87 | + WasiPreview1.builder() |
| 88 | + .withLogger(logger) |
| 89 | + .withOptions(wasiOpts.build()) |
| 90 | + .build()) { |
| 91 | + ImportValues imports = |
| 92 | + ImportValues.builder().addFunction(wasi.toHostFunctions()).build(); |
| 93 | + |
| 94 | + Instance.builder(MODULE) |
| 95 | + .withMachineFactory(WasmToolsModule::create) |
| 96 | + .withMemoryFactory(ByteArrayMemory::new) |
| 97 | + .withImportValues(imports) |
| 98 | + .build(); |
| 99 | + } |
| 100 | + |
| 101 | + createDirectories(output.toPath()); |
| 102 | + Files.copyDirectory(outputFolder.resolve(output.getName()), output.toPath()); |
| 103 | + } catch (IOException e) { |
| 104 | + throw new UncheckedIOException(e); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public static final class Builder { |
| 109 | + private File input; |
| 110 | + private File output; |
| 111 | + private String[] options = new String[0]; |
| 112 | + |
| 113 | + private Builder() {} |
| 114 | + |
| 115 | + public Builder withFile(File f) { |
| 116 | + this.input = f; |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + public Builder withOutput(File f) { |
| 121 | + this.output = f; |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + public Builder withOptions(String[] opts) { |
| 126 | + this.options = opts; |
| 127 | + return this; |
| 128 | + } |
| 129 | + |
| 130 | + public Wast2Json build() { |
| 131 | + return new Wast2Json(input, output, options); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments