Skip to content

Commit

Permalink
Refactor HostFunction constructor signature
Browse files Browse the repository at this point in the history
Signed-off-by: Edoardo Vacchi <[email protected]>
  • Loading branch information
evacchi committed Sep 17, 2024
1 parent b08c3f8 commit e774e26
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 79 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,15 @@ 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();
var message = instance.memory().readString(offset, len);
println(message);
return null;
},
"console",
"log",
List.of(ValueType.I32, ValueType.I32),
List.of());
```
Expand Down
22 changes: 11 additions & 11 deletions aot-tests/src/test/java/com/dylibso/chicory/testing/Spectest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,33 @@ private Spectest() {}
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())
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,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
Expand Down
12 changes: 3 additions & 9 deletions function-processor/src/test/resources/BasicMathGenerated.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
),
Expand Down
8 changes: 2 additions & 6 deletions function-processor/src/test/resources/NestedGenerated.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
),
Expand Down
8 changes: 2 additions & 6 deletions function-processor/src/test/resources/NoPackageGenerated.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
),
Expand Down
16 changes: 4 additions & 12 deletions function-processor/src/test/resources/SimpleGenerated.java
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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()
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,33 @@ private Spectest() {}
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())
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class ExternalFunction implements ExternalValue {
private final List<ValueType> returnTypes;

public ExternalFunction(
WasmFunctionHandle handle,
String moduleName,
String fieldName,
WasmFunctionHandle handle,
List<ValueType> paramTypes,
List<ValueType> returnTypes) {
this.handle = handle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
* 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<ValueType> paramTypes,
List<ValueType> returnTypes) {
super(handle, moduleName, fieldName, paramTypes, returnTypes);
super(moduleName, fieldName, handle, paramTypes, returnTypes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public Store register(String name, Instance instance) {
FunctionType ftype = instance.exportType(exportName);
this.addFunction(
new ExternalFunction(
(inst, args) -> f.apply(args),
name,
exportName,
(inst, args) -> f.apply(args),
ftype.params(),
ftype.returns()));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ void withFunctions() {
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());
}
Expand All @@ -40,8 +40,8 @@ void withFunctions() {
void addFunction() {
final ExternalValues result =
ExternalValues.builder()
.addFunction(new HostFunction(null, "module_1", "", null, null))
.addFunction(new HostFunction(null, "module_2", "", null, null))
.addFunction(new HostFunction("module_1", "", null, null, null))
.addFunction(new HostFunction("module_2", "", null, null, null))
.build();
assertEquals(2, result.functionCount());
}
Expand Down
Loading

0 comments on commit e774e26

Please sign in to comment.