diff --git a/README.md b/README.md index b88bd5221..0bbe55f39 100644 --- a/README.md +++ b/README.md @@ -251,6 +251,8 @@ public void println(String value) { import com.dylibso.chicory.runtime.HostFunction; import com.dylibso.chicory.wasm.types.ValueType; var func = new HostFunction( + "console", + "log", (Instance instance, Value... args) -> { // decompiled is: console_log(13, 0); var len = args[0].asInt(); var offset = args[1].asInt(); @@ -258,8 +260,6 @@ var func = new HostFunction( println(message); return null; }, - "console", - "log", List.of(ValueType.I32, ValueType.I32), List.of()); ``` @@ -277,9 +277,9 @@ Note that the HostFunction needs 3 things: Now we just need to pass this host function in during our instantiation phase: ```java -import com.dylibso.chicory.runtime.HostImports; -var imports = new HostImports(new HostFunction[] {func}); -var instance = Instance.builder(Parser.parse(new File("./logger.wasm"))).withHostImports(imports).build(); +import com.dylibso.chicory.runtime.ExternalValues; +var hostFunctions = new ExternalValues(new HostFunction[] {func}); +var instance = Instance.builder(Parser.parse(new File("./logger.wasm"))).withExternalValues(hostFunctions).build(); var logIt = instance.export("logIt"); logIt.apply(); // should print "Hello, World!" 10 times @@ -306,7 +306,7 @@ import com.dylibso.chicory.runtime.Store; // instantiate the store var store = new Store(); // registers `console.log` in the store (see the previous section for the definition of `func`) -store.addHostFunction(func); +store.addFunction(func); ``` However, the store also automatically exposes the exports of a module to the other instances that are registered. @@ -326,8 +326,9 @@ var logger2 = store.instantiate("logger2", Parser.parse(new File("./logger.wasm" This is equivalent to: ```java -var hostImports = store.toHostImports(); -var instance = Instance.builder(m).withHostImports(hostImports).build(); +var external = store.toExternalValues(); +var m = Parser.parse(new File("./logger.wasm")); +var instance = Instance.builder(m).withExternalValues(external).build(); store.register("logger2", instance); ``` diff --git a/aot-tests/src/test/java/com/dylibso/chicory/testing/Spectest.java b/aot-tests/src/test/java/com/dylibso/chicory/testing/Spectest.java index 8706a2dae..b0051059a 100644 --- a/aot-tests/src/test/java/com/dylibso/chicory/testing/Spectest.java +++ b/aot-tests/src/test/java/com/dylibso/chicory/testing/Spectest.java @@ -1,11 +1,11 @@ package com.dylibso.chicory.testing; +import com.dylibso.chicory.runtime.ExternalGlobal; +import com.dylibso.chicory.runtime.ExternalMemory; +import com.dylibso.chicory.runtime.ExternalTable; +import com.dylibso.chicory.runtime.ExternalValues; import com.dylibso.chicory.runtime.GlobalInstance; import com.dylibso.chicory.runtime.HostFunction; -import com.dylibso.chicory.runtime.HostGlobal; -import com.dylibso.chicory.runtime.HostImports; -import com.dylibso.chicory.runtime.HostMemory; -import com.dylibso.chicory.runtime.HostTable; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.runtime.Memory; import com.dylibso.chicory.runtime.TableInstance; @@ -23,52 +23,54 @@ public final class Spectest { private Spectest() {} - public static HostImports toHostImports() { - return new HostImports( + public static ExternalValues toExternalValues() { + return new ExternalValues( new HostFunction[] { - new HostFunction(noop, "spectest", "print", List.of(), List.of()), + new HostFunction("spectest", "print", noop, List.of(), List.of()), new HostFunction( - noop, "spectest", "print_i32", List.of(ValueType.I32), List.of()), + "spectest", "print_i32", noop, List.of(ValueType.I32), List.of()), new HostFunction( - noop, "spectest", "print_i32_1", List.of(ValueType.I32), List.of()), + "spectest", "print_i32_1", noop, List.of(ValueType.I32), List.of()), new HostFunction( - noop, "spectest", "print_i32_2", List.of(ValueType.I32), List.of()), + "spectest", "print_i32_2", noop, List.of(ValueType.I32), List.of()), new HostFunction( - noop, "spectest", "print_f32", List.of(ValueType.F32), List.of()), + "spectest", "print_f32", noop, List.of(ValueType.F32), List.of()), new HostFunction( - noop, "spectest", "print_i32_f32", + noop, List.of(ValueType.I32, ValueType.F32), List.of()), new HostFunction( - noop, "spectest", "print_i64", List.of(ValueType.I64), List.of()), + "spectest", "print_i64", noop, List.of(ValueType.I64), List.of()), new HostFunction( - noop, "spectest", "print_i64_1", List.of(ValueType.I64), List.of()), + "spectest", "print_i64_1", noop, List.of(ValueType.I64), List.of()), new HostFunction( - noop, "spectest", "print_i64_2", List.of(ValueType.I64), List.of()), + "spectest", "print_i64_2", noop, List.of(ValueType.I64), List.of()), new HostFunction( - noop, "spectest", "print_f64", List.of(ValueType.F64), List.of()), + "spectest", "print_f64", noop, List.of(ValueType.F64), List.of()), new HostFunction( - noop, "spectest", "print_f64_f64", + noop, List.of(ValueType.F64, ValueType.F64), List.of()) }, - new HostGlobal[] { - new HostGlobal("spectest", "global_i32", new GlobalInstance(Value.i32(666))), - new HostGlobal("spectest", "global_i64", new GlobalInstance(Value.i64(666))), - new HostGlobal( + new ExternalGlobal[] { + new ExternalGlobal( + "spectest", "global_i32", new GlobalInstance(Value.i32(666))), + new ExternalGlobal( + "spectest", "global_i64", new GlobalInstance(Value.i64(666))), + new ExternalGlobal( "spectest", "global_f32", new GlobalInstance(Value.fromFloat(666.6f))), - new HostGlobal( + new ExternalGlobal( "spectest", "global_f64", new GlobalInstance(Value.fromDouble(666.6))), }, - new HostMemory[] { - new HostMemory("spectest", "memory", new Memory(new MemoryLimits(1, 2))) + new ExternalMemory[] { + new ExternalMemory("spectest", "memory", new Memory(new MemoryLimits(1, 2))) }, - new HostTable[] { - new HostTable( + new ExternalTable[] { + new ExternalTable( "spectest", "table", new TableInstance(new Table(ValueType.FuncRef, new Limits(10, 20)))) diff --git a/aot-tests/src/test/java/com/dylibso/chicory/testing/TestModule.java b/aot-tests/src/test/java/com/dylibso/chicory/testing/TestModule.java index 410c0d3d6..b84be359c 100644 --- a/aot-tests/src/test/java/com/dylibso/chicory/testing/TestModule.java +++ b/aot-tests/src/test/java/com/dylibso/chicory/testing/TestModule.java @@ -1,7 +1,7 @@ package com.dylibso.chicory.testing; import com.dylibso.chicory.aot.AotMachine; -import com.dylibso.chicory.runtime.HostImports; +import com.dylibso.chicory.runtime.ExternalValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.runtime.Store; import com.dylibso.chicory.wabt.Wat2Wasm; @@ -62,9 +62,9 @@ public TestModule(Module module) { } public Instance instantiate(Store s) { - HostImports hostImports = s.toHostImports(); + ExternalValues externalValues = s.toExternalValues(); return Instance.builder(module) - .withHostImports(hostImports) + .withExternalValues(externalValues) .withMachineFactory(AotMachine::new) .build(); } diff --git a/aot/src/test/java/com/dylibso/chicory/approvals/ApprovalTest.java b/aot/src/test/java/com/dylibso/chicory/approvals/ApprovalTest.java index c755ade93..ba646b079 100644 --- a/aot/src/test/java/com/dylibso/chicory/approvals/ApprovalTest.java +++ b/aot/src/test/java/com/dylibso/chicory/approvals/ApprovalTest.java @@ -3,8 +3,8 @@ import static java.nio.charset.StandardCharsets.UTF_8; import com.dylibso.chicory.aot.AotMachine; +import com.dylibso.chicory.runtime.ExternalValues; import com.dylibso.chicory.runtime.HostFunction; -import com.dylibso.chicory.runtime.HostImports; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.types.ValueType; @@ -38,9 +38,9 @@ public void verifyHelloWasi() { verifyGeneratedBytecode( "hello-wasi.wat.wasm", new HostFunction( - (instance, args) -> null, "wasi_snapshot_preview1", "fd_write", + (instance, args) -> null, List.of(ValueType.I32, ValueType.I32, ValueType.I32, ValueType.I32), List.of(ValueType.I32))); } @@ -89,9 +89,9 @@ public void verifyStart() { verifyGeneratedBytecode( "start.wat.wasm", new HostFunction( - (instance, args) -> null, "env", "gotit", + (instance, args) -> null, List.of(ValueType.I32), List.of())); } @@ -107,7 +107,7 @@ private static void verifyGeneratedBytecode(String name, HostFunction... hostFun Parser.parse( ClassLoader.getSystemClassLoader() .getResourceAsStream("compiled/" + name))) - .withHostImports(new HostImports(hostFunctions)) + .withExternalValues(new ExternalValues(hostFunctions)) .withMachineFactory(AotMachine::new) .withStart(false) .build(); diff --git a/cli/src/main/java/com/dylibso/chicory/cli/Cli.java b/cli/src/main/java/com/dylibso/chicory/cli/Cli.java index 213a1eaaa..23ece7ef5 100644 --- a/cli/src/main/java/com/dylibso/chicory/cli/Cli.java +++ b/cli/src/main/java/com/dylibso/chicory/cli/Cli.java @@ -1,7 +1,7 @@ package com.dylibso.chicory.cli; import com.dylibso.chicory.log.SystemLogger; -import com.dylibso.chicory.runtime.HostImports; +import com.dylibso.chicory.runtime.ExternalValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.wasi.WasiOptions; import com.dylibso.chicory.wasi.WasiPreview1; @@ -60,17 +60,17 @@ public void run() { var module = Parser.parse(file); var imports = wasi - ? new HostImports( + ? new ExternalValues( new WasiPreview1( logger, WasiOptions.builder().inheritSystem().build()) .toHostFunctions()) - : new HostImports(); + : new ExternalValues(); var instance = Instance.builder(module) .withInitialize(true) .withStart(false) - .withHostImports(imports) + .withExternalValues(imports) .build(); if (functionName != null) { diff --git a/function-processor/src/main/java/com/dylibso/chicory/function/processor/FunctionProcessor.java b/function-processor/src/main/java/com/dylibso/chicory/function/processor/FunctionProcessor.java index 4ec9ac649..2ae71baa8 100644 --- a/function-processor/src/main/java/com/dylibso/chicory/function/processor/FunctionProcessor.java +++ b/function-processor/src/main/java/com/dylibso/chicory/function/processor/FunctionProcessor.java @@ -267,9 +267,9 @@ private Expression processMethod( var function = new ObjectCreationExpr() .setType("HostFunction") - .addArgument(handle) .addArgument(new StringLiteralExpr(moduleName)) .addArgument(new StringLiteralExpr(name)) + .addArgument(handle) .addArgument(new MethodCallExpr(new NameExpr("List"), "of", paramTypes)) .addArgument(new MethodCallExpr(new NameExpr("List"), "of", returnType)); // TODO: update javaparser and replace with multiline formatting diff --git a/function-processor/src/test/resources/BasicMathGenerated.java b/function-processor/src/test/resources/BasicMathGenerated.java index 86a8cb775..fef5380ff 100644 --- a/function-processor/src/test/resources/BasicMathGenerated.java +++ b/function-processor/src/test/resources/BasicMathGenerated.java @@ -15,32 +15,26 @@ private BasicMath_ModuleFactory() {} public static HostFunction[] toHostFunctions(BasicMath functions) { return new HostFunction[] { new HostFunction( - (Instance instance, Value... args) -> { + "math", "add", (Instance instance, Value... args) -> { long result = functions.add(args[0].asInt(), args[1].asInt()); return new Value[] { Value.i64(result) }; }, - "math", - "add", List.of(ValueType.I32, ValueType.I32), List.of(ValueType.I64) ), new HostFunction( - (Instance instance, Value... args) -> { + "math", "square", (Instance instance, Value... args) -> { double result = functions.pow2(args[0].asFloat()); return new Value[] { Value.fromDouble(result) }; }, - "math", - "square", List.of(ValueType.F32), List.of(ValueType.F64) ), new HostFunction( - (Instance instance, Value... args) -> { + "math", "floor_div", (Instance instance, Value... args) -> { int result = functions.floorDiv(args[0].asInt(), args[1].asInt()) return new Value[] { Value.i32(result) }; }, - "math", - "floor_div", List.of(ValueType.I32, ValueType.I32), List.of(ValueType.I32) ), diff --git a/function-processor/src/test/resources/NestedGenerated.java b/function-processor/src/test/resources/NestedGenerated.java index bb0804441..e8b2e9144 100644 --- a/function-processor/src/test/resources/NestedGenerated.java +++ b/function-processor/src/test/resources/NestedGenerated.java @@ -16,22 +16,18 @@ private Nested_ModuleFactory() {} public static HostFunction[] toHostFunctions(Nested functions) { return new HostFunction[] { new HostFunction( - (Instance instance, Value... args) -> { + "nested", "print", (Instance instance, Value... args) -> { functions.print(instance.memory(), args[0].asInt(), args[1].asInt()); return null; }, - "nested", - "print", List.of(ValueType.I32, ValueType.I32), List.of() ), new HostFunction( - (Instance instance, Value... args) -> { + "nested", "exit", (Instance instance, Value... args) -> { functions.exit(); return null; }, - "nested", - "exit", List.of(), List.of() ), diff --git a/function-processor/src/test/resources/NoPackageGenerated.java b/function-processor/src/test/resources/NoPackageGenerated.java index 1539a9f8d..2f2ef5495 100644 --- a/function-processor/src/test/resources/NoPackageGenerated.java +++ b/function-processor/src/test/resources/NoPackageGenerated.java @@ -13,22 +13,18 @@ private Nested_ModuleFactory() {} public static HostFunction[] toHostFunctions(NoPackage functions) { return new HostFunction[] { new HostFunction( - (Instance instance, Value... args) -> { + "nopackage", "print", (Instance instance, Value... args) -> { functions.print(instance.memory(), args[0].asInt(), args[1].asInt()); return null; }, - "nopackage", - "print", List.of(ValueType.I32, ValueType.I32), List.of() ), new HostFunction( - (Instance instance, Value... args) -> { + "nopackage", "exit", (Instance instance, Value... args) -> { functions.exit(); return null; }, - "nopackage", - "exit", List.of(), List.of() ), diff --git a/function-processor/src/test/resources/SimpleGenerated.java b/function-processor/src/test/resources/SimpleGenerated.java index 73f6fff71..a64ef8156 100644 --- a/function-processor/src/test/resources/SimpleGenerated.java +++ b/function-processor/src/test/resources/SimpleGenerated.java @@ -15,43 +15,35 @@ private Simple_ModuleFactory() {} public static HostFunction[] toHostFunctions(Simple functions) { return new HostFunction[] { new HostFunction( - (Instance instance, Value... args) -> { + "simple", "print", (Instance instance, Value... args) -> { functions.print( instance.memory().readString(args[0].asInt(), args[1].asInt())); return null; }, - "simple", - "print", List.of(ValueType.I32, ValueType.I32), List.of() ), new HostFunction( - (Instance instance, Value... args) -> { + "simple", "printx", (Instance instance, Value... args) -> { functions.printx(instance.memory().readCString(args[0].asInt())); return null; }, - "simple", - "printx", List.of(ValueType.I32), List.of() ), new HostFunction( - (Instance instance, Value... args) -> { + "simple", "random_get", (Instance instance, Value... args) -> { functions.randomGet(instance.memory(), args[0].asInt(), args[1].asInt()); return null; }, - "simple", - "random_get", List.of(ValueType.I32, ValueType.I32), List.of() ), new HostFunction( - (Instance instance, Value... args) -> { + "simple", "exit", (Instance instance, Value... args) -> { functions.exit(); return null; }, - "simple", - "exit", List.of(), List.of() ), diff --git a/runtime-tests/src/test/java/com/dylibso/chicory/testing/Spectest.java b/runtime-tests/src/test/java/com/dylibso/chicory/testing/Spectest.java index 8706a2dae..b0051059a 100644 --- a/runtime-tests/src/test/java/com/dylibso/chicory/testing/Spectest.java +++ b/runtime-tests/src/test/java/com/dylibso/chicory/testing/Spectest.java @@ -1,11 +1,11 @@ package com.dylibso.chicory.testing; +import com.dylibso.chicory.runtime.ExternalGlobal; +import com.dylibso.chicory.runtime.ExternalMemory; +import com.dylibso.chicory.runtime.ExternalTable; +import com.dylibso.chicory.runtime.ExternalValues; import com.dylibso.chicory.runtime.GlobalInstance; import com.dylibso.chicory.runtime.HostFunction; -import com.dylibso.chicory.runtime.HostGlobal; -import com.dylibso.chicory.runtime.HostImports; -import com.dylibso.chicory.runtime.HostMemory; -import com.dylibso.chicory.runtime.HostTable; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.runtime.Memory; import com.dylibso.chicory.runtime.TableInstance; @@ -23,52 +23,54 @@ public final class Spectest { private Spectest() {} - public static HostImports toHostImports() { - return new HostImports( + public static ExternalValues toExternalValues() { + return new ExternalValues( new HostFunction[] { - new HostFunction(noop, "spectest", "print", List.of(), List.of()), + new HostFunction("spectest", "print", noop, List.of(), List.of()), new HostFunction( - noop, "spectest", "print_i32", List.of(ValueType.I32), List.of()), + "spectest", "print_i32", noop, List.of(ValueType.I32), List.of()), new HostFunction( - noop, "spectest", "print_i32_1", List.of(ValueType.I32), List.of()), + "spectest", "print_i32_1", noop, List.of(ValueType.I32), List.of()), new HostFunction( - noop, "spectest", "print_i32_2", List.of(ValueType.I32), List.of()), + "spectest", "print_i32_2", noop, List.of(ValueType.I32), List.of()), new HostFunction( - noop, "spectest", "print_f32", List.of(ValueType.F32), List.of()), + "spectest", "print_f32", noop, List.of(ValueType.F32), List.of()), new HostFunction( - noop, "spectest", "print_i32_f32", + noop, List.of(ValueType.I32, ValueType.F32), List.of()), new HostFunction( - noop, "spectest", "print_i64", List.of(ValueType.I64), List.of()), + "spectest", "print_i64", noop, List.of(ValueType.I64), List.of()), new HostFunction( - noop, "spectest", "print_i64_1", List.of(ValueType.I64), List.of()), + "spectest", "print_i64_1", noop, List.of(ValueType.I64), List.of()), new HostFunction( - noop, "spectest", "print_i64_2", List.of(ValueType.I64), List.of()), + "spectest", "print_i64_2", noop, List.of(ValueType.I64), List.of()), new HostFunction( - noop, "spectest", "print_f64", List.of(ValueType.F64), List.of()), + "spectest", "print_f64", noop, List.of(ValueType.F64), List.of()), new HostFunction( - noop, "spectest", "print_f64_f64", + noop, List.of(ValueType.F64, ValueType.F64), List.of()) }, - new HostGlobal[] { - new HostGlobal("spectest", "global_i32", new GlobalInstance(Value.i32(666))), - new HostGlobal("spectest", "global_i64", new GlobalInstance(Value.i64(666))), - new HostGlobal( + new ExternalGlobal[] { + new ExternalGlobal( + "spectest", "global_i32", new GlobalInstance(Value.i32(666))), + new ExternalGlobal( + "spectest", "global_i64", new GlobalInstance(Value.i64(666))), + new ExternalGlobal( "spectest", "global_f32", new GlobalInstance(Value.fromFloat(666.6f))), - new HostGlobal( + new ExternalGlobal( "spectest", "global_f64", new GlobalInstance(Value.fromDouble(666.6))), }, - new HostMemory[] { - new HostMemory("spectest", "memory", new Memory(new MemoryLimits(1, 2))) + new ExternalMemory[] { + new ExternalMemory("spectest", "memory", new Memory(new MemoryLimits(1, 2))) }, - new HostTable[] { - new HostTable( + new ExternalTable[] { + new ExternalTable( "spectest", "table", new TableInstance(new Table(ValueType.FuncRef, new Limits(10, 20)))) diff --git a/runtime-tests/src/test/java/com/dylibso/chicory/testing/TestModule.java b/runtime-tests/src/test/java/com/dylibso/chicory/testing/TestModule.java index c8cebb144..cc4e2c602 100644 --- a/runtime-tests/src/test/java/com/dylibso/chicory/testing/TestModule.java +++ b/runtime-tests/src/test/java/com/dylibso/chicory/testing/TestModule.java @@ -1,6 +1,6 @@ package com.dylibso.chicory.testing; -import com.dylibso.chicory.runtime.HostImports; +import com.dylibso.chicory.runtime.ExternalValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.runtime.Store; import com.dylibso.chicory.wabt.Wat2Wasm; @@ -61,7 +61,7 @@ public TestModule(Module module) { } public Instance instantiate(Store s) { - HostImports hostImports = s.toHostImports(); - return Instance.builder(module).withHostImports(hostImports).build(); + ExternalValues externalValues = s.toExternalValues(); + return Instance.builder(module).withExternalValues(externalValues).build(); } } diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalFunction.java b/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalFunction.java new file mode 100644 index 000000000..3f67ef0f5 --- /dev/null +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalFunction.java @@ -0,0 +1,52 @@ +package com.dylibso.chicory.runtime; + +import com.dylibso.chicory.wasm.types.ValueType; +import java.util.List; + +public class ExternalFunction implements ExternalValue { + private final WasmFunctionHandle handle; + private final String moduleName; + private final String fieldName; + private final List paramTypes; + private final List returnTypes; + + public ExternalFunction( + String moduleName, + String fieldName, + WasmFunctionHandle handle, + List paramTypes, + List returnTypes) { + this.handle = handle; + this.moduleName = moduleName; + this.fieldName = fieldName; + this.paramTypes = paramTypes; + this.returnTypes = returnTypes; + } + + public WasmFunctionHandle handle() { + return handle; + } + + @Override + public String moduleName() { + return moduleName; + } + + @Override + public String fieldName() { + return fieldName; + } + + @Override + public Type type() { + return Type.FUNCTION; + } + + public List paramTypes() { + return paramTypes; + } + + public List returnTypes() { + return returnTypes; + } +} diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/HostGlobal.java b/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalGlobal.java similarity index 71% rename from runtime/src/main/java/com/dylibso/chicory/runtime/HostGlobal.java rename to runtime/src/main/java/com/dylibso/chicory/runtime/ExternalGlobal.java index 432deb6b4..fefdfaa0f 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/HostGlobal.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalGlobal.java @@ -1,11 +1,11 @@ package com.dylibso.chicory.runtime; -public class HostGlobal implements FromHost { +public class ExternalGlobal implements ExternalValue { private final GlobalInstance instance; private final String moduleName; private final String fieldName; - public HostGlobal(String moduleName, String fieldName, GlobalInstance instance) { + public ExternalGlobal(String moduleName, String fieldName, GlobalInstance instance) { this.instance = instance; this.moduleName = moduleName; this.fieldName = fieldName; @@ -26,7 +26,7 @@ public String fieldName() { } @Override - public FromHostType type() { - return FromHostType.GLOBAL; + public ExternalValue.Type type() { + return Type.GLOBAL; } } diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/HostMemory.java b/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalMemory.java similarity index 71% rename from runtime/src/main/java/com/dylibso/chicory/runtime/HostMemory.java rename to runtime/src/main/java/com/dylibso/chicory/runtime/ExternalMemory.java index fb65ca66f..087febec1 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/HostMemory.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalMemory.java @@ -1,11 +1,11 @@ package com.dylibso.chicory.runtime; -public class HostMemory implements FromHost { +public class ExternalMemory implements ExternalValue { private final String moduleName; private final String fieldName; private final Memory memory; - public HostMemory(String moduleName, String fieldName, Memory memory) { + public ExternalMemory(String moduleName, String fieldName, Memory memory) { this.moduleName = moduleName; this.fieldName = fieldName; this.memory = memory; @@ -22,8 +22,8 @@ public String fieldName() { } @Override - public FromHostType type() { - return FromHostType.MEMORY; + public ExternalValue.Type type() { + return Type.MEMORY; } public Memory memory() { diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/HostTable.java b/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalTable.java similarity index 77% rename from runtime/src/main/java/com/dylibso/chicory/runtime/HostTable.java rename to runtime/src/main/java/com/dylibso/chicory/runtime/ExternalTable.java index 4105956c5..da24388f9 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/HostTable.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalTable.java @@ -5,18 +5,18 @@ import com.dylibso.chicory.wasm.types.ValueType; import java.util.Map; -public class HostTable implements FromHost { +public class ExternalTable implements ExternalValue { private final String moduleName; private final String fieldName; private final TableInstance table; - public HostTable(String moduleName, String fieldName, TableInstance table) { + public ExternalTable(String moduleName, String fieldName, TableInstance table) { this.moduleName = moduleName; this.fieldName = fieldName; this.table = table; } - public HostTable(String moduleName, String fieldName, Map funcRefs) { + public ExternalTable(String moduleName, String fieldName, Map funcRefs) { this.moduleName = moduleName; this.fieldName = fieldName; @@ -43,8 +43,8 @@ public String fieldName() { } @Override - public FromHostType type() { - return FromHostType.TABLE; + public ExternalValue.Type type() { + return Type.TABLE; } public TableInstance table() { diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalValue.java b/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalValue.java new file mode 100644 index 000000000..589844eef --- /dev/null +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalValue.java @@ -0,0 +1,23 @@ +package com.dylibso.chicory.runtime; + +/** + * An external value is the runtime representation of an entity that can be imported or exported. + * It is an address denoting either a function instance, table instance, memory instance, + * or global instances in the shared store. + * + * See External Values. + */ +public interface ExternalValue { + enum Type { + FUNCTION, + GLOBAL, + MEMORY, + TABLE + } + + String moduleName(); + + String fieldName(); + + ExternalValue.Type type(); +} diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalValues.java b/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalValues.java new file mode 100644 index 000000000..231ef357d --- /dev/null +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalValues.java @@ -0,0 +1,216 @@ +package com.dylibso.chicory.runtime; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class ExternalValues { + private static final ExternalFunction[] NO_EXTERNAL_FUNCTIONS = new ExternalFunction[0]; + private static final ExternalGlobal[] NO_EXTERNAL_GLOBALS = new ExternalGlobal[0]; + private static final ExternalMemory[] NO_EXTERNAL_MEMORIES = new ExternalMemory[0]; + private static final ExternalTable[] NO_EXTERNAL_TABLES = new ExternalTable[0]; + + private final ExternalFunction[] functions; + private final ExternalGlobal[] globals; + private final ExternalMemory[] memories; + private final ExternalTable[] tables; + + public ExternalValues() { + this.functions = NO_EXTERNAL_FUNCTIONS; + this.globals = NO_EXTERNAL_GLOBALS; + this.memories = NO_EXTERNAL_MEMORIES; + this.tables = NO_EXTERNAL_TABLES; + } + + public ExternalValues(HostFunction[] functions) { + this.functions = functions.clone(); + this.globals = NO_EXTERNAL_GLOBALS; + this.memories = NO_EXTERNAL_MEMORIES; + this.tables = NO_EXTERNAL_TABLES; + } + + public ExternalValues(ExternalGlobal[] globals) { + this.functions = NO_EXTERNAL_FUNCTIONS; + this.globals = globals.clone(); + this.memories = NO_EXTERNAL_MEMORIES; + this.tables = NO_EXTERNAL_TABLES; + } + + public ExternalValues(ExternalMemory[] memories) { + this.functions = NO_EXTERNAL_FUNCTIONS; + this.globals = NO_EXTERNAL_GLOBALS; + this.memories = memories.clone(); + this.tables = NO_EXTERNAL_TABLES; + } + + public ExternalValues(ExternalMemory memory) { + this.functions = NO_EXTERNAL_FUNCTIONS; + this.globals = NO_EXTERNAL_GLOBALS; + this.memories = new ExternalMemory[] {memory}; + this.tables = NO_EXTERNAL_TABLES; + } + + public ExternalValues(ExternalTable[] tables) { + this.functions = NO_EXTERNAL_FUNCTIONS; + this.globals = NO_EXTERNAL_GLOBALS; + this.memories = NO_EXTERNAL_MEMORIES; + this.tables = tables.clone(); + } + + public ExternalValues( + ExternalFunction[] functions, + ExternalGlobal[] globals, + ExternalMemory memory, + ExternalTable[] tables) { + this.functions = functions.clone(); + this.globals = globals.clone(); + this.memories = new ExternalMemory[] {memory}; + this.tables = tables.clone(); + } + + public ExternalValues( + ExternalFunction[] functions, + ExternalGlobal[] globals, + ExternalMemory[] memories, + ExternalTable[] tables) { + this.functions = functions.clone(); + this.globals = globals.clone(); + this.memories = memories.clone(); + this.tables = tables.clone(); + } + + public ExternalFunction[] functions() { + return functions.clone(); + } + + public int functionCount() { + return functions.length; + } + + public ExternalFunction function(int idx) { + return functions[idx]; + } + + public ExternalGlobal[] globals() { + return globals; + } + + public int globalCount() { + return globals.length; + } + + public ExternalGlobal global(int idx) { + return globals[idx]; + } + + public ExternalMemory[] memories() { + return memories; + } + + public int memoryCount() { + return memories.length; + } + + public ExternalMemory memory(int idx) { + return memories[idx]; + } + + public ExternalTable[] tables() { + return tables; + } + + public int tableCount() { + return tables.length; + } + + public ExternalTable table(int idx) { + return tables[idx]; + } + + public static Builder builder() { + return new Builder(); + } + + public static ExternalValues empty() { + return new Builder().build(); + } + + public static final class Builder { + private List functions; + private List globals; + private List memories; + private List tables; + + Builder() {} + + public Builder withFunctions(List functions) { + this.functions = functions; + return this; + } + + public Builder addFunction(ExternalFunction... function) { + if (this.functions == null) { + this.functions = new ArrayList<>(); + } + Collections.addAll(this.functions, function); + return this; + } + + public Builder withGlobals(List globals) { + this.globals = globals; + return this; + } + + public Builder addGlobal(ExternalGlobal... global) { + if (this.globals == null) { + this.globals = new ArrayList<>(); + } + Collections.addAll(this.globals, global); + return this; + } + + public Builder withMemories(List memories) { + this.memories = memories; + return this; + } + + public Builder addMemory(ExternalMemory... memory) { + if (this.memories == null) { + this.memories = new ArrayList<>(); + } + Collections.addAll(this.memories, memory); + return this; + } + + public Builder withTables(List tables) { + this.tables = tables; + return this; + } + + public Builder addTable(ExternalTable... table) { + if (this.tables == null) { + this.tables = new ArrayList<>(); + } + Collections.addAll(this.tables, table); + return this; + } + + public ExternalValues build() { + final ExternalValues externalValues = + new ExternalValues( + functions == null + ? new HostFunction[0] + : functions.toArray(new HostFunction[0]), + globals == null + ? new ExternalGlobal[0] + : globals.toArray(new ExternalGlobal[0]), + memories == null + ? new ExternalMemory[0] + : memories.toArray(new ExternalMemory[0]), + tables == null + ? new ExternalTable[0] + : tables.toArray(new ExternalTable[0])); + return externalValues; + } + } +} diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/FromHost.java b/runtime/src/main/java/com/dylibso/chicory/runtime/FromHost.java deleted file mode 100644 index 60af99a7e..000000000 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/FromHost.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.dylibso.chicory.runtime; - -public interface FromHost { - enum FromHostType { - FUNCTION, - GLOBAL, - MEMORY, - TABLE - } - - String moduleName(); - - String fieldName(); - - FromHostType type(); -} diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/HostFunction.java b/runtime/src/main/java/com/dylibso/chicory/runtime/HostFunction.java index d37c12b26..e80ab404f 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/HostFunction.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/HostFunction.java @@ -3,50 +3,16 @@ import com.dylibso.chicory.wasm.types.ValueType; import java.util.List; -public class HostFunction implements FromHost { - private final WasmFunctionHandle handle; - private final String moduleName; - private final String fieldName; - private final List paramTypes; - private final List returnTypes; - +/** + * A HostFunction is an ExternalFunction that has been defined by the host. + */ +public class HostFunction extends ExternalFunction { public HostFunction( - WasmFunctionHandle handle, String moduleName, String fieldName, + WasmFunctionHandle handle, List paramTypes, List returnTypes) { - this.handle = handle; - this.moduleName = moduleName; - this.fieldName = fieldName; - this.paramTypes = paramTypes; - this.returnTypes = returnTypes; - } - - public WasmFunctionHandle handle() { - return handle; - } - - @Override - public String moduleName() { - return moduleName; - } - - @Override - public String fieldName() { - return fieldName; - } - - @Override - public FromHostType type() { - return FromHostType.FUNCTION; - } - - public List paramTypes() { - return paramTypes; - } - - public List returnTypes() { - return returnTypes; + super(moduleName, fieldName, handle, paramTypes, returnTypes); } } diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/HostImports.java b/runtime/src/main/java/com/dylibso/chicory/runtime/HostImports.java deleted file mode 100644 index 2de8b599c..000000000 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/HostImports.java +++ /dev/null @@ -1,211 +0,0 @@ -package com.dylibso.chicory.runtime; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class HostImports { - private static final HostFunction[] NO_HOST_FUNCTIONS = new HostFunction[0]; - private static final HostGlobal[] NO_HOST_GLOBALS = new HostGlobal[0]; - private static final HostMemory[] NO_HOST_MEMORIES = new HostMemory[0]; - private static final HostTable[] NO_HOST_TABLES = new HostTable[0]; - - private final HostFunction[] functions; - private final HostGlobal[] globals; - private final HostMemory[] memories; - private final HostTable[] tables; - - public HostImports() { - this.functions = NO_HOST_FUNCTIONS; - this.globals = NO_HOST_GLOBALS; - this.memories = NO_HOST_MEMORIES; - this.tables = NO_HOST_TABLES; - } - - public HostImports(HostFunction[] functions) { - this.functions = functions.clone(); - this.globals = NO_HOST_GLOBALS; - this.memories = NO_HOST_MEMORIES; - this.tables = NO_HOST_TABLES; - } - - public HostImports(HostGlobal[] globals) { - this.functions = NO_HOST_FUNCTIONS; - this.globals = globals.clone(); - this.memories = NO_HOST_MEMORIES; - this.tables = NO_HOST_TABLES; - } - - public HostImports(HostMemory[] memories) { - this.functions = NO_HOST_FUNCTIONS; - this.globals = NO_HOST_GLOBALS; - this.memories = memories.clone(); - this.tables = NO_HOST_TABLES; - } - - public HostImports(HostMemory memory) { - this.functions = NO_HOST_FUNCTIONS; - this.globals = NO_HOST_GLOBALS; - this.memories = new HostMemory[] {memory}; - this.tables = NO_HOST_TABLES; - } - - public HostImports(HostTable[] tables) { - this.functions = NO_HOST_FUNCTIONS; - this.globals = NO_HOST_GLOBALS; - this.memories = NO_HOST_MEMORIES; - this.tables = tables.clone(); - } - - public HostImports( - HostFunction[] functions, HostGlobal[] globals, HostMemory memory, HostTable[] tables) { - this.functions = functions.clone(); - this.globals = globals.clone(); - this.memories = new HostMemory[] {memory}; - this.tables = tables.clone(); - } - - public HostImports( - HostFunction[] functions, - HostGlobal[] globals, - HostMemory[] memories, - HostTable[] tables) { - this.functions = functions.clone(); - this.globals = globals.clone(); - this.memories = memories.clone(); - this.tables = tables.clone(); - } - - public HostFunction[] functions() { - return functions.clone(); - } - - public int functionCount() { - return functions.length; - } - - public HostFunction function(int idx) { - return functions[idx]; - } - - public HostGlobal[] globals() { - return globals; - } - - public int globalCount() { - return globals.length; - } - - public HostGlobal global(int idx) { - return globals[idx]; - } - - public HostMemory[] memories() { - return memories; - } - - public int memoryCount() { - return memories.length; - } - - public HostMemory memory(int idx) { - return memories[idx]; - } - - public HostTable[] tables() { - return tables; - } - - public int tableCount() { - return tables.length; - } - - public HostTable table(int idx) { - return tables[idx]; - } - - public static Builder builder() { - return new Builder(); - } - - public static HostImports empty() { - return new Builder().build(); - } - - public static final class Builder { - private List functions; - private List globals; - private List memories; - private List tables; - - Builder() {} - - public Builder withFunctions(List functions) { - this.functions = functions; - return this; - } - - public Builder addFunction(HostFunction... function) { - if (this.functions == null) { - this.functions = new ArrayList<>(); - } - Collections.addAll(this.functions, function); - return this; - } - - public Builder withGlobals(List globals) { - this.globals = globals; - return this; - } - - public Builder addGlobal(HostGlobal... global) { - if (this.globals == null) { - this.globals = new ArrayList<>(); - } - Collections.addAll(this.globals, global); - return this; - } - - public Builder withMemories(List memories) { - this.memories = memories; - return this; - } - - public Builder addMemory(HostMemory... memory) { - if (this.memories == null) { - this.memories = new ArrayList<>(); - } - Collections.addAll(this.memories, memory); - return this; - } - - public Builder withTables(List tables) { - this.tables = tables; - return this; - } - - public Builder addTable(HostTable... table) { - if (this.tables == null) { - this.tables = new ArrayList<>(); - } - Collections.addAll(this.tables, table); - return this; - } - - public HostImports build() { - final HostImports hostImports = - new HostImports( - functions == null - ? new HostFunction[0] - : functions.toArray(new HostFunction[0]), - globals == null - ? new HostGlobal[0] - : globals.toArray(new HostGlobal[0]), - memories == null - ? new HostMemory[0] - : memories.toArray(new HostMemory[0]), - tables == null ? new HostTable[0] : tables.toArray(new HostTable[0])); - return hostImports; - } - } -} diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/Instance.java b/runtime/src/main/java/com/dylibso/chicory/runtime/Instance.java index 9d2ef1db5..1f716a8d1 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Instance.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Instance.java @@ -56,7 +56,7 @@ public class Instance { private final int importedTablesOffset; private final FunctionType[] types; private final int[] functionTypes; - private final HostImports imports; + private final ExternalValues imports; private final Table[] roughTables; private final TableInstance[] tables; private final Element[] elements; @@ -74,7 +74,7 @@ public Instance( FunctionBody[] functions, FunctionType[] types, int[] functionTypes, - HostImports imports, + ExternalValues imports, Table[] tables, Element[] elements, Map exports, @@ -298,7 +298,7 @@ public int functionType(int idx) { return functionTypes[idx]; } - public HostImports imports() { + public ExternalValues imports() { return imports; } @@ -359,7 +359,7 @@ public static final class Builder { private boolean initialize = true; private boolean start = true; private ExecutionListener listener; - private HostImports hostImports; + private ExternalValues externalValues; private Function machineFactory; private Builder(Module module) { @@ -384,8 +384,8 @@ public Builder withUnsafeExecutionListener(ExecutionListener listener) { return this; } - public Builder withHostImports(HostImports hostImports) { - this.hostImports = hostImports; + public Builder withExternalValues(ExternalValues externalValues) { + this.externalValues = externalValues; return this; } @@ -394,7 +394,7 @@ public Builder withMachineFactory(Function machineFactory) { return this; } - private void validateHostFunctionSignature(FunctionImport imprt, HostFunction f) { + private void validateExternalFunctionSignature(FunctionImport imprt, ExternalFunction f) { var expectedType = module.typeSection().getType(imprt.typeIndex()); if (expectedType.params().size() != f.paramTypes().size() || expectedType.returns().size() != f.returnTypes().size()) { @@ -428,14 +428,14 @@ private void validateHostFunctionSignature(FunctionImport imprt, HostFunction f) } } - private void validateHostGlobalType(GlobalImport i, HostGlobal g) { + private void validateHostGlobalType(GlobalImport i, ExternalGlobal g) { if (i.type() != g.instance().getValue().type() || i.mutabilityType() != g.instance().getMutabilityType()) { throw new UnlinkableException("incompatible import type"); } } - private void validateHostTableType(TableImport i, HostTable t) { + private void validateHostTableType(TableImport i, ExternalTable t) { var minExpected = t.table().limits().min(); var maxExpected = t.table().limits().max(); var minCurrent = i.limits().min(); @@ -455,7 +455,7 @@ private void validateHostTableType(TableImport i, HostTable t) { } } - private void validateHostMemoryType(MemoryImport i, HostMemory m) { + private void validateHostMemoryType(MemoryImport i, ExternalMemory m) { // Notice we do not compare to m.memory().initialPages() // because m might have grown in the meantime. // Instead, we use the current number of pages. @@ -485,8 +485,8 @@ private void validateHostMemoryType(MemoryImport i, HostMemory m) { } private void validateNegativeImportType( - String moduleName, String name, FromHost[] fromHost) { - for (var fh : fromHost) { + String moduleName, String name, ExternalValue[] external) { + for (var fh : external) { if (fh.moduleName().equals(moduleName) && fh.fieldName().equals(name)) { throw new UnlinkableException("incompatible import type"); } @@ -494,33 +494,33 @@ private void validateNegativeImportType( } private void validateNegativeImportType( - String moduleName, String name, ExternalType typ, HostImports hostImports) { + String moduleName, String name, ExternalType typ, ExternalValues externalValues) { switch (typ) { case FUNCTION: - validateNegativeImportType(moduleName, name, hostImports.globals()); - validateNegativeImportType(moduleName, name, hostImports.memories()); - validateNegativeImportType(moduleName, name, hostImports.tables()); + validateNegativeImportType(moduleName, name, externalValues.globals()); + validateNegativeImportType(moduleName, name, externalValues.memories()); + validateNegativeImportType(moduleName, name, externalValues.tables()); break; case GLOBAL: - validateNegativeImportType(moduleName, name, hostImports.functions()); - validateNegativeImportType(moduleName, name, hostImports.memories()); - validateNegativeImportType(moduleName, name, hostImports.tables()); + validateNegativeImportType(moduleName, name, externalValues.functions()); + validateNegativeImportType(moduleName, name, externalValues.memories()); + validateNegativeImportType(moduleName, name, externalValues.tables()); break; case MEMORY: - validateNegativeImportType(moduleName, name, hostImports.functions()); - validateNegativeImportType(moduleName, name, hostImports.globals()); - validateNegativeImportType(moduleName, name, hostImports.tables()); + validateNegativeImportType(moduleName, name, externalValues.functions()); + validateNegativeImportType(moduleName, name, externalValues.globals()); + validateNegativeImportType(moduleName, name, externalValues.tables()); break; case TABLE: - validateNegativeImportType(moduleName, name, hostImports.functions()); - validateNegativeImportType(moduleName, name, hostImports.globals()); - validateNegativeImportType(moduleName, name, hostImports.memories()); + validateNegativeImportType(moduleName, name, externalValues.functions()); + validateNegativeImportType(moduleName, name, externalValues.globals()); + validateNegativeImportType(moduleName, name, externalValues.memories()); break; } } - private HostImports mapHostImports( - Import[] imports, HostImports hostImports, int memoryCount) { + private ExternalValues mapHostImports( + Import[] imports, ExternalValues externalValues, int memoryCount) { int hostFuncNum = 0; int hostGlobalNum = 0; int hostMemNum = 0; @@ -547,31 +547,32 @@ private HostImports mapHostImports( } // TODO: this can probably be refactored ... - var hostFuncs = new HostFunction[hostFuncNum]; + var hostFuncs = new ExternalFunction[hostFuncNum]; var hostFuncIdx = 0; - var hostGlobals = new HostGlobal[hostGlobalNum]; + var hostGlobals = new ExternalGlobal[hostGlobalNum]; var hostGlobalIdx = 0; - var hostMems = new HostMemory[hostMemNum]; + var hostMems = new ExternalMemory[hostMemNum]; var hostMemIdx = 0; - var hostTables = new HostTable[hostTableNum]; + var hostTables = new ExternalTable[hostTableNum]; var hostTableIdx = 0; int cnt; for (var impIdx = 0; impIdx < imports.length; impIdx++) { var i = imports[impIdx]; var name = i.moduleName() + "." + i.name(); var found = false; - validateNegativeImportType(i.moduleName(), i.name(), i.importType(), hostImports); - Function checkName = - (FromHost fh) -> + validateNegativeImportType( + i.moduleName(), i.name(), i.importType(), externalValues); + Function checkName = + (ExternalValue fh) -> i.moduleName().equals(fh.moduleName()) && i.name().equals(fh.fieldName()); switch (i.importType()) { case FUNCTION: - cnt = hostImports.functionCount(); + cnt = externalValues.functionCount(); for (int j = 0; j < cnt; j++) { - HostFunction f = hostImports.function(j); + ExternalFunction f = externalValues.function(j); if (checkName.apply(f)) { - validateHostFunctionSignature((FunctionImport) i, f); + validateExternalFunctionSignature((FunctionImport) i, f); hostFuncs[hostFuncIdx] = f; found = true; break; @@ -580,9 +581,9 @@ private HostImports mapHostImports( hostFuncIdx++; break; case GLOBAL: - cnt = hostImports.globalCount(); + cnt = externalValues.globalCount(); for (int j = 0; j < cnt; j++) { - HostGlobal g = hostImports.global(j); + ExternalGlobal g = externalValues.global(j); if (checkName.apply(g)) { validateHostGlobalType((GlobalImport) i, g); hostGlobals[hostGlobalIdx] = g; @@ -593,9 +594,9 @@ private HostImports mapHostImports( hostGlobalIdx++; break; case MEMORY: - cnt = hostImports.memoryCount(); + cnt = externalValues.memoryCount(); for (int j = 0; j < cnt; j++) { - HostMemory m = hostImports.memory(j); + ExternalMemory m = externalValues.memory(j); if (checkName.apply(m)) { validateHostMemoryType((MemoryImport) i, m); hostMems[hostMemIdx] = m; @@ -606,9 +607,9 @@ private HostImports mapHostImports( hostMemIdx++; break; case TABLE: - cnt = hostImports.tableCount(); + cnt = externalValues.tableCount(); for (int j = 0; j < cnt; j++) { - HostTable t = hostImports.table(j); + ExternalTable t = externalValues.table(j); if (checkName.apply(t)) { validateHostTableType((TableImport) i, t); hostTables[hostTableIdx] = t; @@ -628,7 +629,7 @@ private HostImports mapHostImports( } } - return new HostImports(hostFuncs, hostGlobals, hostMems, hostTables); + return new ExternalValues(hostFuncs, hostGlobals, hostMems, hostTables); } private Map genExports(ExportSection export) { @@ -678,7 +679,7 @@ public Instance build() { var mappedHostImports = mapHostImports( imports, - (hostImports == null) ? new HostImports() : hostImports, + (externalValues == null) ? new ExternalValues() : externalValues, module.memorySection().map(MemorySection::memoryCount).orElse(0)); if (module.startSection().isPresent()) { diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/Store.java b/runtime/src/main/java/com/dylibso/chicory/runtime/Store.java index ea6993013..151bde16e 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Store.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Store.java @@ -11,17 +11,17 @@ * The runtime storage for all function, global, memory, table instances. */ public class Store { - final LinkedHashMap functions = new LinkedHashMap<>(); - final LinkedHashMap globals = new LinkedHashMap<>(); - final LinkedHashMap memories = new LinkedHashMap<>(); - final LinkedHashMap tables = new LinkedHashMap<>(); + final LinkedHashMap functions = new LinkedHashMap<>(); + final LinkedHashMap globals = new LinkedHashMap<>(); + final LinkedHashMap memories = new LinkedHashMap<>(); + final LinkedHashMap tables = new LinkedHashMap<>(); public Store() {} /** * Add a function to the store. */ - public Store addFunction(HostFunction... function) { + public Store addFunction(ExternalFunction... function) { for (var f : function) { functions.put(new QualifiedName(f.moduleName(), f.fieldName()), f); } @@ -31,7 +31,7 @@ public Store addFunction(HostFunction... function) { /** * Add a global to the store. */ - public Store addGlobal(HostGlobal... global) { + public Store addGlobal(ExternalGlobal... global) { for (var g : global) { globals.put(new QualifiedName(g.moduleName(), g.fieldName()), g); } @@ -41,7 +41,7 @@ public Store addGlobal(HostGlobal... global) { /** * Add a memory to the store. */ - public Store addMemory(HostMemory... memory) { + public Store addMemory(ExternalMemory... memory) { for (var m : memory) { memories.put(new QualifiedName(m.moduleName(), m.fieldName()), m); } @@ -51,7 +51,7 @@ public Store addMemory(HostMemory... memory) { /** * Add a table to the store. */ - public Store addTable(HostTable... table) { + public Store addTable(ExternalTable... table) { for (var t : table) { tables.put(new QualifiedName(t.moduleName(), t.fieldName()), t); } @@ -59,24 +59,24 @@ public Store addTable(HostTable... table) { } /** - * Add the contents of a {@link HostImports} instance to the store. + * Add the contents of a {@link ExternalValues} instance to the store. */ - public Store addHostImports(HostImports hostImports) { - return this.addGlobal(hostImports.globals()) - .addFunction(hostImports.functions()) - .addMemory(hostImports.memories()) - .addTable(hostImports.tables()); + public Store addExternalValues(ExternalValues externalValues) { + return this.addGlobal(externalValues.globals()) + .addFunction(externalValues.functions()) + .addMemory(externalValues.memories()) + .addTable(externalValues.tables()); } /** - * Convert the contents of a store to a {@link HostImports} instance. + * Convert the contents of a store to a {@link ExternalValues} instance. */ - public HostImports toHostImports() { - return new HostImports( - functions.values().toArray(new HostFunction[0]), - globals.values().toArray(new HostGlobal[0]), - memories.values().toArray(new HostMemory[0]), - tables.values().toArray(new HostTable[0])); + public ExternalValues toExternalValues() { + return new ExternalValues( + functions.values().toArray(new ExternalFunction[0]), + globals.values().toArray(new ExternalGlobal[0]), + memories.values().toArray(new ExternalMemory[0]), + tables.values().toArray(new ExternalTable[0])); } /** @@ -98,25 +98,26 @@ public Store register(String name, Instance instance) { ExportFunction f = instance.export(exportName); FunctionType ftype = instance.exportType(exportName); this.addFunction( - new HostFunction( - (inst, args) -> f.apply(args), + new ExternalFunction( name, exportName, + (inst, args) -> f.apply(args), ftype.params(), ftype.returns())); break; case TABLE: - this.addTable(new HostTable(name, exportName, instance.table(export.index()))); + this.addTable( + new ExternalTable(name, exportName, instance.table(export.index()))); break; case MEMORY: - this.addMemory(new HostMemory(name, exportName, instance.memory())); + this.addMemory(new ExternalMemory(name, exportName, instance.memory())); break; case GLOBAL: GlobalInstance g = instance.global(export.index()); - this.addGlobal(new HostGlobal(name, exportName, g)); + this.addGlobal(new ExternalGlobal(name, exportName, g)); break; } } @@ -127,8 +128,8 @@ public Store register(String name, Instance instance) { * A shorthand for instantiating a module and registering it in the store. */ public Instance instantiate(String name, Module m) { - HostImports hostImports = toHostImports(); - Instance instance = Instance.builder(m).withHostImports(hostImports).build(); + ExternalValues externalValues = this.toExternalValues(); + Instance instance = Instance.builder(m).withExternalValues(externalValues).build(); register(name, instance); return instance; } diff --git a/runtime/src/test/java/com/dylibso/chicory/runtime/HostImportsTest.java b/runtime/src/test/java/com/dylibso/chicory/runtime/ExternalValuesTest.java similarity index 66% rename from runtime/src/test/java/com/dylibso/chicory/runtime/HostImportsTest.java rename to runtime/src/test/java/com/dylibso/chicory/runtime/ExternalValuesTest.java index 61cbe8bcf..2eedf7c08 100644 --- a/runtime/src/test/java/com/dylibso/chicory/runtime/HostImportsTest.java +++ b/runtime/src/test/java/com/dylibso/chicory/runtime/ExternalValuesTest.java @@ -8,13 +8,13 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -class HostImportsTest { +class ExternalValuesTest { @Nested class Builder { @Test void empty() { - final HostImports result = HostImports.builder().build(); + final ExternalValues result = ExternalValues.builder().build(); assertEquals(0, result.functionCount()); assertEquals(0, result.globalCount()); assertEquals(0, result.memoryCount()); @@ -26,22 +26,22 @@ class Function { @Test void withFunctions() { - final HostImports result = - HostImports.builder() + final ExternalValues result = + ExternalValues.builder() .withFunctions( Arrays.asList( - new HostFunction(null, "module_1", "", null, null), - new HostFunction(null, "module_2", "", null, null))) + new HostFunction("module_1", "", null, null, null), + new HostFunction("module_2", "", null, null, null))) .build(); assertEquals(2, result.functionCount()); } @Test void addFunction() { - final HostImports result = - HostImports.builder() - .addFunction(new HostFunction(null, "module_1", "", null, null)) - .addFunction(new HostFunction(null, "module_2", "", null, null)) + final ExternalValues result = + ExternalValues.builder() + .addFunction(new HostFunction("module_1", "", null, null, null)) + .addFunction(new HostFunction("module_2", "", null, null, null)) .build(); assertEquals(2, result.functionCount()); } @@ -52,15 +52,15 @@ class Global { @Test void withGlobals() { - final HostImports result = - HostImports.builder() + final ExternalValues result = + ExternalValues.builder() .withGlobals( Arrays.asList( - new HostGlobal( + new ExternalGlobal( "spectest", "global_i32", new GlobalInstance(Value.i32(666))), - new HostGlobal( + new ExternalGlobal( "spectest", "global_i64", new GlobalInstance(Value.i64(666))))) @@ -70,15 +70,15 @@ void withGlobals() { @Test void addGlobal() { - final HostImports result = - HostImports.builder() + final ExternalValues result = + ExternalValues.builder() .addGlobal( - new HostGlobal( + new ExternalGlobal( "spectest", "global_i32", new GlobalInstance(Value.i32(666)))) .addGlobal( - new HostGlobal( + new ExternalGlobal( "spectest", "global_i64", new GlobalInstance(Value.i64(666)))) @@ -92,22 +92,22 @@ class Memory { @Test void withMemories() { - final HostImports result = - HostImports.builder() + final ExternalValues result = + ExternalValues.builder() .withMemories( Arrays.asList( - new HostMemory("spectest", "memory", null), - new HostMemory("spectest", "memory_2", null))) + new ExternalMemory("spectest", "memory", null), + new ExternalMemory("spectest", "memory_2", null))) .build(); assertEquals(2, result.memoryCount()); } @Test void addMemory() { - final HostImports result = - HostImports.builder() - .addMemory(new HostMemory("spectest", "memory", null)) - .addMemory(new HostMemory("spectest", "memory_2", null)) + final ExternalValues result = + ExternalValues.builder() + .addMemory(new ExternalMemory("spectest", "memory", null)) + .addMemory(new ExternalMemory("spectest", "memory_2", null)) .build(); assertEquals(2, result.memoryCount()); } @@ -118,15 +118,15 @@ class Table { @Test void withTables() { - final HostImports result = - HostImports.builder() + final ExternalValues result = + ExternalValues.builder() .withTables( Arrays.asList( - new HostTable( + new ExternalTable( "spectest", "table", Collections.emptyMap()), - new HostTable( + new ExternalTable( "spectest", "table_2", Collections.emptyMap()))) @@ -136,12 +136,13 @@ void withTables() { @Test void addMemory() { - final HostImports result = - HostImports.builder() + final ExternalValues result = + ExternalValues.builder() .addTable( - new HostTable("spectest", "table", Collections.emptyMap())) + new ExternalTable( + "spectest", "table", Collections.emptyMap())) .addTable( - new HostTable( + new ExternalTable( "spectest", "table_2", Collections.emptyMap())) .build(); assertEquals(2, result.tableCount()); diff --git a/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java b/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java index aa93d8826..92aa501cd 100644 --- a/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java +++ b/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java @@ -83,6 +83,8 @@ public void shouldConsoleLogWithString() { var func = new HostFunction( + "console", + "log", (Instance instance, Value... args) -> { // decompiled is: console_log(13, 0); Memory memory = instance.memory(); @@ -96,14 +98,12 @@ public void shouldConsoleLogWithString() { return null; }, - "console", - "log", List.of(ValueType.I32, ValueType.I32), List.of()); var funcs = new HostFunction[] {func}; var instance = Instance.builder(loadModule("compiled/host-function.wat.wasm")) - .withHostImports(new HostImports(funcs)) + .withExternalValues(new ExternalValues(funcs)) .build(); var logIt = instance.export("logIt"); logIt.apply(); @@ -138,6 +138,8 @@ public void shouldWorkWithStartFunction() { var func = new HostFunction( + "env", + "gotit", (Instance instance, Value... args) -> { var val = args[0]; @@ -147,13 +149,11 @@ public void shouldWorkWithStartFunction() { return null; }, - "env", - "gotit", List.of(ValueType.I32), List.of()); var funcs = new HostFunction[] {func}; Instance.builder(loadModule("compiled/start.wat.wasm")) - .withHostImports(new HostImports(funcs)) + .withExternalValues(new ExternalValues(funcs)) .build(); assertTrue(count.get() > 0); @@ -265,39 +265,39 @@ public void shouldOperateMemoryOps() { public void shouldRunMixedImports() { var cbrtFunc = new HostFunction( + "env", + "cbrt", (Instance instance, Value... args) -> { var x = args[0].asInt(); var cbrt = Math.cbrt(x); return new Value[] {Value.fromDouble(cbrt)}; }, - "env", - "cbrt", List.of(ValueType.I32), List.of(ValueType.F64)); var logResult = new AtomicReference(null); var logFunc = new HostFunction( + "env", + "log", (Instance instance, Value... args) -> { var logLevel = args[0].asInt(); var value = (int) args[1].asDouble(); logResult.set(logLevel + ": " + value); return null; }, - "env", - "log", List.of(ValueType.I32, ValueType.F64), List.of()); - var memory = new HostMemory("env", "memory", new Memory(new MemoryLimits(1))); + var memory = new ExternalMemory("env", "memory", new Memory(new MemoryLimits(1))); var hostImports = - new HostImports( + new ExternalValues( new HostFunction[] {cbrtFunc, logFunc}, - new HostGlobal[0], + new ExternalGlobal[0], memory, - new HostTable[0]); + new ExternalTable[0]); var instance = Instance.builder(loadModule("compiled/mixed-imports.wat.wasm")) - .withHostImports(hostImports) + .withExternalValues(hostImports) .build(); var run = instance.export("main"); diff --git a/runtime/src/test/java/com/dylibso/chicory/runtime/StoreTest.java b/runtime/src/test/java/com/dylibso/chicory/runtime/StoreTest.java index f1ed16315..88bd6e853 100644 --- a/runtime/src/test/java/com/dylibso/chicory/runtime/StoreTest.java +++ b/runtime/src/test/java/com/dylibso/chicory/runtime/StoreTest.java @@ -19,11 +19,11 @@ private static Module loadModule(String fileName) { public void nameClashesShouldOverwriteTheStore() { Store store = new Store(); - HostFunction f1 = new HostFunction(null, "m", "f", null, null); + HostFunction f1 = new HostFunction("m", "f", null, null, null); store.addFunction(f1); assertEquals(f1, store.functions.get(new Store.QualifiedName("m", "f"))); - HostFunction f2 = new HostFunction(null, "m", "f", null, null); + HostFunction f2 = new HostFunction("m", "f", null, null, null); store.addFunction(f2); assertEquals(f2, store.functions.get(new Store.QualifiedName("m", "f"))); } diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaTestGen.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaTestGen.java index 779b03e6d..806578574 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaTestGen.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaTestGen.java @@ -132,7 +132,7 @@ public CompilationUnit generate(String name, Wast wast, File wasmFilesFolder) { testClass.addFieldWithInitializer( "Store", "store", - new NameExpr("new Store().addHostImports(Spectest.toHostImports())")); + new NameExpr("new Store().addExternalValues(Spectest.toExternalValues())")); String currentWasmFile = null; for (var cmd : wast.commands()) { diff --git a/wabt/src/main/java/com/dylibso/chicory/wabt/Wast2Json.java b/wabt/src/main/java/com/dylibso/chicory/wabt/Wast2Json.java index a67cbf2b3..fbb4f0887 100644 --- a/wabt/src/main/java/com/dylibso/chicory/wabt/Wast2Json.java +++ b/wabt/src/main/java/com/dylibso/chicory/wabt/Wast2Json.java @@ -5,7 +5,7 @@ import com.dylibso.chicory.log.Logger; import com.dylibso.chicory.log.SystemLogger; -import com.dylibso.chicory.runtime.HostImports; +import com.dylibso.chicory.runtime.ExternalValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.wasi.WasiOptions; import com.dylibso.chicory.wasi.WasiPreview1; @@ -87,8 +87,8 @@ public void process() { .withLogger(logger) .withOpts(wasiOpts.build()) .build()) { - HostImports imports = new HostImports(wasi.toHostFunctions()); - Instance.builder(MODULE).withHostImports(imports).build(); + ExternalValues imports = new ExternalValues(wasi.toHostFunctions()); + Instance.builder(MODULE).withExternalValues(imports).build(); } createDirectories(output.toPath().getParent()); diff --git a/wabt/src/main/java/com/dylibso/chicory/wabt/Wat2Wasm.java b/wabt/src/main/java/com/dylibso/chicory/wabt/Wat2Wasm.java index f8f6dd913..fae1d5373 100644 --- a/wabt/src/main/java/com/dylibso/chicory/wabt/Wat2Wasm.java +++ b/wabt/src/main/java/com/dylibso/chicory/wabt/Wat2Wasm.java @@ -4,7 +4,7 @@ import com.dylibso.chicory.log.Logger; import com.dylibso.chicory.log.SystemLogger; -import com.dylibso.chicory.runtime.HostImports; +import com.dylibso.chicory.runtime.ExternalValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.wasi.WasiOptions; import com.dylibso.chicory.wasi.WasiPreview1; @@ -71,8 +71,8 @@ private static byte[] parse(InputStream is, String fileName) { try (var wasi = WasiPreview1.builder().withLogger(logger).withOpts(wasiOpts).build()) { - HostImports imports = new HostImports(wasi.toHostFunctions()); - Instance.builder(MODULE).withHostImports(imports).build(); + ExternalValues imports = new ExternalValues(wasi.toHostFunctions()); + Instance.builder(MODULE).withExternalValues(imports).build(); } return stdoutStream.toByteArray(); diff --git a/wasi/README.md b/wasi/README.md index 04f2646b7..4efd05311 100644 --- a/wasi/README.md +++ b/wasi/README.md @@ -129,7 +129,7 @@ We do have intentions to support wasip2 in the future, however this work has not ## How to use -As a host who is running Wasm modules, WASI is just a collection of host imports that you need to provide +As a host who is running Wasm modules, WASI is just a collection of imports that you need to provide to a wasi-compiled module when instantiating it. You'll also need to configure some options for how these functions behave and what the module can and cannot do. @@ -154,9 +154,8 @@ copyFileFromWasmCorpus("hello-wasi.wat.wasm", "hello-wasi.wasm"); import com.dylibso.chicory.log.SystemLogger; import com.dylibso.chicory.wasi.WasiOptions; import com.dylibso.chicory.wasi.WasiPreview1; -import com.dylibso.chicory.wasm.Module; import com.dylibso.chicory.wasm.Parser; -import com.dylibso.chicory.runtime.HostImports; +import com.dylibso.chicory.runtime.ExternalValues; import com.dylibso.chicory.runtime.Instance; import java.io.File; @@ -166,11 +165,11 @@ var logger = new SystemLogger(); var options = WasiOptions.builder().build(); // create our instance of wasip1 var wasi = new WasiPreview1(logger, WasiOptions.builder().build()); -// turn those into host imports. Here we could add any other custom imports we have -var imports = new HostImports(wasi.toHostFunctions()); -// create the module and connect imports +// turn those into host functions. Here we could add any other custom definitions we have +var hostFunctions = new ExternalValues(wasi.toHostFunctions()); +// create the module and connect the external values // this will execute the module if it's a WASI command-pattern module -Instance.builder(Parser.parse(new File("hello-wasi.wasm"))).withHostImports(imports).build(); +Instance.builder(Parser.parse(new File("hello-wasi.wasm"))).withExternalValues(hostFunctions).build(); ``` > **Note**: Take note that we don't explicitly execute the module. The module will run when you instantiate it. This @@ -205,11 +204,11 @@ var fakeStderr = new ByteArrayOutputStream(); var wasiOpts = WasiOptions.builder().withStdout(fakeStdout).withStderr(fakeStderr).withStdin(fakeStdin).build(); var wasi = new WasiPreview1(logger, wasiOpts); -var imports = new HostImports(wasi.toHostFunctions()); +var hostFunctions = new ExternalValues(wasi.toHostFunctions()); // greet-wasi is a rust program that greets the string passed in stdin // instantiating will execute the module if it's a WASI command-pattern module -Instance.builder(Parser.parse(new File("greet-wasi.wasm"))).withHostImports(imports).build(); +Instance.builder(Parser.parse(new File("greet-wasi.wasm"))).withExternalValues(hostFunctions).build(); // check that we output the greeting assert(fakeStdout.toString().equals("Hello, Andrea!")); diff --git a/wasi/src/test/java/com/dylibso/chicory/wasi/WasiPreview1Test.java b/wasi/src/test/java/com/dylibso/chicory/wasi/WasiPreview1Test.java index 0729d091b..301e7f66a 100644 --- a/wasi/src/test/java/com/dylibso/chicory/wasi/WasiPreview1Test.java +++ b/wasi/src/test/java/com/dylibso/chicory/wasi/WasiPreview1Test.java @@ -7,7 +7,7 @@ import com.dylibso.chicory.log.Logger; import com.dylibso.chicory.log.SystemLogger; -import com.dylibso.chicory.runtime.HostImports; +import com.dylibso.chicory.runtime.ExternalValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.runtime.Memory; import com.dylibso.chicory.wasm.Module; @@ -32,9 +32,9 @@ public void shouldRunWasiModule() { var fakeStdout = new MockPrintStream(); var wasi = new WasiPreview1(this.logger, WasiOptions.builder().withStdout(fakeStdout).build()); - var imports = new HostImports(wasi.toHostFunctions()); + var imports = new ExternalValues(wasi.toHostFunctions()); Instance.builder(loadModule("compiled/hello-wasi.wat.wasm")) - .withHostImports(imports) + .withExternalValues(imports) .build(); assertEquals(fakeStdout.output().strip(), "hello world"); } @@ -45,9 +45,9 @@ public void shouldRunWasiRustModule() { var expected = "Hello, World!"; var stdout = new MockPrintStream(); var wasi = new WasiPreview1(this.logger, WasiOptions.builder().withStdout(stdout).build()); - var imports = new HostImports(wasi.toHostFunctions()); + var imports = new ExternalValues(wasi.toHostFunctions()); Instance.builder(loadModule("compiled/hello-wasi.rs.wasm")) - .withHostImports(imports) + .withExternalValues(imports) .build(); // run _start and prints Hello, World! assertEquals(expected, stdout.output().strip()); } @@ -58,9 +58,9 @@ public void shouldRunWasiGreetRustModule() { var fakeStdin = new ByteArrayInputStream("Benjamin".getBytes(UTF_8)); var wasiOpts = WasiOptions.builder().withStdout(System.out).withStdin(fakeStdin).build(); var wasi = new WasiPreview1(this.logger, wasiOpts); - var imports = new HostImports(wasi.toHostFunctions()); + var imports = new ExternalValues(wasi.toHostFunctions()); Instance.builder(loadModule("compiled/greet-wasi.rs.wasm")) - .withHostImports(imports) + .withExternalValues(imports) .build(); } @@ -72,9 +72,9 @@ public void shouldRunWasiDemoJavyModule() { var fakeStdout = new MockPrintStream(); var wasiOpts = WasiOptions.builder().withStdout(fakeStdout).withStdin(fakeStdin).build(); var wasi = new WasiPreview1(this.logger, wasiOpts); - var imports = new HostImports(wasi.toHostFunctions()); + var imports = new ExternalValues(wasi.toHostFunctions()); Instance.builder(loadModule("compiled/javy-demo.js.javy.wasm")) - .withHostImports(imports) + .withExternalValues(imports) .build(); assertEquals(fakeStdout.output(), "{\"foo\":3,\"newBar\":\"baz!\"}"); @@ -84,9 +84,9 @@ public void shouldRunWasiDemoJavyModule() { public void shouldRunTinyGoModule() { var wasiOpts = WasiOptions.builder().build(); var wasi = new WasiPreview1(this.logger, wasiOpts); - var imports = new HostImports(wasi.toHostFunctions()); + var imports = new ExternalValues(wasi.toHostFunctions()); var module = loadModule("compiled/sum.go.tiny.wasm"); - var instance = Instance.builder(module).withHostImports(imports).build(); + var instance = Instance.builder(module).withExternalValues(imports).build(); var sum = instance.export("add"); var result = sum.apply(Value.i32(20), Value.i32(22))[0]; @@ -98,12 +98,12 @@ public void shouldRunWasiGoModule() { var fakeStdout = new MockPrintStream(); var wasiOpts = WasiOptions.builder().withStdout(fakeStdout).build(); var wasi = new WasiPreview1(this.logger, wasiOpts); - var imports = new HostImports(wasi.toHostFunctions()); + var imports = new ExternalValues(wasi.toHostFunctions()); var module = loadModule("compiled/main.go.wasm"); var exit = assertThrows( WasiExitException.class, - () -> Instance.builder(module).withHostImports(imports).build()); + () -> Instance.builder(module).withExternalValues(imports).build()); assertEquals(0, exit.exitCode()); assertEquals("Hello, WebAssembly!\n", fakeStdout.output()); } @@ -120,10 +120,10 @@ public void shouldRunWasiDemoDotnetModule() throws Exception { .withArguments(List.of("")) .build(); var wasi = new WasiPreview1(this.logger, wasiOpts); - var imports = new HostImports(wasi.toHostFunctions()); + var imports = new ExternalValues(wasi.toHostFunctions()); var module = loadModule("compiled/basic.dotnet.wasm"); - Instance.builder(module).withHostImports(imports).build(); + Instance.builder(module).withExternalValues(imports).build(); assertEquals("Hello, Wasi Console!\n", fakeStdout.output()); } diff --git a/wasi/src/test/java/com/dylibso/chicory/wasi/WasiTestRunner.java b/wasi/src/test/java/com/dylibso/chicory/wasi/WasiTestRunner.java index cf7bf4fa5..2972239e3 100644 --- a/wasi/src/test/java/com/dylibso/chicory/wasi/WasiTestRunner.java +++ b/wasi/src/test/java/com/dylibso/chicory/wasi/WasiTestRunner.java @@ -4,7 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import com.dylibso.chicory.log.SystemLogger; -import com.dylibso.chicory.runtime.HostImports; +import com.dylibso.chicory.runtime.ExternalValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.wasm.Parser; import com.google.common.jimfs.Configuration; @@ -84,7 +84,7 @@ public static void execute( private static int execute(File test, WasiOptions wasiOptions) { try (var wasi = new WasiPreview1(LOGGER, wasiOptions)) { Instance.builder(Parser.parse(test)) - .withHostImports(new HostImports(wasi.toHostFunctions())) + .withExternalValues(new ExternalValues(wasi.toHostFunctions())) .build(); } catch (WasiExitException e) { return e.exitCode();