Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor review of the public API #48

Merged
merged 1 commit into from
Oct 29, 2023
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
11 changes: 9 additions & 2 deletions runtime/src/main/java/com/dylibso/chicory/runtime/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.dylibso.chicory.wasm.exceptions.ChicoryException;
import com.dylibso.chicory.wasm.exceptions.InvalidException;
import com.dylibso.chicory.wasm.types.*;
import java.io.File;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.HashMap;

public class Module {
Expand All @@ -13,7 +15,7 @@ public class Module {

private HashMap<String, Export> exports;

public static Module build(String wasmFile) {
public static Module build(File wasmFile) {
var parser = new Parser(wasmFile);
return new Module(parser.parseModule());
}
Expand All @@ -23,7 +25,12 @@ public static Module build(InputStream inputWasmFile) {
return new Module(parser.parseModule());
}

public static Module build(String wasmFile, ModuleType type) {
public static Module build(ByteBuffer buffer) {
var parser = new Parser(buffer);
return new Module(parser.parseModule());
}

public static Module build(File wasmFile, ModuleType type) {
switch (type) {
case TEXT:
return build(wasmFile);
Expand Down
38 changes: 26 additions & 12 deletions runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.dylibso.chicory.wasm.types.Value;
import com.dylibso.chicory.wasm.types.ValueType;
import java.io.File;
import java.util.List;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -38,7 +39,7 @@ public class ModuleTest {
*/
@Test
public void shouldWorkFactorial() {
var module = Module.build("src/test/resources/wasm/iterfact.wat.wasm");
var module = Module.build(new File("src/test/resources/wasm/iterfact.wat.wasm"));
var instance = module.instantiate();
var iterFact = instance.getExport("iterFact");
var result = iterFact.apply(Value.i32(5))[0];
Expand All @@ -47,7 +48,8 @@ public void shouldWorkFactorial() {

@Test
public void shouldSupportBrTable() {
var instance = Module.build("src/test/resources/wasm/br_table.wat.wasm").instantiate();
var instance =
Module.build(new File("src/test/resources/wasm/br_table.wat.wasm")).instantiate();
var switchLike = instance.getExport("switch_like");
var result = switchLike.apply(Value.i32(0))[0];
assertEquals(102, result.asInt());
Expand All @@ -67,7 +69,8 @@ public void shouldSupportBrTable() {

@Test
public void shouldExerciseBranches() {
var module = Module.build("src/test/resources/wasm/branching.wat.wasm").instantiate();
var module =
Module.build(new File("src/test/resources/wasm/branching.wat.wasm")).instantiate();
var foo = module.getExport("foo");

var result = foo.apply(Value.i32(0))[0];
Expand Down Expand Up @@ -100,15 +103,17 @@ public void shouldConsoleLogWithString() {
List.of());
var funcs = new HostFunction[] {func};
var instance =
Module.build("src/test/resources/wasm/host-function.wat.wasm").instantiate(funcs);
Module.build(new File("src/test/resources/wasm/host-function.wat.wasm"))
.instantiate(funcs);
var logIt = instance.getExport("logIt");
logIt.apply();
assertEquals(10, printer.times());
}

@Test
public void shouldComputeFactorial() {
var module = Module.build("src/test/resources/wasm/iterfact.wat.wasm").instantiate();
var module =
Module.build(new File("src/test/resources/wasm/iterfact.wat.wasm")).instantiate();
var iterFact = module.getExport("iterFact");

// don't make this too big we will overflow 32 bits
Expand Down Expand Up @@ -142,30 +147,35 @@ public void shouldWorkWithStartFunction() {
List.of(ValueType.I32),
List.of());
var funcs = new HostFunction[] {func};
var module = Module.build("src/test/resources/wasm/start.wat.wasm").instantiate(funcs);
var module =
Module.build(new File("src/test/resources/wasm/start.wat.wasm")).instantiate(funcs);
var start = module.getExport("_start");
start.apply();
assertTrue(printer.times() > 0);
}

@Test
public void shouldTrapOnUnreachable() {
var instance = Module.build("src/test/resources/wasm/trap.wat.wasm").instantiate();
var instance =
Module.build(new File("src/test/resources/wasm/trap.wat.wasm")).instantiate();
var start = instance.getExport("_start");
assertThrows(TrapException.class, start::apply);
}

@Test
public void shouldSupportGlobals() {
var instance = Module.build("src/test/resources/wasm/globals.wat.wasm").instantiate();
var instance =
Module.build(new File("src/test/resources/wasm/globals.wat.wasm")).instantiate();
var doit = instance.getExport("doit");
var result = doit.apply(Value.i32(32))[0];
assertEquals(42, result.asInt());
}

@Test
public void shouldCountVowels() {
var instance = Module.build("src/test/resources/wasm/count_vowels.rs.wasm").instantiate();
var instance =
Module.build(new File("src/test/resources/wasm/count_vowels.rs.wasm"))
.instantiate();
var alloc = instance.getExport("alloc");
var dealloc = instance.getExport("dealloc");
var countVowels = instance.getExport("count_vowels");
Expand All @@ -182,7 +192,7 @@ public void shouldCountVowels() {
@Test
public void shouldRunBasicCProgram() {
// check with: wasmtime src/test/resources/wasm/basic.c.wasm --invoke run
var instance = Module.build("src/test/resources/wasm/basic.c.wasm").instantiate();
var instance = Module.build(new File("src/test/resources/wasm/basic.c.wasm")).instantiate();
var run = instance.getExport("run");
var result = run.apply()[0];
assertEquals(42, result.asInt());
Expand Down Expand Up @@ -210,7 +220,8 @@ public void shouldRunBasicCProgram() {

@Test
public void shouldWorkWithMemoryOps() {
var instance = Module.build("src/test/resources/wasm/memory.wat.wasm").instantiate();
var instance =
Module.build(new File("src/test/resources/wasm/memory.wat.wasm")).instantiate();
var run = instance.getExport("run32");
var results = run.apply(Value.i32(42));
var result = results[0];
Expand Down Expand Up @@ -239,7 +250,10 @@ public void shouldWorkWithMemoryOps() {
public void shouldRunKitchenSink() {
// check with: wasmtime src/test/resources/wasm/kitchensink.wat.wasm --invoke
// run 100
var instance = Module.build("src/test/resources/wasm/kitchensink.wat.wasm").instantiate();
var instance =
Module.build(new File("src/test/resources/wasm/kitchensink.wat.wasm"))
.instantiate();

var run = instance.getExport("run");
assertEquals(6, run.apply(Value.i32(100))[0].asInt());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public CompilationUnit generate(

// all the imports
// junit imports
cu.addImport("java.io.File");
cu.addImport("org.junit.jupiter.api.Disabled");
cu.addImport("org.junit.jupiter.api.Test");
if (ordered) {
Expand Down Expand Up @@ -313,7 +314,11 @@ private Expression generateModuleInstantiation(Command cmd, File folder) {
additionalParam = ", ModuleType." + cmd.getModuleType().toUpperCase();
}
return new NameExpr(
"Module.build(\"" + relativeFile + "\"" + additionalParam + ").instantiate()");
"Module.build(new File(\""
+ relativeFile
+ "\")"
+ additionalParam
+ ").instantiate()");
}

private List<Expression> generateAssertThrows(Command cmd, File wasmFilesFolder) {
Expand Down
35 changes: 21 additions & 14 deletions wasm/src/main/java/com/dylibso/chicory/wasm/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@
import com.dylibso.chicory.wasm.types.TypeSection;
import com.dylibso.chicory.wasm.types.Value;
import com.dylibso.chicory.wasm.types.ValueType;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Stack;
Expand All @@ -62,27 +64,32 @@ public final class Parser {

private final BitSet includeSections;

public Parser(String filePath) {
this(() -> inputStreamFromFile(filePath), new BitSet());
}

public Parser(InputStream inputStream) {
this(() -> inputStream, new BitSet());
}

public Parser(ByteBuffer buffer) {
this(() -> new ByteArrayInputStream(buffer.array()), new BitSet());
}

public Parser(File file) {
this(
() -> {
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException(
"File not found at path: " + file.getPath(), e);
}
},
new BitSet());
}

public Parser(Supplier<InputStream> input, BitSet includeSections) {
this.input = requireNonNull(input, "input");
this.includeSections = requireNonNull(includeSections, "includeSections");
}

private static InputStream inputStreamFromFile(String location) {
try {
return Files.newInputStream(Path.of(location));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private ByteBuffer readByteBuffer() {
try {
var buffer = ByteBuffer.wrap(readBytesFromInput());
Expand Down
16 changes: 8 additions & 8 deletions wasm/src/test/java/com/dylibso/chicory/wasm/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ParserTest {

@Test
public void shouldParseFile() {
var parser = new Parser("src/test/resources/wasm/start.wat.wasm");
var parser = new Parser(new File("src/test/resources/wasm/start.wat.wasm"));
var module = parser.parseModule();

// check types section
Expand Down Expand Up @@ -79,7 +79,7 @@ public void shouldParseFile() {

@Test
public void shouldParseIterfact() {
var parser = new Parser("src/test/resources/wasm/iterfact.wat.wasm");
var parser = new Parser(new File("src/test/resources/wasm/iterfact.wat.wasm"));
var module = parser.parseModule();

// check types section
Expand Down Expand Up @@ -110,14 +110,14 @@ public void shouldParseAllFiles() {
File dir = new File("src/test/resources/wasm/");
File[] files = dir.listFiles((dir1, name) -> name.toLowerCase().endsWith(".wasm"));
for (var f : files) {
var parser = new Parser(f.getPath());
var parser = new Parser(f);
var module = parser.parseModule();
}
}

@Test
public void shouldSupportCustomListener() {
var parser = new Parser("src/test/resources/wasm/code.wasm");
var parser = new Parser(new File("src/test/resources/wasm/code.wasm"));
parser.includeSection(SectionId.CUSTOM);
parser.parse(
s -> {
Expand All @@ -143,7 +143,7 @@ public void shouldSupportCustomListener() {

@Test
public void shouldParseFloats() {
var parser = new Parser("src/test/resources/wasm/float.wat.wasm");
var parser = new Parser(new File("src/test/resources/wasm/float.wat.wasm"));
var module = parser.parseModule();
var codeSection = module.getCodeSection();
var fbody = codeSection.getFunctionBodies()[0];
Expand All @@ -155,7 +155,7 @@ public void shouldParseFloats() {

@Test
public void shouldProperlyParseSignedValue() {
var parser = new Parser("src/test/resources/wasm/i32.wat.wasm");
var parser = new Parser(new File("src/test/resources/wasm/i32.wat.wasm"));
var module = parser.parseModule();
var codeSection = module.getCodeSection();
var fbody = codeSection.getFunctionBodies()[0];
Expand All @@ -177,7 +177,7 @@ public void shouldProperlyParseSignedValue() {

@Test
public void shouldParseLocalDefinitions() {
var parser = new Parser("src/test/resources/wasm/define-locals.wat.wasm");
var parser = new Parser(new File("src/test/resources/wasm/define-locals.wat.wasm"));
var module = parser.parseModule();
var codeSection = module.getCodeSection();
var fbody = codeSection.getFunctionBodies()[0];
Expand All @@ -187,7 +187,7 @@ public void shouldParseLocalDefinitions() {

@Test
public void shouldParseNamesSection() {
var parser = new Parser("src/test/resources/wasm/count_vowels.rs.wasm");
var parser = new Parser(new File("src/test/resources/wasm/count_vowels.rs.wasm"));
var module = parser.parseModule();
var nameSec = module.getNameSection();
assertEquals(nameSec.getFunctionNames().size(), 94);
Expand Down