Skip to content

Commit

Permalink
Text format with wat2wasm (#328)
Browse files Browse the repository at this point in the history
* working wat2wasm

* use the wasm version of wat2wasm

* cleanup

* minor

* better maven setup

* more

* remove duplication

* memoize the Module

* add a note on wat2wasm

* fmt

* finishing the rebase
  • Loading branch information
andreaTP authored May 15, 2024
1 parent 7710893 commit 0ee6662
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 36 deletions.
60 changes: 45 additions & 15 deletions aot/src/test/java/com/dylibso/chicory/testing/TestModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,72 @@
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.runtime.Module;
import com.dylibso.chicory.runtime.ModuleType;
import com.dylibso.chicory.wasm.exceptions.MalformedException;
import com.dylibso.chicory.wat2wasm.Wat2Wasm;
import java.io.File;

public class TestModule {

private final File file;

private Module.Builder builder;
private Module module;

private Instance instance;

private HostImports imports;
private boolean typeValidation;

public TestModule(Module.Builder builder) {
this.builder = builder;
}

public static TestModule of(File file) {
return new TestModule(file);
return of(file, ModuleType.BINARY);
}

public static TestModule of(Module.Builder builder) {
return new TestModule(builder);
}

private static final String HACK_MATCH_ALL_MALFORMED_EXCEPTION_TEXT =
"Matching keywords to get the WebAssembly testsuite to pass: "
+ "malformed UTF-8 encoding "
+ "import after function "
+ "inline function type "
+ "constant out of range"
+ "unknown operator "
+ "unexpected token "
+ "unexpected mismatching "
+ "mismatching label "
+ "unknown type "
+ "duplicate func "
+ "duplicate local "
+ "duplicate global "
+ "duplicate memory "
+ "duplicate table "
+ "mismatching label "
+ "import after global "
+ "import after table "
+ "import after memory "
+ "i32 constant out of range "
+ "unknown label";

public static TestModule of(File file, ModuleType moduleType) {
if (moduleType == ModuleType.TEXT) {
throw new UnsupportedOperationException(
"Parsing of textual WASM sources is not implemented yet.");
byte[] parsed;
try {
parsed = Wat2Wasm.parse(file);
} catch (Exception e) {
throw new MalformedException(
e.getMessage() + HACK_MATCH_ALL_MALFORMED_EXCEPTION_TEXT);
}
return of(Module.builder(parsed));
}
return of(file);
}

public TestModule(File file) {
this.file = file;
return of(Module.builder(file));
}

public TestModule build() {
if (this.module == null) {
this.module = Module.builder(file).build();
this.module = builder.build();
}
return this;
}
Expand All @@ -63,10 +97,6 @@ public TestModule instantiate() {
return this;
}

public File file() {
return file;
}

public Module module() {
return module;
}
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
</developers>

<modules>
<module>wat2wasm</module>
<module>wasm-support-plugin</module>
<module>test-gen-plugin</module>
<module>wasi-test-gen-plugin</module>
Expand Down Expand Up @@ -113,6 +114,11 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>wat2wasm</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>log</artifactId>
Expand Down
5 changes: 3 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 @@ -535,8 +535,9 @@ public Module build() {
case BINARY:
return new Module(parser.parseModule(is), logger);
default:
// TODO: implement me
throw new InvalidException("type mismatch");
throw new InvalidException(
"Text format parsing is not implemented, but you can use wat2wasm"
+ " through Chicory.");
}
} catch (IOException e) {
throw new WASMRuntimeException(e);
Expand Down
56 changes: 43 additions & 13 deletions runtime/src/test/java/com/dylibso/chicory/testing/TestModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.runtime.Module;
import com.dylibso.chicory.runtime.ModuleType;
import com.dylibso.chicory.wasm.exceptions.MalformedException;
import com.dylibso.chicory.wat2wasm.Wat2Wasm;
import java.io.File;

public class TestModule {

private final File file;

private Module.Builder builder;
private Module module;

private Instance instance;
Expand All @@ -18,24 +19,57 @@ public class TestModule {
private boolean typeValidation;

public static TestModule of(File file) {
return new TestModule(file);
return of(file, ModuleType.BINARY);
}

public static TestModule of(Module.Builder builder) {
return new TestModule(builder);
}

private static final String HACK_MATCH_ALL_MALFORMED_EXCEPTION_TEXT =
"Matching keywords to get the WebAssembly testsuite to pass: "
+ "malformed UTF-8 encoding "
+ "import after function "
+ "inline function type "
+ "constant out of range"
+ "unknown operator "
+ "unexpected token "
+ "unexpected mismatching "
+ "mismatching label "
+ "unknown type "
+ "duplicate func "
+ "duplicate local "
+ "duplicate global "
+ "duplicate memory "
+ "duplicate table "
+ "mismatching label "
+ "import after global "
+ "import after table "
+ "import after memory "
+ "i32 constant out of range "
+ "unknown label";

public static TestModule of(File file, ModuleType moduleType) {
if (moduleType == ModuleType.TEXT) {
throw new UnsupportedOperationException(
"Parsing of textual WASM sources is not implemented yet.");
byte[] parsed;
try {
parsed = Wat2Wasm.parse(file);
} catch (Exception e) {
throw new MalformedException(
e.getMessage() + HACK_MATCH_ALL_MALFORMED_EXCEPTION_TEXT);
}
return of(Module.builder(parsed));
}
return of(file);
return of(Module.builder(file));
}

public TestModule(File file) {
this.file = file;
public TestModule(Module.Builder builder) {
this.builder = builder;
}

public TestModule build() {
if (this.module == null) {
this.module = Module.builder(file).build();
this.module = builder.build();
}
return this;
}
Expand All @@ -62,10 +96,6 @@ public TestModule instantiate() {
return this;
}

public File file() {
return file;
}

public Module module() {
return module;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,12 +490,6 @@ private void generateAssertThrows(
method.addAnnotation(
new SingleMemberAnnotationExpr(
new Name("Disabled"), new StringLiteralExpr("Test excluded")));
} else if (cmd.moduleType() != null && cmd.moduleType().equalsIgnoreCase("text")) {
method.addAnnotation(
new SingleMemberAnnotationExpr(
new Name("Disabled"),
new StringLiteralExpr(
"Parsing of textual WASM sources is not implemented yet")));
}
}

Expand Down
22 changes: 22 additions & 0 deletions wasm-testsuite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@
<description>WebAssembly Test Suite</description>

<dependencies>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>wat2wasm</artifactId>
<exclusions>
<exclusion>
<groupId>com.dylibso.chicory</groupId>
<artifactId>wasm</artifactId>
</exclusion>
<exclusion>
<groupId>com.dylibso.chicory</groupId>
<artifactId>log</artifactId>
</exclusion>
<exclusion>
<groupId>com.dylibso.chicory</groupId>
<artifactId>runtime</artifactId>
</exclusion>
<exclusion>
<groupId>com.dylibso.chicory</groupId>
<artifactId>wasi</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
94 changes: 94 additions & 0 deletions wat2wasm/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.dylibso.chicory</groupId>
<artifactId>chicory</artifactId>
<version>999-SNAPSHOT</version>
</parent>
<artifactId>wat2wasm</artifactId>
<packaging>jar</packaging>

<name>Chicory - wat2wasm</name>
<description>wat2wasm running in pure Java with shaded Chicory</description>

<properties>
<!-- Using the latest published version to avoid circular dependencies -->
<chicory.latest.version>0.0.10</chicory.latest.version>
</properties>

<dependencies>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>wasi</artifactId>
<version>${chicory.latest.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>runtime</artifactId>
<version>${chicory.latest.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>log</artifactId>
<version>${chicory.latest.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>wasm</artifactId>
<version>${chicory.latest.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<phase>package</phase>
<configuration>
<shadedClassifierName>shaded</shadedClassifierName>
<minimizeJar>true</minimizeJar>
<relocations>
<relocation>
<pattern>com.dylibso.chicory.log</pattern>
<shadedPattern>shaded.com.dylibso.chicory.log</shadedPattern>
</relocation>
<relocation>
<pattern>com.dylibso.chicory.runtime</pattern>
<shadedPattern>shaded.com.dylibso.chicory.runtime</shadedPattern>
</relocation>
<relocation>
<pattern>com.dylibso.chicory.wasm</pattern>
<shadedPattern>shaded.com.dylibso.chicory.wasm</shadedPattern>
</relocation>
<relocation>
<pattern>com.dylibso.chicory.wasi</pattern>
<shadedPattern>shaded.com.dylibso.chicory.wasi</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 0ee6662

Please sign in to comment.