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 7dbd9a0c3..19877682a 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.ImportGlobal; +import com.dylibso.chicory.runtime.ImportMemory; +import com.dylibso.chicory.runtime.ImportTable; +import com.dylibso.chicory.runtime.ImportValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.runtime.Memory; import com.dylibso.chicory.runtime.TableInstance; @@ -23,54 +23,52 @@ public final class Spectest { private Spectest() {} - public static ExternalValues toExternalValues() { - return new ExternalValues( + public static ImportValues toImportValues() { + return new ImportValues( new HostFunction[] { - new HostFunction("spectest", "print", noop, List.of(), List.of()), + new HostFunction("spectest", "print", List.of(), List.of(), noop), new HostFunction( - "spectest", "print_i32", noop, List.of(ValueType.I32), List.of()), + "spectest", "print_i32", List.of(ValueType.I32), List.of(), noop), new HostFunction( - "spectest", "print_i32_1", noop, List.of(ValueType.I32), List.of()), + "spectest", "print_i32_1", List.of(ValueType.I32), List.of(), noop), new HostFunction( - "spectest", "print_i32_2", noop, List.of(ValueType.I32), List.of()), + "spectest", "print_i32_2", List.of(ValueType.I32), List.of(), noop), new HostFunction( - "spectest", "print_f32", noop, List.of(ValueType.F32), List.of()), + "spectest", "print_f32", List.of(ValueType.F32), List.of(), noop), new HostFunction( "spectest", "print_i32_f32", - noop, List.of(ValueType.I32, ValueType.F32), - List.of()), + List.of(), + noop), new HostFunction( - "spectest", "print_i64", noop, List.of(ValueType.I64), List.of()), + "spectest", "print_i64", List.of(ValueType.I64), List.of(), noop), new HostFunction( - "spectest", "print_i64_1", noop, List.of(ValueType.I64), List.of()), + "spectest", "print_i64_1", List.of(ValueType.I64), List.of(), noop), new HostFunction( - "spectest", "print_i64_2", noop, List.of(ValueType.I64), List.of()), + "spectest", "print_i64_2", List.of(ValueType.I64), List.of(), noop), new HostFunction( - "spectest", "print_f64", noop, List.of(ValueType.F64), List.of()), + "spectest", "print_f64", List.of(ValueType.F64), List.of(), noop), new HostFunction( "spectest", "print_f64_f64", - noop, List.of(ValueType.F64, ValueType.F64), - List.of()) + List.of(), + noop) }, - new ExternalGlobal[] { - new ExternalGlobal( - "spectest", "global_i32", new GlobalInstance(Value.i32(666))), - new ExternalGlobal( - "spectest", "global_i64", new GlobalInstance(Value.i64(666))), - new ExternalGlobal( + new ImportGlobal[] { + new ImportGlobal("spectest", "global_i32", new GlobalInstance(Value.i32(666))), + new ImportGlobal("spectest", "global_i64", new GlobalInstance(Value.i64(666))), + new ImportGlobal( "spectest", "global_f32", new GlobalInstance(Value.fromFloat(666.6f))), - new ExternalGlobal( + new ImportGlobal( "spectest", "global_f64", new GlobalInstance(Value.fromDouble(666.6))), }, - new ExternalMemory[] { - new ExternalMemory("spectest", "memory", new Memory(new MemoryLimits(1, 2))) + new ImportMemory[] { + new ImportMemory("spectest", "memory", new Memory(new MemoryLimits(1, 2))) }, - new ExternalTable[] { - new ExternalTable( + new ImportTable[] { + new ImportTable( "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 b84be359c..e9836ef04 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.ExternalValues; +import com.dylibso.chicory.runtime.ImportValues; 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) { - ExternalValues externalValues = s.toExternalValues(); + ImportValues importValues = s.toImportValues(); return Instance.builder(module) - .withExternalValues(externalValues) + .withImportValues(importValues) .withMachineFactory(AotMachine::new) .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 33fe40854..949854241 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.ExternalValues; +import com.dylibso.chicory.runtime.ImportValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.wasi.WasiOptions; import com.dylibso.chicory.wasi.WasiPreview1; @@ -59,17 +59,17 @@ public void run() { var module = Parser.parse(file); var imports = wasi - ? new ExternalValues( + ? new ImportValues( new WasiPreview1( logger, WasiOptions.builder().inheritSystem().build()) .toHostFunctions()) - : new ExternalValues(); + : new ImportValues(); var instance = Instance.builder(module) .withInitialize(true) .withStart(false) - .withExternalValues(imports) + .withImportValues(imports) .build(); if (functionName != null) { diff --git a/docs/docs/usage/host-functions.md b/docs/docs/usage/host-functions.md index a81705d78..350bc2383 100644 --- a/docs/docs/usage/host-functions.md +++ b/docs/docs/usage/host-functions.md @@ -53,15 +53,15 @@ import com.dylibso.chicory.wasm.types.ValueType; var func = new HostFunction( "console", "log", + List.of(ValueType.I32, ValueType.I32), + List.of(), (Instance instance, long... args) -> { // decompiled is: console_log(13, 0); var len = (int) args[0]; var offset = (int) args[1]; var message = instance.memory().readString(offset, len); println(message); return null; - }, - List.of(ValueType.I32, ValueType.I32), - List.of()); + }); ``` Again we're dealing with pointers here. The module calls `console.log` with the length of the string @@ -78,9 +78,9 @@ Now we just need to pass this host function in during our instantiation phase: ```java import com.dylibso.chicory.wasm.Parser; -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(); +import com.dylibso.chicory.runtime.ImportValues; +var hostFunctions = new ImportValues(new HostFunction[] {func}); +var instance = Instance.builder(Parser.parse(new File("./logger.wasm"))).withImportValues(hostFunctions).build(); var logIt = instance.export("logIt"); logIt.apply(); // should print "Hello, World!" 10 times diff --git a/docs/docs/usage/linking.md b/docs/docs/usage/linking.md index 3aef480fc..bfaad47cd 100644 --- a/docs/docs/usage/linking.md +++ b/docs/docs/usage/linking.md @@ -22,23 +22,23 @@ TODO: should we make this more explicit? import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.runtime.HostFunction; -import com.dylibso.chicory.runtime.ExternalValues; +import com.dylibso.chicory.runtime.ImportValues; import com.dylibso.chicory.wasm.types.ValueType; var func = new HostFunction( "console", "log", + List.of(ValueType.I32, ValueType.I32), + List.of(), (Instance instance, long... args) -> { // decompiled is: console_log(13, 0); var len = (int) args[0]; var offset = (int) args[1]; var message = instance.memory().readString(offset, len); println(message); return null; - }, - List.of(ValueType.I32, ValueType.I32), - List.of()); -var hostFunctions = new ExternalValues(new HostFunction[] {func}); -var instance = Instance.builder(Parser.parse(new File("./logger.wasm"))).withExternalValues(hostFunctions).build(); + }); +var hostFunctions = new ImportValues(new HostFunction[] {func}); +var instance = Instance.builder(Parser.parse(new File("./logger.wasm"))).withImportValues(hostFunctions).build(); ``` --> @@ -68,9 +68,9 @@ var logger2 = store.instantiate("logger2", Parser.parse(new File("./logger.wasm" This is equivalent to: ```java -var external = store.toExternalValues(); +var external = store.toImportValues(); var m = Parser.parse(new File("./logger.wasm")); -var instance = Instance.builder(m).withExternalValues(external).build(); +var instance = Instance.builder(m).withImportValues(external).build(); store.register("logger2", instance); ``` diff --git a/host-module/it/src/it/with-imports/src/test/java/chicory/test/WithImportsTest.java b/host-module/it/src/it/with-imports/src/test/java/chicory/test/WithImportsTest.java index b1ece8313..e00cee672 100644 --- a/host-module/it/src/it/with-imports/src/test/java/chicory/test/WithImportsTest.java +++ b/host-module/it/src/it/with-imports/src/test/java/chicory/test/WithImportsTest.java @@ -4,7 +4,7 @@ import com.dylibso.chicory.hostmodule.annotations.HostModule; import com.dylibso.chicory.hostmodule.annotations.WasmExport; -import com.dylibso.chicory.runtime.ExternalValues; +import com.dylibso.chicory.runtime.ImportValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.wasm.Parser; import java.util.concurrent.atomic.AtomicInteger; @@ -24,8 +24,8 @@ public TestModule() { Parser.parse( WithImportsTest.class.getResourceAsStream( "/compiled/host-function.wat.wasm"))) - .withExternalValues( - new ExternalValues( + .withImportValues( + new ImportValues( TestModule_ModuleFactory.toHostFunctions(this))) .build(); } diff --git a/host-module/processor/src/main/java/com/dylibso/chicory/hostmodule/processor/HostModuleProcessor.java b/host-module/processor/src/main/java/com/dylibso/chicory/hostmodule/processor/HostModuleProcessor.java index 0a6fe47af..6e1b2208e 100644 --- a/host-module/processor/src/main/java/com/dylibso/chicory/hostmodule/processor/HostModuleProcessor.java +++ b/host-module/processor/src/main/java/com/dylibso/chicory/hostmodule/processor/HostModuleProcessor.java @@ -279,9 +279,9 @@ private Expression processMethod( .setType("HostFunction") .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)); + .addArgument(new MethodCallExpr(new NameExpr("List"), "of", returnType)) + .addArgument(handle); // TODO: update javaparser and replace with multiline formatting function.setLineComment(""); return function; diff --git a/host-module/processor/src/test/resources/BasicMathGenerated.java b/host-module/processor/src/test/resources/BasicMathGenerated.java index 4079e522b..c9417142a 100644 --- a/host-module/processor/src/test/resources/BasicMathGenerated.java +++ b/host-module/processor/src/test/resources/BasicMathGenerated.java @@ -15,33 +15,33 @@ private BasicMath_ModuleFactory() { public static HostFunction[] toHostFunctions(BasicMath functions) { return new HostFunction[] { // - new HostFunction("math", - "add", - (Instance instance, long... args) -> { - long result = functions.add((int) args[0], - (int) args[1]); - return new long[] { result }; - }, - List.of(ValueType.I32, - ValueType.I32), - List.of(ValueType.I64)), // - new HostFunction("math", - "square", - (Instance instance, long... args) -> { - double result = functions.pow2(Value.longToFloat(args[0])); - return new long[] { Value.doubleToLong(result) }; - }, - List.of(ValueType.F32), - List.of(ValueType.F64)), // - new HostFunction("math", - "floor_div", - (Instance instance, long... args) -> { - int result = functions.floorDiv((int) args[0], - (int) args[1]); - return new long[] { (long) result }; - }, - List.of(ValueType.I32, - ValueType.I32), - List.of(ValueType.I32)) }; + new HostFunction("math", + "add", + List.of(ValueType.I32, + ValueType.I32), + List.of(ValueType.I64), + (Instance instance, long... args) -> { + long result = functions.add((int) args[0], + (int) args[1]); + return new long[] { result }; + }), // + new HostFunction("math", + "square", + List.of(ValueType.F32), + List.of(ValueType.F64), + (Instance instance, long... args) -> { + double result = functions.pow2(Value.longToFloat(args[0])); + return new long[] { Value.doubleToLong(result) }; + }), // + new HostFunction("math", + "floor_div", + List.of(ValueType.I32, + ValueType.I32), + List.of(ValueType.I32), + (Instance instance, long... args) -> { + int result = functions.floorDiv((int) args[0], + (int) args[1]); + return new long[] { (long) result }; + }) }; } } diff --git a/host-module/processor/src/test/resources/NestedGenerated.java b/host-module/processor/src/test/resources/NestedGenerated.java index 40cc4a04f..f3243461c 100644 --- a/host-module/processor/src/test/resources/NestedGenerated.java +++ b/host-module/processor/src/test/resources/NestedGenerated.java @@ -16,24 +16,24 @@ private Nested_ModuleFactory() { public static HostFunction[] toHostFunctions(Nested functions) { return new HostFunction[] { // - new HostFunction("nested", - "print", - (Instance instance, long... args) -> { - functions.print(instance.memory(), - (int) args[0], - (int) args[1]); - return null; - }, - List.of(ValueType.I32, - ValueType.I32), - List.of()), // - new HostFunction("nested", - "exit", - (Instance instance, long... args) -> { - functions.exit(); - return null; - }, - List.of(), - List.of()) }; + new HostFunction("nested", + "print", + List.of(ValueType.I32, + ValueType.I32), + List.of(), + (Instance instance, long... args) -> { + functions.print(instance.memory(), + (int) args[0], + (int) args[1]); + return null; + }), // + new HostFunction("nested", + "exit", + List.of(), + List.of(), + (Instance instance, long... args) -> { + functions.exit(); + return null; + }) }; } } diff --git a/host-module/processor/src/test/resources/NoPackageGenerated.java b/host-module/processor/src/test/resources/NoPackageGenerated.java index 0958f1c01..208016bb4 100644 --- a/host-module/processor/src/test/resources/NoPackageGenerated.java +++ b/host-module/processor/src/test/resources/NoPackageGenerated.java @@ -13,24 +13,24 @@ private NoPackage_ModuleFactory() { public static HostFunction[] toHostFunctions(NoPackage functions) { return new HostFunction[] { // - new HostFunction("nopackage", - "print", - (Instance instance, long... args) -> { - functions.print(instance.memory(), - (int) args[0], - (int) args[1]); - return null; - }, - List.of(ValueType.I32, - ValueType.I32), - List.of()), // - new HostFunction("nopackage", - "exit", - (Instance instance, long... args) -> { - functions.exit(); - return null; - }, - List.of(), - List.of()) }; + new HostFunction("nopackage", + "print", + List.of(ValueType.I32, + ValueType.I32), + List.of(), + (Instance instance, long... args) -> { + functions.print(instance.memory(), + (int) args[0], + (int) args[1]); + return null; + }), // + new HostFunction("nopackage", + "exit", + List.of(), + List.of(), + (Instance instance, long... args) -> { + functions.exit(); + return null; + }) }; } } diff --git a/host-module/processor/src/test/resources/SimpleGenerated.java b/host-module/processor/src/test/resources/SimpleGenerated.java index 867c3d10a..ed300ef56 100644 --- a/host-module/processor/src/test/resources/SimpleGenerated.java +++ b/host-module/processor/src/test/resources/SimpleGenerated.java @@ -15,42 +15,42 @@ private Simple_ModuleFactory() { public static HostFunction[] toHostFunctions(Simple functions) { return new HostFunction[] { // - new HostFunction("simple", - "print", - (Instance instance, long... args) -> { - functions.print(instance.memory().readString((int) args[0], - (int) args[1])); - return null; - }, - List.of(ValueType.I32, - ValueType.I32), - List.of()), // - new HostFunction("simple", - "printx", - (Instance instance, long... args) -> { - functions.printx(instance.memory().readCString((int) args[0])); - return null; - }, - List.of(ValueType.I32), - List.of()), // - new HostFunction("simple", - "random_get", - (Instance instance, long... args) -> { - functions.randomGet(instance.memory(), - (int) args[0], - (int) args[1]); - return null; - }, - List.of(ValueType.I32, - ValueType.I32), - List.of()), // - new HostFunction("simple", - "exit", - (Instance instance, long... args) -> { - functions.exit(); - return null; - }, - List.of(), - List.of()) }; + new HostFunction("simple", + "print", + List.of(ValueType.I32, + ValueType.I32), + List.of(), + (Instance instance, long... args) -> { + functions.print(instance.memory().readString((int) args[0], + (int) args[1])); + return null; + }), // + new HostFunction("simple", + "printx", + List.of(ValueType.I32), + List.of(), + (Instance instance, long... args) -> { + functions.printx(instance.memory().readCString((int) args[0])); + return null; + }), // + new HostFunction("simple", + "random_get", + List.of(ValueType.I32, + ValueType.I32), + List.of(), + (Instance instance, long... args) -> { + functions.randomGet(instance.memory(), + (int) args[0], + (int) args[1]); + return null; + }), // + new HostFunction("simple", + "exit", + List.of(), + List.of(), + (Instance instance, long... args) -> { + functions.exit(); + return null; + }) }; } } diff --git a/machine-tests/src/test/java/com/dylibso/chicory/testing/MachinesTest.java b/machine-tests/src/test/java/com/dylibso/chicory/testing/MachinesTest.java index 6f0706dab..e5109a822 100644 --- a/machine-tests/src/test/java/com/dylibso/chicory/testing/MachinesTest.java +++ b/machine-tests/src/test/java/com/dylibso/chicory/testing/MachinesTest.java @@ -7,7 +7,7 @@ import com.dylibso.chicory.aot.AotMachine; import com.dylibso.chicory.log.SystemLogger; -import com.dylibso.chicory.runtime.ExternalValues; +import com.dylibso.chicory.runtime.ImportValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.runtime.Store; import com.dylibso.chicory.testing.gen.DynamicHelloJSMachineFactory; @@ -76,13 +76,13 @@ public void shouldRunQuickJsPrecompiled() { var quickjs = quickJsInstanceBuilder() .withMachineFactory(QuickJSMachineFactory::create) - .withExternalValues(new ExternalValues(wasi.toHostFunctions())) + .withImportValues(new ImportValues(wasi.toHostFunctions())) .build(); var store = new Store().register("javy_quickjs_provider_v1", quickjs); // the module is going to use the interpreter instead - moduleInstanceBuilder().withExternalValues(store.toExternalValues()).build(); + moduleInstanceBuilder().withImportValues(store.toImportValues()).build(); // stderr? assertEquals(expectedOutput, stderr.toString(UTF_8)); @@ -90,7 +90,7 @@ public void shouldRunQuickJsPrecompiled() { // and now runtime AOT moduleInstanceBuilder() .withMachineFactory(AotMachine::new) - .withExternalValues(store.toExternalValues()) + .withImportValues(store.toImportValues()) .build(); assertEquals(expectedOutput + expectedOutput, stderr.toString(UTF_8)); @@ -106,7 +106,7 @@ public void shouldRunQuickJsInterpreted() { // using the pre-compiled version of QuickJS var quickjs = quickJsInstanceBuilder() - .withExternalValues(new ExternalValues(wasi.toHostFunctions())) + .withImportValues(new ImportValues(wasi.toHostFunctions())) .build(); var store = new Store().register("javy_quickjs_provider_v1", quickjs); @@ -114,7 +114,7 @@ public void shouldRunQuickJsInterpreted() { // the module is going to use the pre compiled aot moduleInstanceBuilder() .withMachineFactory(DynamicHelloJSMachineFactory::create) - .withExternalValues(store.toExternalValues()) + .withImportValues(store.toImportValues()) .build(); // stderr? @@ -123,7 +123,7 @@ public void shouldRunQuickJsInterpreted() { // and now runtime AOT moduleInstanceBuilder() .withMachineFactory(AotMachine::new) - .withExternalValues(store.toExternalValues()) + .withImportValues(store.toImportValues()) .build(); assertEquals(expectedOutput + expectedOutput, stderr.toString(UTF_8)); @@ -140,7 +140,7 @@ public void shouldRunQuickJsRuntimeAot() { var quickjs = quickJsInstanceBuilder() .withMachineFactory(AotMachine::new) - .withExternalValues(new ExternalValues(wasi.toHostFunctions())) + .withImportValues(new ImportValues(wasi.toHostFunctions())) .build(); var store = new Store().register("javy_quickjs_provider_v1", quickjs); @@ -148,14 +148,14 @@ public void shouldRunQuickJsRuntimeAot() { // the module is going to use the pre compiled aot moduleInstanceBuilder() .withMachineFactory(DynamicHelloJSMachineFactory::create) - .withExternalValues(store.toExternalValues()) + .withImportValues(store.toImportValues()) .build(); // stderr? assertEquals(expectedOutput, stderr.toString(UTF_8)); // and now the interpreter - moduleInstanceBuilder().withExternalValues(store.toExternalValues()).build(); + moduleInstanceBuilder().withImportValues(store.toImportValues()).build(); assertEquals(expectedOutput + expectedOutput, stderr.toString(UTF_8)); } @@ -184,7 +184,7 @@ public void shouldUseMachineCallOnlyForExport() throws Exception { .build(); var logger = new SystemLogger(); try (var wasi = WasiPreview1.builder().withLogger(logger).withOpts(wasiOpts).build()) { - ExternalValues imports = new ExternalValues(wasi.toHostFunctions()); + ImportValues imports = new ImportValues(wasi.toHostFunctions()); var wat2WasmModule = Parser.parse(new File("../wabt/src/main/resources/wat2wasm")); var startFunctionIndex = new AtomicInteger(); for (int i = 0; i < wat2WasmModule.exportSection().exportCount(); i++) { @@ -206,7 +206,7 @@ public void shouldUseMachineCallOnlyForExport() throws Exception { return machine.call(funcId, args); }; }) - .withExternalValues(imports) + .withImportValues(imports) .build(); } 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 7dbd9a0c3..19877682a 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.ImportGlobal; +import com.dylibso.chicory.runtime.ImportMemory; +import com.dylibso.chicory.runtime.ImportTable; +import com.dylibso.chicory.runtime.ImportValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.runtime.Memory; import com.dylibso.chicory.runtime.TableInstance; @@ -23,54 +23,52 @@ public final class Spectest { private Spectest() {} - public static ExternalValues toExternalValues() { - return new ExternalValues( + public static ImportValues toImportValues() { + return new ImportValues( new HostFunction[] { - new HostFunction("spectest", "print", noop, List.of(), List.of()), + new HostFunction("spectest", "print", List.of(), List.of(), noop), new HostFunction( - "spectest", "print_i32", noop, List.of(ValueType.I32), List.of()), + "spectest", "print_i32", List.of(ValueType.I32), List.of(), noop), new HostFunction( - "spectest", "print_i32_1", noop, List.of(ValueType.I32), List.of()), + "spectest", "print_i32_1", List.of(ValueType.I32), List.of(), noop), new HostFunction( - "spectest", "print_i32_2", noop, List.of(ValueType.I32), List.of()), + "spectest", "print_i32_2", List.of(ValueType.I32), List.of(), noop), new HostFunction( - "spectest", "print_f32", noop, List.of(ValueType.F32), List.of()), + "spectest", "print_f32", List.of(ValueType.F32), List.of(), noop), new HostFunction( "spectest", "print_i32_f32", - noop, List.of(ValueType.I32, ValueType.F32), - List.of()), + List.of(), + noop), new HostFunction( - "spectest", "print_i64", noop, List.of(ValueType.I64), List.of()), + "spectest", "print_i64", List.of(ValueType.I64), List.of(), noop), new HostFunction( - "spectest", "print_i64_1", noop, List.of(ValueType.I64), List.of()), + "spectest", "print_i64_1", List.of(ValueType.I64), List.of(), noop), new HostFunction( - "spectest", "print_i64_2", noop, List.of(ValueType.I64), List.of()), + "spectest", "print_i64_2", List.of(ValueType.I64), List.of(), noop), new HostFunction( - "spectest", "print_f64", noop, List.of(ValueType.F64), List.of()), + "spectest", "print_f64", List.of(ValueType.F64), List.of(), noop), new HostFunction( "spectest", "print_f64_f64", - noop, List.of(ValueType.F64, ValueType.F64), - List.of()) + List.of(), + noop) }, - new ExternalGlobal[] { - new ExternalGlobal( - "spectest", "global_i32", new GlobalInstance(Value.i32(666))), - new ExternalGlobal( - "spectest", "global_i64", new GlobalInstance(Value.i64(666))), - new ExternalGlobal( + new ImportGlobal[] { + new ImportGlobal("spectest", "global_i32", new GlobalInstance(Value.i32(666))), + new ImportGlobal("spectest", "global_i64", new GlobalInstance(Value.i64(666))), + new ImportGlobal( "spectest", "global_f32", new GlobalInstance(Value.fromFloat(666.6f))), - new ExternalGlobal( + new ImportGlobal( "spectest", "global_f64", new GlobalInstance(Value.fromDouble(666.6))), }, - new ExternalMemory[] { - new ExternalMemory("spectest", "memory", new Memory(new MemoryLimits(1, 2))) + new ImportMemory[] { + new ImportMemory("spectest", "memory", new Memory(new MemoryLimits(1, 2))) }, - new ExternalTable[] { - new ExternalTable( + new ImportTable[] { + new ImportTable( "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 cc4e2c602..b73cb44fc 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.ExternalValues; +import com.dylibso.chicory.runtime.ImportValues; 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) { - ExternalValues externalValues = s.toExternalValues(); - return Instance.builder(module).withExternalValues(externalValues).build(); + ImportValues importValues = s.toImportValues(); + return Instance.builder(module).withImportValues(importValues).build(); } } 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 9d749562b..42bb00e28 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/HostFunction.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/HostFunction.java @@ -6,13 +6,13 @@ /** * A HostFunction is an ExternalFunction that has been defined by the host. */ -public class HostFunction extends ExternalFunction { +public class HostFunction extends ImportFunction { public HostFunction( String moduleName, String symbolName, - WasmFunctionHandle handle, List paramTypes, - List returnTypes) { - super(moduleName, symbolName, handle, paramTypes, returnTypes); + List returnTypes, + WasmFunctionHandle handle) { + super(moduleName, symbolName, paramTypes, returnTypes, handle); } } diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalFunction.java b/runtime/src/main/java/com/dylibso/chicory/runtime/ImportFunction.java similarity index 85% rename from runtime/src/main/java/com/dylibso/chicory/runtime/ExternalFunction.java rename to runtime/src/main/java/com/dylibso/chicory/runtime/ImportFunction.java index 2683f1ab2..d9a3f157f 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalFunction.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/ImportFunction.java @@ -3,24 +3,24 @@ import com.dylibso.chicory.wasm.types.ValueType; import java.util.List; -public class ExternalFunction implements ExternalValue { - private final WasmFunctionHandle handle; +public class ImportFunction implements ImportValue { private final String module; private final String name; private final List paramTypes; private final List returnTypes; + private final WasmFunctionHandle handle; - public ExternalFunction( + public ImportFunction( String module, String name, - WasmFunctionHandle handle, List paramTypes, - List returnTypes) { - this.handle = handle; + List returnTypes, + WasmFunctionHandle handle) { this.module = module; this.name = name; this.paramTypes = paramTypes; this.returnTypes = returnTypes; + this.handle = handle; } public WasmFunctionHandle handle() { diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalGlobal.java b/runtime/src/main/java/com/dylibso/chicory/runtime/ImportGlobal.java similarity index 74% rename from runtime/src/main/java/com/dylibso/chicory/runtime/ExternalGlobal.java rename to runtime/src/main/java/com/dylibso/chicory/runtime/ImportGlobal.java index c8a9172db..091cb93f7 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalGlobal.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/ImportGlobal.java @@ -1,11 +1,11 @@ package com.dylibso.chicory.runtime; -public class ExternalGlobal implements ExternalValue { +public class ImportGlobal implements ImportValue { private final GlobalInstance instance; private final String module; private final String name; - public ExternalGlobal(String module, String name, GlobalInstance instance) { + public ImportGlobal(String module, String name, GlobalInstance instance) { this.instance = instance; this.module = module; this.name = name; @@ -26,7 +26,7 @@ public String name() { } @Override - public ExternalValue.Type type() { + public ImportValue.Type type() { return Type.GLOBAL; } } diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalMemory.java b/runtime/src/main/java/com/dylibso/chicory/runtime/ImportMemory.java similarity index 74% rename from runtime/src/main/java/com/dylibso/chicory/runtime/ExternalMemory.java rename to runtime/src/main/java/com/dylibso/chicory/runtime/ImportMemory.java index 5c099c268..b6394418f 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalMemory.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/ImportMemory.java @@ -1,11 +1,11 @@ package com.dylibso.chicory.runtime; -public class ExternalMemory implements ExternalValue { +public class ImportMemory implements ImportValue { private final String module; private final String name; private final Memory memory; - public ExternalMemory(String module, String name, Memory memory) { + public ImportMemory(String module, String name, Memory memory) { this.module = module; this.name = name; this.memory = memory; @@ -22,7 +22,7 @@ public String name() { } @Override - public ExternalValue.Type type() { + public ImportValue.Type type() { return Type.MEMORY; } diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalTable.java b/runtime/src/main/java/com/dylibso/chicory/runtime/ImportTable.java similarity index 80% rename from runtime/src/main/java/com/dylibso/chicory/runtime/ExternalTable.java rename to runtime/src/main/java/com/dylibso/chicory/runtime/ImportTable.java index e21cea970..1438f9e51 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalTable.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/ImportTable.java @@ -5,18 +5,18 @@ import com.dylibso.chicory.wasm.types.ValueType; import java.util.Map; -public class ExternalTable implements ExternalValue { +public class ImportTable implements ImportValue { private final String module; private final String name; private final TableInstance table; - public ExternalTable(String module, String name, TableInstance table) { + public ImportTable(String module, String name, TableInstance table) { this.module = module; this.name = name; this.table = table; } - public ExternalTable(String module, String name, Map funcRefs) { + public ImportTable(String module, String name, Map funcRefs) { this.module = module; this.name = name; @@ -43,7 +43,7 @@ public String name() { } @Override - public ExternalValue.Type type() { + public ImportValue.Type type() { return Type.TABLE; } diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalValue.java b/runtime/src/main/java/com/dylibso/chicory/runtime/ImportValue.java similarity index 61% rename from runtime/src/main/java/com/dylibso/chicory/runtime/ExternalValue.java rename to runtime/src/main/java/com/dylibso/chicory/runtime/ImportValue.java index 833e686fd..99567e7e2 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalValue.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/ImportValue.java @@ -1,13 +1,15 @@ package com.dylibso.chicory.runtime; /** - * An external value is the runtime representation of an entity that can be imported or exported. + * An external value is the runtime representation of an entity that can be imported. * It is an address denoting either a function instance, table instance, memory instance, * or global instances in the shared store. * - * See External Values. + * See also External Values. + * + * @see ExportFunction */ -public interface ExternalValue { +public interface ImportValue { enum Type { FUNCTION, GLOBAL, @@ -19,5 +21,5 @@ enum Type { String name(); - ExternalValue.Type type(); + ImportValue.Type type(); } diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalValues.java b/runtime/src/main/java/com/dylibso/chicory/runtime/ImportValues.java similarity index 54% rename from runtime/src/main/java/com/dylibso/chicory/runtime/ExternalValues.java rename to runtime/src/main/java/com/dylibso/chicory/runtime/ImportValues.java index 231ef357d..89ae70885 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/ExternalValues.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/ImportValues.java @@ -4,82 +4,82 @@ 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() { +public class ImportValues { + private static final ImportFunction[] NO_EXTERNAL_FUNCTIONS = new ImportFunction[0]; + private static final ImportGlobal[] NO_EXTERNAL_GLOBALS = new ImportGlobal[0]; + private static final ImportMemory[] NO_EXTERNAL_MEMORIES = new ImportMemory[0]; + private static final ImportTable[] NO_EXTERNAL_TABLES = new ImportTable[0]; + + private final ImportFunction[] functions; + private final ImportGlobal[] globals; + private final ImportMemory[] memories; + private final ImportTable[] tables; + + public ImportValues() { this.functions = NO_EXTERNAL_FUNCTIONS; this.globals = NO_EXTERNAL_GLOBALS; this.memories = NO_EXTERNAL_MEMORIES; this.tables = NO_EXTERNAL_TABLES; } - public ExternalValues(HostFunction[] functions) { + public ImportValues(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) { + public ImportValues(ImportGlobal[] globals) { this.functions = NO_EXTERNAL_FUNCTIONS; this.globals = globals.clone(); this.memories = NO_EXTERNAL_MEMORIES; this.tables = NO_EXTERNAL_TABLES; } - public ExternalValues(ExternalMemory[] memories) { + public ImportValues(ImportMemory[] memories) { this.functions = NO_EXTERNAL_FUNCTIONS; this.globals = NO_EXTERNAL_GLOBALS; this.memories = memories.clone(); this.tables = NO_EXTERNAL_TABLES; } - public ExternalValues(ExternalMemory memory) { + public ImportValues(ImportMemory memory) { this.functions = NO_EXTERNAL_FUNCTIONS; this.globals = NO_EXTERNAL_GLOBALS; - this.memories = new ExternalMemory[] {memory}; + this.memories = new ImportMemory[] {memory}; this.tables = NO_EXTERNAL_TABLES; } - public ExternalValues(ExternalTable[] tables) { + public ImportValues(ImportTable[] 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) { + public ImportValues( + ImportFunction[] functions, + ImportGlobal[] globals, + ImportMemory memory, + ImportTable[] tables) { this.functions = functions.clone(); this.globals = globals.clone(); - this.memories = new ExternalMemory[] {memory}; + this.memories = new ImportMemory[] {memory}; this.tables = tables.clone(); } - public ExternalValues( - ExternalFunction[] functions, - ExternalGlobal[] globals, - ExternalMemory[] memories, - ExternalTable[] tables) { + public ImportValues( + ImportFunction[] functions, + ImportGlobal[] globals, + ImportMemory[] memories, + ImportTable[] tables) { this.functions = functions.clone(); this.globals = globals.clone(); this.memories = memories.clone(); this.tables = tables.clone(); } - public ExternalFunction[] functions() { + public ImportFunction[] functions() { return functions.clone(); } @@ -87,11 +87,11 @@ public int functionCount() { return functions.length; } - public ExternalFunction function(int idx) { + public ImportFunction function(int idx) { return functions[idx]; } - public ExternalGlobal[] globals() { + public ImportGlobal[] globals() { return globals; } @@ -99,11 +99,11 @@ public int globalCount() { return globals.length; } - public ExternalGlobal global(int idx) { + public ImportGlobal global(int idx) { return globals[idx]; } - public ExternalMemory[] memories() { + public ImportMemory[] memories() { return memories; } @@ -111,11 +111,11 @@ public int memoryCount() { return memories.length; } - public ExternalMemory memory(int idx) { + public ImportMemory memory(int idx) { return memories[idx]; } - public ExternalTable[] tables() { + public ImportTable[] tables() { return tables; } @@ -123,7 +123,7 @@ public int tableCount() { return tables.length; } - public ExternalTable table(int idx) { + public ImportTable table(int idx) { return tables[idx]; } @@ -131,24 +131,24 @@ public static Builder builder() { return new Builder(); } - public static ExternalValues empty() { + public static ImportValues empty() { return new Builder().build(); } public static final class Builder { - private List functions; - private List globals; - private List memories; - private List tables; + private List functions; + private List globals; + private List memories; + private List tables; Builder() {} - public Builder withFunctions(List functions) { + public Builder withFunctions(List functions) { this.functions = functions; return this; } - public Builder addFunction(ExternalFunction... function) { + public Builder addFunction(ImportFunction... function) { if (this.functions == null) { this.functions = new ArrayList<>(); } @@ -156,12 +156,12 @@ public Builder addFunction(ExternalFunction... function) { return this; } - public Builder withGlobals(List globals) { + public Builder withGlobals(List globals) { this.globals = globals; return this; } - public Builder addGlobal(ExternalGlobal... global) { + public Builder addGlobal(ImportGlobal... global) { if (this.globals == null) { this.globals = new ArrayList<>(); } @@ -169,12 +169,12 @@ public Builder addGlobal(ExternalGlobal... global) { return this; } - public Builder withMemories(List memories) { + public Builder withMemories(List memories) { this.memories = memories; return this; } - public Builder addMemory(ExternalMemory... memory) { + public Builder addMemory(ImportMemory... memory) { if (this.memories == null) { this.memories = new ArrayList<>(); } @@ -182,12 +182,12 @@ public Builder addMemory(ExternalMemory... memory) { return this; } - public Builder withTables(List tables) { + public Builder withTables(List tables) { this.tables = tables; return this; } - public Builder addTable(ExternalTable... table) { + public Builder addTable(ImportTable... table) { if (this.tables == null) { this.tables = new ArrayList<>(); } @@ -195,22 +195,22 @@ public Builder addTable(ExternalTable... table) { return this; } - public ExternalValues build() { - final ExternalValues externalValues = - new ExternalValues( + public ImportValues build() { + final ImportValues importValues = + new ImportValues( functions == null ? new HostFunction[0] : functions.toArray(new HostFunction[0]), globals == null - ? new ExternalGlobal[0] - : globals.toArray(new ExternalGlobal[0]), + ? new ImportGlobal[0] + : globals.toArray(new ImportGlobal[0]), memories == null - ? new ExternalMemory[0] - : memories.toArray(new ExternalMemory[0]), + ? new ImportMemory[0] + : memories.toArray(new ImportMemory[0]), tables == null - ? new ExternalTable[0] - : tables.toArray(new ExternalTable[0])); - return externalValues; + ? new ImportTable[0] + : tables.toArray(new ImportTable[0])); + return importValues; } } } 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 b6137fe96..003731ef1 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Instance.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Instance.java @@ -57,7 +57,7 @@ public class Instance { private final int importedTablesOffset; private final FunctionType[] types; private final int[] functionTypes; - private final ExternalValues imports; + private final ImportValues imports; private final Table[] roughTables; private final TableInstance[] tables; private final Element[] elements; @@ -75,7 +75,7 @@ public Instance( FunctionBody[] functions, FunctionType[] types, int[] functionTypes, - ExternalValues imports, + ImportValues imports, Table[] tables, Element[] elements, Map exports, @@ -308,7 +308,7 @@ public int functionType(int idx) { return functionTypes[idx]; } - public ExternalValues imports() { + public ImportValues imports() { return imports; } @@ -369,7 +369,7 @@ public static final class Builder { private boolean initialize = true; private boolean start = true; private ExecutionListener listener; - private ExternalValues externalValues; + private ImportValues importValues; private Function machineFactory; private Builder(Module module) { @@ -394,8 +394,8 @@ public Builder withUnsafeExecutionListener(ExecutionListener listener) { return this; } - public Builder withExternalValues(ExternalValues externalValues) { - this.externalValues = externalValues; + public Builder withImportValues(ImportValues importValues) { + this.importValues = importValues; return this; } @@ -404,7 +404,7 @@ public Builder withMachineFactory(Function machineFactory) { return this; } - private void validateExternalFunctionSignature(FunctionImport imprt, ExternalFunction f) { + private void validateExternalFunctionSignature(FunctionImport imprt, ImportFunction f) { var expectedType = module.typeSection().getType(imprt.typeIndex()); if (expectedType.params().size() != f.paramTypes().size() || expectedType.returns().size() != f.returnTypes().size()) { @@ -438,14 +438,14 @@ private void validateExternalFunctionSignature(FunctionImport imprt, ExternalFun } } - private void validateHostGlobalType(GlobalImport i, ExternalGlobal g) { + private void validateHostGlobalType(GlobalImport i, ImportGlobal g) { if (i.type() != g.instance().getType() || i.mutabilityType() != g.instance().getMutabilityType()) { throw new UnlinkableException("incompatible import type"); } } - private void validateHostTableType(TableImport i, ExternalTable t) { + private void validateHostTableType(TableImport i, ImportTable t) { var minExpected = t.table().limits().min(); var maxExpected = t.table().limits().max(); var minCurrent = i.limits().min(); @@ -465,7 +465,7 @@ private void validateHostTableType(TableImport i, ExternalTable t) { } } - private void validateHostMemoryType(MemoryImport i, ExternalMemory m) { + private void validateHostMemoryType(MemoryImport i, ImportMemory 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. @@ -495,7 +495,7 @@ private void validateHostMemoryType(MemoryImport i, ExternalMemory m) { } private void validateNegativeImportType( - String moduleName, String name, ExternalValue[] external) { + String moduleName, String name, ImportValue[] external) { for (var fh : external) { if (fh.module().equals(moduleName) && fh.name().equals(name)) { throw new UnlinkableException("incompatible import type"); @@ -504,33 +504,33 @@ private void validateNegativeImportType( } private void validateNegativeImportType( - String moduleName, String name, ExternalType typ, ExternalValues externalValues) { + String moduleName, String name, ExternalType typ, ImportValues importValues) { switch (typ) { case FUNCTION: - validateNegativeImportType(moduleName, name, externalValues.globals()); - validateNegativeImportType(moduleName, name, externalValues.memories()); - validateNegativeImportType(moduleName, name, externalValues.tables()); + validateNegativeImportType(moduleName, name, importValues.globals()); + validateNegativeImportType(moduleName, name, importValues.memories()); + validateNegativeImportType(moduleName, name, importValues.tables()); break; case GLOBAL: - validateNegativeImportType(moduleName, name, externalValues.functions()); - validateNegativeImportType(moduleName, name, externalValues.memories()); - validateNegativeImportType(moduleName, name, externalValues.tables()); + validateNegativeImportType(moduleName, name, importValues.functions()); + validateNegativeImportType(moduleName, name, importValues.memories()); + validateNegativeImportType(moduleName, name, importValues.tables()); break; case MEMORY: - validateNegativeImportType(moduleName, name, externalValues.functions()); - validateNegativeImportType(moduleName, name, externalValues.globals()); - validateNegativeImportType(moduleName, name, externalValues.tables()); + validateNegativeImportType(moduleName, name, importValues.functions()); + validateNegativeImportType(moduleName, name, importValues.globals()); + validateNegativeImportType(moduleName, name, importValues.tables()); break; case TABLE: - validateNegativeImportType(moduleName, name, externalValues.functions()); - validateNegativeImportType(moduleName, name, externalValues.globals()); - validateNegativeImportType(moduleName, name, externalValues.memories()); + validateNegativeImportType(moduleName, name, importValues.functions()); + validateNegativeImportType(moduleName, name, importValues.globals()); + validateNegativeImportType(moduleName, name, importValues.memories()); break; } } - private ExternalValues mapHostImports( - Import[] imports, ExternalValues externalValues, int memoryCount) { + private ImportValues mapHostImports( + Import[] imports, ImportValues importValues, int memoryCount) { int hostFuncNum = 0; int hostGlobalNum = 0; int hostMemNum = 0; @@ -557,28 +557,28 @@ private ExternalValues mapHostImports( } // TODO: this can probably be refactored ... - var hostFuncs = new ExternalFunction[hostFuncNum]; + var hostFuncs = new ImportFunction[hostFuncNum]; var hostFuncIdx = 0; - var hostGlobals = new ExternalGlobal[hostGlobalNum]; + var hostGlobals = new ImportGlobal[hostGlobalNum]; var hostGlobalIdx = 0; - var hostMems = new ExternalMemory[hostMemNum]; + var hostMems = new ImportMemory[hostMemNum]; var hostMemIdx = 0; - var hostTables = new ExternalTable[hostTableNum]; + var hostTables = new ImportTable[hostTableNum]; var hostTableIdx = 0; int cnt; for (var impIdx = 0; impIdx < imports.length; impIdx++) { var i = imports[impIdx]; var name = i.module() + "." + i.name(); var found = false; - validateNegativeImportType(i.module(), i.name(), i.importType(), externalValues); - Function checkName = - (ExternalValue fh) -> + validateNegativeImportType(i.module(), i.name(), i.importType(), importValues); + Function checkName = + (ImportValue fh) -> i.module().equals(fh.module()) && i.name().equals(fh.name()); switch (i.importType()) { case FUNCTION: - cnt = externalValues.functionCount(); + cnt = importValues.functionCount(); for (int j = 0; j < cnt; j++) { - ExternalFunction f = externalValues.function(j); + ImportFunction f = importValues.function(j); if (checkName.apply(f)) { validateExternalFunctionSignature((FunctionImport) i, f); hostFuncs[hostFuncIdx] = f; @@ -589,9 +589,9 @@ private ExternalValues mapHostImports( hostFuncIdx++; break; case GLOBAL: - cnt = externalValues.globalCount(); + cnt = importValues.globalCount(); for (int j = 0; j < cnt; j++) { - ExternalGlobal g = externalValues.global(j); + ImportGlobal g = importValues.global(j); if (checkName.apply(g)) { validateHostGlobalType((GlobalImport) i, g); hostGlobals[hostGlobalIdx] = g; @@ -602,9 +602,9 @@ private ExternalValues mapHostImports( hostGlobalIdx++; break; case MEMORY: - cnt = externalValues.memoryCount(); + cnt = importValues.memoryCount(); for (int j = 0; j < cnt; j++) { - ExternalMemory m = externalValues.memory(j); + ImportMemory m = importValues.memory(j); if (checkName.apply(m)) { validateHostMemoryType((MemoryImport) i, m); hostMems[hostMemIdx] = m; @@ -615,9 +615,9 @@ private ExternalValues mapHostImports( hostMemIdx++; break; case TABLE: - cnt = externalValues.tableCount(); + cnt = importValues.tableCount(); for (int j = 0; j < cnt; j++) { - ExternalTable t = externalValues.table(j); + ImportTable t = importValues.table(j); if (checkName.apply(t)) { validateHostTableType((TableImport) i, t); hostTables[hostTableIdx] = t; @@ -637,7 +637,7 @@ private ExternalValues mapHostImports( } } - return new ExternalValues(hostFuncs, hostGlobals, hostMems, hostTables); + return new ImportValues(hostFuncs, hostGlobals, hostMems, hostTables); } private Map genExports(ExportSection export) { @@ -687,7 +687,7 @@ public Instance build() { var mappedHostImports = mapHostImports( imports, - requireNonNullElseGet(externalValues, ExternalValues::new), + requireNonNullElseGet(importValues, ImportValues::new), 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 60954f375..35b3843db 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(ExternalFunction... function) { + public Store addFunction(ImportFunction... function) { for (var f : function) { functions.put(new QualifiedName(f.module(), f.name()), f); } @@ -31,7 +31,7 @@ public Store addFunction(ExternalFunction... function) { /** * Add a global to the store. */ - public Store addGlobal(ExternalGlobal... global) { + public Store addGlobal(ImportGlobal... global) { for (var g : global) { globals.put(new QualifiedName(g.module(), g.name()), g); } @@ -41,7 +41,7 @@ public Store addGlobal(ExternalGlobal... global) { /** * Add a memory to the store. */ - public Store addMemory(ExternalMemory... memory) { + public Store addMemory(ImportMemory... memory) { for (var m : memory) { memories.put(new QualifiedName(m.module(), m.name()), m); } @@ -51,7 +51,7 @@ public Store addMemory(ExternalMemory... memory) { /** * Add a table to the store. */ - public Store addTable(ExternalTable... table) { + public Store addTable(ImportTable... table) { for (var t : table) { tables.put(new QualifiedName(t.module(), t.name()), t); } @@ -59,24 +59,24 @@ public Store addTable(ExternalTable... table) { } /** - * Add the contents of a {@link ExternalValues} instance to the store. + * Add the contents of a {@link ImportValues} instance to the store. */ - public Store addExternalValues(ExternalValues externalValues) { - return this.addGlobal(externalValues.globals()) - .addFunction(externalValues.functions()) - .addMemory(externalValues.memories()) - .addTable(externalValues.tables()); + public Store addImportValues(ImportValues importValues) { + return this.addGlobal(importValues.globals()) + .addFunction(importValues.functions()) + .addMemory(importValues.memories()) + .addTable(importValues.tables()); } /** - * Convert the contents of a store to a {@link ExternalValues} instance. + * Convert the contents of a store to a {@link ImportValues} instance. */ - 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])); + public ImportValues toImportValues() { + return new ImportValues( + functions.values().toArray(new ImportFunction[0]), + globals.values().toArray(new ImportGlobal[0]), + memories.values().toArray(new ImportMemory[0]), + tables.values().toArray(new ImportTable[0])); } /** @@ -98,26 +98,26 @@ public Store register(String name, Instance instance) { ExportFunction f = instance.export(exportName); FunctionType ftype = instance.exportType(exportName); this.addFunction( - new ExternalFunction( + new ImportFunction( name, exportName, - (inst, args) -> f.apply(args), ftype.params(), - ftype.returns())); + ftype.returns(), + (inst, args) -> f.apply(args))); break; case TABLE: this.addTable( - new ExternalTable(name, exportName, instance.table(export.index()))); + new ImportTable(name, exportName, instance.table(export.index()))); break; case MEMORY: - this.addMemory(new ExternalMemory(name, exportName, instance.memory())); + this.addMemory(new ImportMemory(name, exportName, instance.memory())); break; case GLOBAL: GlobalInstance g = instance.global(export.index()); - this.addGlobal(new ExternalGlobal(name, exportName, g)); + this.addGlobal(new ImportGlobal(name, exportName, g)); break; } } @@ -128,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) { - ExternalValues externalValues = this.toExternalValues(); - Instance instance = Instance.builder(m).withExternalValues(externalValues).build(); + ImportValues importValues = this.toImportValues(); + Instance instance = Instance.builder(m).withImportValues(importValues).build(); register(name, instance); return instance; } diff --git a/runtime/src/test/java/com/dylibso/chicory/runtime/ExternalValuesTest.java b/runtime/src/test/java/com/dylibso/chicory/runtime/ImportValuesTest.java similarity index 72% rename from runtime/src/test/java/com/dylibso/chicory/runtime/ExternalValuesTest.java rename to runtime/src/test/java/com/dylibso/chicory/runtime/ImportValuesTest.java index 2eedf7c08..02efc1c24 100644 --- a/runtime/src/test/java/com/dylibso/chicory/runtime/ExternalValuesTest.java +++ b/runtime/src/test/java/com/dylibso/chicory/runtime/ImportValuesTest.java @@ -8,13 +8,13 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -class ExternalValuesTest { +class ImportValuesTest { @Nested class Builder { @Test void empty() { - final ExternalValues result = ExternalValues.builder().build(); + final ImportValues result = ImportValues.builder().build(); assertEquals(0, result.functionCount()); assertEquals(0, result.globalCount()); assertEquals(0, result.memoryCount()); @@ -26,8 +26,8 @@ class Function { @Test void withFunctions() { - final ExternalValues result = - ExternalValues.builder() + final ImportValues result = + ImportValues.builder() .withFunctions( Arrays.asList( new HostFunction("module_1", "", null, null, null), @@ -38,8 +38,8 @@ void withFunctions() { @Test void addFunction() { - final ExternalValues result = - ExternalValues.builder() + final ImportValues result = + ImportValues.builder() .addFunction(new HostFunction("module_1", "", null, null, null)) .addFunction(new HostFunction("module_2", "", null, null, null)) .build(); @@ -52,15 +52,15 @@ class Global { @Test void withGlobals() { - final ExternalValues result = - ExternalValues.builder() + final ImportValues result = + ImportValues.builder() .withGlobals( Arrays.asList( - new ExternalGlobal( + new ImportGlobal( "spectest", "global_i32", new GlobalInstance(Value.i32(666))), - new ExternalGlobal( + new ImportGlobal( "spectest", "global_i64", new GlobalInstance(Value.i64(666))))) @@ -70,15 +70,15 @@ void withGlobals() { @Test void addGlobal() { - final ExternalValues result = - ExternalValues.builder() + final ImportValues result = + ImportValues.builder() .addGlobal( - new ExternalGlobal( + new ImportGlobal( "spectest", "global_i32", new GlobalInstance(Value.i32(666)))) .addGlobal( - new ExternalGlobal( + new ImportGlobal( "spectest", "global_i64", new GlobalInstance(Value.i64(666)))) @@ -92,22 +92,22 @@ class Memory { @Test void withMemories() { - final ExternalValues result = - ExternalValues.builder() + final ImportValues result = + ImportValues.builder() .withMemories( Arrays.asList( - new ExternalMemory("spectest", "memory", null), - new ExternalMemory("spectest", "memory_2", null))) + new ImportMemory("spectest", "memory", null), + new ImportMemory("spectest", "memory_2", null))) .build(); assertEquals(2, result.memoryCount()); } @Test void addMemory() { - final ExternalValues result = - ExternalValues.builder() - .addMemory(new ExternalMemory("spectest", "memory", null)) - .addMemory(new ExternalMemory("spectest", "memory_2", null)) + final ImportValues result = + ImportValues.builder() + .addMemory(new ImportMemory("spectest", "memory", null)) + .addMemory(new ImportMemory("spectest", "memory_2", null)) .build(); assertEquals(2, result.memoryCount()); } @@ -118,15 +118,15 @@ class Table { @Test void withTables() { - final ExternalValues result = - ExternalValues.builder() + final ImportValues result = + ImportValues.builder() .withTables( Arrays.asList( - new ExternalTable( + new ImportTable( "spectest", "table", Collections.emptyMap()), - new ExternalTable( + new ImportTable( "spectest", "table_2", Collections.emptyMap()))) @@ -136,13 +136,13 @@ void withTables() { @Test void addMemory() { - final ExternalValues result = - ExternalValues.builder() + final ImportValues result = + ImportValues.builder() .addTable( - new ExternalTable( + new ImportTable( "spectest", "table", Collections.emptyMap())) .addTable( - new ExternalTable( + new ImportTable( "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 e86018cbb..906c2ecaa 100644 --- a/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java +++ b/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java @@ -84,6 +84,8 @@ public void shouldConsoleLogWithString() { new HostFunction( "console", "log", + List.of(ValueType.I32, ValueType.I32), + List.of(), (Instance instance, long... args) -> { // decompiled is: console_log(13, 0); Memory memory = instance.memory(); int len = (int) args[0]; @@ -95,13 +97,11 @@ public void shouldConsoleLogWithString() { } return null; - }, - List.of(ValueType.I32, ValueType.I32), - List.of()); + }); var funcs = new HostFunction[] {func}; var instance = Instance.builder(loadModule("compiled/host-function.wat.wasm")) - .withExternalValues(new ExternalValues(funcs)) + .withImportValues(new ImportValues(funcs)) .build(); var logIt = instance.export("logIt"); logIt.apply(); @@ -138,6 +138,8 @@ public void shouldWorkWithStartFunction() { new HostFunction( "env", "gotit", + List.of(ValueType.I32), + List.of(), (Instance instance, long... args) -> { var val = args[0]; @@ -146,12 +148,10 @@ public void shouldWorkWithStartFunction() { } return null; - }, - List.of(ValueType.I32), - List.of()); + }); var funcs = new HostFunction[] {func}; Instance.builder(loadModule("compiled/start.wat.wasm")) - .withExternalValues(new ExternalValues(funcs)) + .withImportValues(new ImportValues(funcs)) .build(); assertTrue(count.get() > 0); @@ -265,37 +265,37 @@ public void shouldRunMixedImports() { new HostFunction( "env", "cbrt", + List.of(ValueType.I32), + List.of(ValueType.F64), (Instance instance, long... args) -> { var x = args[0]; var cbrt = Math.cbrt((double) x); return new long[] {Double.doubleToRawLongBits(cbrt)}; - }, - List.of(ValueType.I32), - List.of(ValueType.F64)); + }); var logResult = new AtomicReference(null); var logFunc = new HostFunction( "env", "log", + List.of(ValueType.I32, ValueType.F64), + List.of(), (Instance instance, long... args) -> { var logLevel = args[0]; var value = (int) Double.longBitsToDouble(args[1]); logResult.set(logLevel + ": " + value); return null; - }, - List.of(ValueType.I32, ValueType.F64), - List.of()); - var memory = new ExternalMemory("env", "memory", new Memory(new MemoryLimits(1))); + }); + var memory = new ImportMemory("env", "memory", new Memory(new MemoryLimits(1))); var hostImports = - new ExternalValues( + new ImportValues( new HostFunction[] {cbrtFunc, logFunc}, - new ExternalGlobal[0], + new ImportGlobal[0], memory, - new ExternalTable[0]); + new ImportTable[0]); var instance = Instance.builder(loadModule("compiled/mixed-imports.wat.wasm")) - .withExternalValues(hostImports) + .withImportValues(hostImports) .build(); var run = instance.export("main"); 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 c0bb01f30..1ba665789 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().addExternalValues(Spectest.toExternalValues())")); + new NameExpr("new Store().addImportValues(Spectest.toImportValues())")); 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 3a4c4978b..f9ab53840 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.ExternalValues; +import com.dylibso.chicory.runtime.ImportValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.wasi.WasiOptions; import com.dylibso.chicory.wasi.WasiPreview1; @@ -86,10 +86,10 @@ public void process() { .withLogger(logger) .withOpts(wasiOpts.build()) .build()) { - ExternalValues imports = new ExternalValues(wasi.toHostFunctions()); + ImportValues imports = new ImportValues(wasi.toHostFunctions()); Instance.builder(MODULE) - .withExternalValues(imports) + .withImportValues(imports) .withMachineFactory(Wast2JsonModuleMachineFactory::create) .build(); } 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 69c0d760f..0f5a467d5 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.ExternalValues; +import com.dylibso.chicory.runtime.ImportValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.wasi.WasiOptions; import com.dylibso.chicory.wasi.WasiPreview1; @@ -71,10 +71,10 @@ private static byte[] parse(InputStream is, String fileName) { try (var wasi = WasiPreview1.builder().withLogger(logger).withOpts(wasiOpts).build()) { - ExternalValues imports = new ExternalValues(wasi.toHostFunctions()); + ImportValues imports = new ImportValues(wasi.toHostFunctions()); Instance.builder(MODULE) .withMachineFactory(Wat2WasmModuleMachineFactory::create) - .withExternalValues(imports) + .withImportValues(imports) .build(); } diff --git a/wasi/README.md b/wasi/README.md index eadaf29fe..c2d7f70a2 100644 --- a/wasi/README.md +++ b/wasi/README.md @@ -151,8 +151,7 @@ import com.dylibso.chicory.log.SystemLogger; import com.dylibso.chicory.wasi.WasiOptions; import com.dylibso.chicory.wasi.WasiPreview1; import com.dylibso.chicory.wasm.Parser; -import com.dylibso.chicory.runtime.ExternalValues; -import com.dylibso.chicory.runtime.Instance; +import com.dylibso.chicory.runtime.ImportValues; import java.io.File; @@ -162,10 +161,10 @@ var options = WasiOptions.builder().build(); // create our instance of wasip1 var wasi = new WasiPreview1(logger, WasiOptions.builder().build()); // turn those into host functions. Here we could add any other custom definitions we have -var hostFunctions = new ExternalValues(wasi.toHostFunctions()); +var hostFunctions = new ImportValues(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"))).withExternalValues(hostFunctions).build(); +Instance.builder(Parser.parse(new File("hello-wasi.wasm"))).withImportValues(hostFunctions).build(); ``` > **Note**: Take note that we don't explicitly execute the module. The module will run when you instantiate it. This @@ -200,11 +199,11 @@ var fakeStderr = new ByteArrayOutputStream(); var wasiOpts = WasiOptions.builder().withStdout(fakeStdout).withStderr(fakeStderr).withStdin(fakeStdin).build(); var wasi = new WasiPreview1(logger, wasiOpts); -var hostFunctions = new ExternalValues(wasi.toHostFunctions()); +var hostFunctions = new ImportValues(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"))).withExternalValues(hostFunctions).build(); +Instance.builder(Parser.parse(new File("greet-wasi.wasm"))).withImportValues(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 4c241dec7..55b265e71 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.ExternalValues; +import com.dylibso.chicory.runtime.ImportValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.runtime.Memory; import com.dylibso.chicory.runtime.Store; @@ -33,9 +33,9 @@ public void shouldRunWasiModule() { var fakeStdout = new MockPrintStream(); var wasi = new WasiPreview1(this.logger, WasiOptions.builder().withStdout(fakeStdout).build()); - var imports = new ExternalValues(wasi.toHostFunctions()); + var imports = new ImportValues(wasi.toHostFunctions()); Instance.builder(loadModule("compiled/hello-wasi.wat.wasm")) - .withExternalValues(imports) + .withImportValues(imports) .build(); assertEquals(fakeStdout.output().strip(), "hello world"); } @@ -46,9 +46,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 ExternalValues(wasi.toHostFunctions()); + var imports = new ImportValues(wasi.toHostFunctions()); Instance.builder(loadModule("compiled/hello-wasi.rs.wasm")) - .withExternalValues(imports) + .withImportValues(imports) .build(); // run _start and prints Hello, World! assertEquals(expected, stdout.output().strip()); } @@ -60,9 +60,9 @@ public void shouldRunWasiGreetRustModule() { var fakeStdout = new MockPrintStream(); var wasiOpts = WasiOptions.builder().withStdout(fakeStdout).withStdin(fakeStdin).build(); var wasi = new WasiPreview1(this.logger, wasiOpts); - var imports = new ExternalValues(wasi.toHostFunctions()); + var imports = new ImportValues(wasi.toHostFunctions()); Instance.builder(loadModule("compiled/greet-wasi.rs.wasm")) - .withExternalValues(imports) + .withImportValues(imports) .build(); assertEquals(fakeStdout.output().strip(), "Hello, Benjamin!"); } @@ -75,9 +75,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 ExternalValues(wasi.toHostFunctions()); + var imports = new ImportValues(wasi.toHostFunctions()); Instance.builder(loadModule("compiled/javy-demo.js.javy.wasm")) - .withExternalValues(imports) + .withImportValues(imports) .build(); assertEquals(fakeStdout.output(), "{\"foo\":3,\"newBar\":\"baz!\"}"); @@ -100,7 +100,7 @@ public void shouldUseQuickJsProvider() { var wasi = new WasiPreview1(logger, wasiOpts); var quickjs = Instance.builder(loadModule("compiled/quickjs-provider.javy-dynamic.wasm")) - .withExternalValues(new ExternalValues(wasi.toHostFunctions())) + .withImportValues(new ImportValues(wasi.toHostFunctions())) .build(); var greetingMsg = "Hello QuickJS!"; @@ -144,14 +144,14 @@ public void shouldUseDynamicallyLinkedJavyModules() { var wasi = new WasiPreview1(logger, wasiOpts); var quickjs = Instance.builder(loadModule("compiled/quickjs-provider.javy-dynamic.wasm")) - .withExternalValues(new ExternalValues(wasi.toHostFunctions())) + .withImportValues(new ImportValues(wasi.toHostFunctions())) .build(); var store = new Store(); store.register("javy_quickjs_provider_v1", quickjs); Instance.builder(loadModule("compiled/hello-world.js.javy-dynamic.wasm")) - .withExternalValues(store.toExternalValues()) + .withImportValues(store.toImportValues()) .build(); // stderr? @@ -162,9 +162,9 @@ public void shouldUseDynamicallyLinkedJavyModules() { public void shouldRunTinyGoModule() { var wasiOpts = WasiOptions.builder().build(); var wasi = new WasiPreview1(this.logger, wasiOpts); - var imports = new ExternalValues(wasi.toHostFunctions()); + var imports = new ImportValues(wasi.toHostFunctions()); var module = loadModule("compiled/sum.go.tiny.wasm"); - var instance = Instance.builder(module).withExternalValues(imports).build(); + var instance = Instance.builder(module).withImportValues(imports).build(); var sum = instance.export("add"); var result = sum.apply(20, 22)[0]; @@ -176,12 +176,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 ExternalValues(wasi.toHostFunctions()); + var imports = new ImportValues(wasi.toHostFunctions()); var module = loadModule("compiled/main.go.wasm"); var exit = assertThrows( WasiExitException.class, - () -> Instance.builder(module).withExternalValues(imports).build()); + () -> Instance.builder(module).withImportValues(imports).build()); assertEquals(0, exit.exitCode()); assertEquals("Hello, WebAssembly!\n", fakeStdout.output()); } @@ -198,10 +198,10 @@ public void shouldRunWasiDemoDotnetModule() throws Exception { .withArguments(List.of("")) .build(); var wasi = new WasiPreview1(this.logger, wasiOpts); - var imports = new ExternalValues(wasi.toHostFunctions()); + var imports = new ImportValues(wasi.toHostFunctions()); var module = loadModule("compiled/basic.dotnet.wasm"); - Instance.builder(module).withExternalValues(imports).build(); + Instance.builder(module).withImportValues(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 16c3e1d99..ccb3b0397 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.ExternalValues; +import com.dylibso.chicory.runtime.ImportValues; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.wasm.Parser; import com.google.common.jimfs.Configuration; @@ -85,7 +85,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)) - .withExternalValues(new ExternalValues(wasi.toHostFunctions())) + .withImportValues(new ImportValues(wasi.toHostFunctions())) .build(); } catch (WasiExitException e) { return e.exitCode();