Skip to content

Commit

Permalink
test wasm/README and update all the docs
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Aug 13, 2024
1 parent 02844ce commit 04aaa54
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 30 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ jobs:
script: aot/README.md
env:
JBANG_REPO: "${{ github.workspace }}/repository"
# Test WASM Readme
- name: jbang
uses: jbangdev/jbang-action@9f8c55e0a2b6b297711162b20c209c5e07076e59 # tag=v0.117.1
with:
script: wasm/README.md
env:
JBANG_REPO: "${{ github.workspace }}/repository"

test-results:
name: Test Results
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ Now let's load this module and instantiate it:
import com.dylibso.chicory.runtime.ExportFunction;
import com.dylibso.chicory.wasm.types.Value;
import com.dylibso.chicory.wasm.Module;
import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.runtime.Instance;
import java.io.File;

// point this to your path on disk
Module module = Module.builder(new File("./factorial.wasm")).build();
Module module = Parser.parse(new File("./factorial.wasm"));
Instance instance = Instance.builder(module).build();
```

Expand Down Expand Up @@ -171,7 +172,7 @@ copyFileFromWasmCorpus("count_vowels.rs.wasm", "count_vowels.wasm");
Build and instantiate this module:

```java
Instance instance = Instance.builder(Module.builder(new File("./count_vowels.wasm")).build()).build();
Instance instance = Instance.builder(Parser.parse(new File("./count_vowels.wasm"))).build();
ExportFunction countVowels = instance.export("count_vowels");
```

Expand Down Expand Up @@ -278,7 +279,7 @@ Now we just need to pass this host function in during our instantiation phase:
```java
import com.dylibso.chicory.runtime.HostImports;
var imports = new HostImports(new HostFunction[] {func});
var instance = Instance.builder(Module.builder(new File("./logger.wasm")).build()).withHostImports(imports).build();
var instance = Instance.builder(Parser.parse(new File("./logger.wasm"))).withHostImports(imports).build();
var logIt = instance.export("logIt");
logIt.apply();
// should print "Hello, World!" 10 times
Expand Down
4 changes: 2 additions & 2 deletions aot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ To enable use the AotMachine factory when building the module:
```java
// ...

import com.dylibso.chicory.wasm.Module;
import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.aot.AotMachine;
// ...
var is = ClassLoader.getSystemClassLoader().getResourceAsStream("compiled/basic.c.wasm");
Instance.builder(Module.builder(is).build()).withMachineFactory(AotMachine::new).build();
Instance.builder(Parser.parse(is)).withMachineFactory(AotMachine::new).build();
```
1 change: 1 addition & 0 deletions readmes/wasm/expected/parser-base.result
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target_features
8 changes: 8 additions & 0 deletions readmes/wasm/expected/parser-listener.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.debug_abbrev
.debug_info
.debug_ranges
.debug_str
.debug_line
name
producers
target_features
5 changes: 3 additions & 2 deletions wasi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ import com.dylibso.chicory.log.SystemLogger;
import com.dylibso.chicory.wasi.WasiOptions;
import com.dylibso.chicory.wasi.WasiPreview1;
import com.dylibso.chicory.wasm.Module;
import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.runtime.HostImports;
import com.dylibso.chicory.runtime.Instance;

Expand All @@ -169,7 +170,7 @@ var wasi = new WasiPreview1(logger, WasiOptions.builder().build());
var imports = new HostImports(wasi.toHostFunctions());
// create the module and connect imports
// this will execute the module if it's a WASI command-pattern module
Instance.builder(Module.builder(new File("hello-wasi.wasm")).build()).withHostImports(imports).build();
Instance.builder(Parser.parse(new File("hello-wasi.wasm"))).withHostImports(imports).build();
```

> **Note**: Take note that we don't explicitly execute the module. The module will run when you instantiate it. This
Expand Down Expand Up @@ -208,7 +209,7 @@ var imports = new HostImports(wasi.toHostFunctions());

// greet-wasi is a rust program that greets the string passed in stdin
// instantiating will execute the module if it's a WASI command-pattern module
Instance.builder(Module.builder(new File("greet-wasi.wasm")).build()).withHostImports(imports).build();
Instance.builder(Parser.parse(new File("greet-wasi.wasm"))).withHostImports(imports).build();

// check that we output the greeting
assert(fakeStdout.toString().equals("Hello, Andrea!"));
Expand Down
76 changes: 55 additions & 21 deletions wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,80 @@ for a beta we will publish to maven central. For now this is available as Github
## Usage

There are two ways you can interface with this library. The simplest way is to parse the whole
module using `parseModule`:
module using `Parser.parse`:

<!--
```java
var parser = new Parser(new SystemLogger());
try (var fis = new FileInputStream("/tmp/code.wasm")) {
var module = parser.parseModule(fis);
var customSection = module.customSections().get(0);
System.out.println("First custom section: " + customSection.getName());
} catch (Exception e) {
throw new RuntimeException(e);
};
//DEPS com.dylibso.chicory:wasm-corpus:999-SNAPSHOT
//DEPS com.dylibso.chicory:wasm:999-SNAPSHOT
```
-->

<!--
```java
var readmeResults = "readmes/wasm/current";
new File(readmeResults).mkdirs();
public void writeResultFile(String name, String content) throws Exception {
FileWriter fileWriter = new FileWriter(new File(".").toPath().resolve(readmeResults).resolve(name).toFile());
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.print(content);
printWriter.flush();
printWriter.close();
}
```
-->

```java
import com.dylibso.chicory.wasm.Parser;

var is = ClassLoader.getSystemClassLoader().getResourceAsStream("compiled/count_vowels.rs.wasm");
var module = Parser.parse(is);
var customSection = module.customSections().get(0);
System.out.println("First custom section: " + customSection.name());
```

<!--
```java
writeResultFile("parser-base.result", customSection.name());
```
-->

The second is to use the `ParserListener` interface and the `parse()` method. In this mode you can also call
`includeSection(int sectionId)` for each section you wish to parse. It will skip all other
sections. This is useful for performance if you only want to parse a piece of the module.
If you don't call this method once it will parse all sections.

```java
var parser = new Parser(new SystemLogger());
import com.dylibso.chicory.wasm.ParserListener;
import com.dylibso.chicory.wasm.types.CustomSection;
import com.dylibso.chicory.wasm.types.SectionId;

var parser = new Parser();

// include the custom sections, don't call this to receive all sections
parser.includeSection(SectionId.CUSTOM);
// parser.includeSection(SectionId.CODE); // call for each section you want

String result = "";
// implement the listener
parser.setListener(section -> {
if (section.getSectionId() == SectionId.CUSTOM) {
ParserListener listener = section -> {
if (section.sectionId() == SectionId.CUSTOM) {
var customSection = (CustomSection) section;
var name = customSection.getName();
var name = customSection.name();
result += name + "\n";
System.out.println("Got custom section with name: " + name);
} else {
fail("Should not have received section with id: " + section.getSectionId());
throw new RuntimeException("Should not have received section with id: " + section.sectionId());
}
});

// call parseModule()
try (var fis = new FileInputStream("/tmp/code.wasm")) {
parser.parseModule(fis);
} catch (Exception e) {
throw new RuntimeException(e);
};

// call parse()
var is = ClassLoader.getSystemClassLoader().getResourceAsStream("compiled/count_vowels.rs.wasm");
parser.parse(is, listener);
```
<!--
```java
writeResultFile("parser-listener.result", result);
```
-->
3 changes: 1 addition & 2 deletions wasm/src/main/java/com/dylibso/chicory/wasm/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ public void validateSectionType(byte sectionId) {
}
}

// package protected to make it visible for testing
void parse(InputStream in, ParserListener listener) {
public void parse(InputStream in, ParserListener listener) {

requireNonNull(listener, "listener");
var validator = new SectionsValidator();
Expand Down

0 comments on commit 04aaa54

Please sign in to comment.