Skip to content

Commit

Permalink
finalizing generation
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Dec 2, 2024
1 parent 7d0bbc9 commit cdb8f42
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,58 +1,68 @@
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() {
// Arrange
var withImportsModule = new TestModule();

// Act
// withImportsModule.logIt();
withImportsModule.logIt();

// Assert
// assertEquals(10, count.get());
assertEquals(10, count.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -279,7 +295,9 @@ private void processModule(TypeElement type) throws URISyntaxException, IOExcept
Map<String, List<Import>> 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(),
Expand Down Expand Up @@ -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()));
Expand Down

0 comments on commit cdb8f42

Please sign in to comment.