Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions aot-tests/src/test/java/com/dylibso/chicory/testing/Spectest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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))))
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
8 changes: 4 additions & 4 deletions cli/src/main/java/com/dylibso/chicory/cli/Cli.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions docs/docs/usage/host-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions docs/docs/usage/linking.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```
-->

Expand Down Expand Up @@ -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);
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
56 changes: 28 additions & 28 deletions host-module/processor/src/test/resources/BasicMathGenerated.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}) };
}
}
38 changes: 19 additions & 19 deletions host-module/processor/src/test/resources/NestedGenerated.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}) };
}
}
Loading
Loading