From cdb8f4280e023fe79bb3e6365b72a408db2ce482 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Mon, 2 Dec 2024 17:37:11 +0000 Subject: [PATCH] finalizing generation --- .../java/chicory/test/WithImportsTest.java | 84 +++++++++++-------- .../processor/AbstractModuleProcessor.java | 3 +- .../processor/WasmModuleProcessor.java | 36 +++++++- 3 files changed, 81 insertions(+), 42 deletions(-) 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 d9b6128d9..4d14c8fe2 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 @@ -1,48 +1,58 @@ package chicory.test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.dylibso.chicory.experimental.hostmodule.annotations.HostModule; +import com.dylibso.chicory.experimental.hostmodule.annotations.WasmExport; import com.dylibso.chicory.experimental.hostmodule.annotations.WasmModuleInterface; +import com.dylibso.chicory.runtime.ImportValues; +import com.dylibso.chicory.runtime.Instance; +import com.dylibso.chicory.wasm.Parser; import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.Test; class WithImportsTest { public final AtomicInteger count = new AtomicInteger(); + @HostModule("console") @WasmModuleInterface("host-function.wat.wasm") - class TestModule {} - - // @HostModule("console") - // class TestModule { - // private static final String EXPECTED = "Hello, World!"; - // private final Instance instance; - // - // public TestModule() { - // instance = - // Instance.builder( - // Parser.parse( - // WithImportsTest.class.getResourceAsStream( - // "/compiled/host-function.wat.wasm"))) - // .withImportValues( - // ImportValues.builder() - // .addFunction( - // - // TestModule_ModuleFactory.toHostFunctions(this)) - // .build()) - // .build(); - // } - // - // public void logIt() { - // instance.export("logIt").apply(); - // } - // - // @WasmExport - // public void log(int len, int offset) { - // var message = instance.memory().readString(offset, len); - // - // if (EXPECTED.equals(message)) { - // count.incrementAndGet(); - // } - // } - // } + class TestModule + implements TestModule_ModuleExports, TestModule_ModuleImports, TestModule_Console { + private static final String EXPECTED = "Hello, World!"; + private final Instance instance; + + public TestModule() { + var module = + Parser.parse( + WithImportsTest.class.getResourceAsStream( + "/compiled/host-function.wat.wasm")); + var imports = + ImportValues.builder() + .addFunction(TestModule_ModuleFactory.toHostFunctions(this)) + .build(); + instance = Instance.builder(module).withImportValues(imports).build(); + } + + @Override + public Instance instance() { + return instance; + } + + @Override + public TestModule_Console console() { + return this; + } + + @Override + @WasmExport + public void log(int len, int offset) { + var message = instance.memory().readString(offset, len); + + if (EXPECTED.equals(message)) { + count.incrementAndGet(); + } + } + } @Test public void withImportsModule() { @@ -50,9 +60,9 @@ public void withImportsModule() { var withImportsModule = new TestModule(); // Act - // withImportsModule.logIt(); + withImportsModule.logIt(); // Assert - // assertEquals(10, count.get()); + assertEquals(10, count.get()); } } diff --git a/host-module/processor/src/main/java/com/dylibso/chicory/experimental/hostmodule/processor/AbstractModuleProcessor.java b/host-module/processor/src/main/java/com/dylibso/chicory/experimental/hostmodule/processor/AbstractModuleProcessor.java index 204ef006e..5c82d9be1 100644 --- a/host-module/processor/src/main/java/com/dylibso/chicory/experimental/hostmodule/processor/AbstractModuleProcessor.java +++ b/host-module/processor/src/main/java/com/dylibso/chicory/experimental/hostmodule/processor/AbstractModuleProcessor.java @@ -49,7 +49,8 @@ static String camelCaseToSnakeCase(String name) { static String snakeCaseToCamelCase(String name, boolean className) { var sb = new StringBuilder(); var toUppercase = className; - for (var c : name.toCharArray()) { + for (int i = 0; i < name.length(); i++) { + var c = name.charAt(i); if (c == '_') { toUppercase = true; } else if (toUppercase) { diff --git a/host-module/processor/src/main/java/com/dylibso/chicory/experimental/hostmodule/processor/WasmModuleProcessor.java b/host-module/processor/src/main/java/com/dylibso/chicory/experimental/hostmodule/processor/WasmModuleProcessor.java index e05509137..3b15dc722 100644 --- a/host-module/processor/src/main/java/com/dylibso/chicory/experimental/hostmodule/processor/WasmModuleProcessor.java +++ b/host-module/processor/src/main/java/com/dylibso/chicory/experimental/hostmodule/processor/WasmModuleProcessor.java @@ -173,8 +173,24 @@ private void processModule(TypeElement type) throws URISyntaxException, IOExcept // generate module exports for (int i = 0; i < module.exportSection().exportCount(); i++) { var export = module.exportSection().getExport(i); - if (export.exportType() != ExternalType.FUNCTION) { - // TODO: implement support for Memories, Tables, Globals! + + if (export.exportType() == ExternalType.MEMORY) { + var exportMethod = + exportsInterface.addMethod( + snakeCaseToCamelCase(export.name(), false), + Modifier.Keyword.DEFAULT); + + exportsCu.addImport("com.dylibso.chicory.runtime.Memory"); + exportMethod.setType("Memory"); + exportMethod + .createBody() + .addStatement( + new ReturnStmt( + new MethodCallExpr( + new MethodCallExpr("instance"), "memory"))); + continue; + } else if (export.exportType() != ExternalType.FUNCTION) { + // TODO: implement support for Tables, Globals! continue; } var funcType = @@ -246,7 +262,7 @@ private void processModule(TypeElement type) throws URISyntaxException, IOExcept methodBody.addStatement( new AssignExpr( new VariableDeclarationExpr(parseType("long"), "result"), - new ArrayAccessExpr(exportCallHandle, new IntegerLiteralExpr(0)), + new ArrayAccessExpr(exportCallHandle, new IntegerLiteralExpr("0")), AssignExpr.Operator.ASSIGN)); methodBody.addStatement( new ReturnStmt( @@ -279,7 +295,9 @@ private void processModule(TypeElement type) throws URISyntaxException, IOExcept Map> importedModules = new HashMap<>(); for (int i = 0; i < module.importSection().importCount(); i++) { var imprt = module.importSection().getImport(i); - if (imprt.importType() == ExternalType.FUNCTION) { + + if (imprt.importType() == ExternalType.FUNCTION + || imprt.importType() == ExternalType.MEMORY) { importedModules.computeIfAbsent(imprt.module(), ignored -> new ArrayList<>()); importedModules.computeIfPresent( imprt.module(), @@ -307,6 +325,16 @@ private void processModule(TypeElement type) throws URISyntaxException, IOExcept writableClasses.put(prefix + importClassName, cu); for (var importedFun : imprt.getValue()) { + if (importedFun.importType() == ExternalType.MEMORY) { + var importMemoryMethod = + importInterface.addMethod( + snakeCaseToCamelCase(importedFun.name(), false)); + cu.addImport("com.dylibso.chicory.runtime.Memory"); + importMemoryMethod.setType("Memory"); + importMemoryMethod.removeBody(); + continue; + } + var importType = module.typeSection() .getType((((FunctionImport) importedFun).typeIndex()));