-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
9,105 additions
and
2,666 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?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> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<artifactId>docs-lib</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Chicory - Docs Lib</name> | ||
<description>INTERNAL USAGE ONLY - utils to verify the docs</description> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package docs; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.PrintWriter; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.StandardCopyOption; | ||
|
||
public final class FileOps { | ||
|
||
private FileOps() {} | ||
|
||
public static void copyFromWasmCorpus(String sourceName, String destName) throws Exception { | ||
var dest = new File(".").toPath().resolve(destName); | ||
if (dest.toFile().exists()) { | ||
dest.toFile().delete(); | ||
} | ||
Files.copy( | ||
new File(".") | ||
.toPath() | ||
.resolve("wasm-corpus") | ||
.resolve("src") | ||
.resolve("main") | ||
.resolve("resources") | ||
.resolve("compiled") | ||
.resolve(sourceName), | ||
dest, | ||
StandardCopyOption.REPLACE_EXISTING); | ||
} | ||
|
||
public static void writeResult(String folder, String name, String content) throws Exception { | ||
var dir = new File(".").toPath().resolve(folder); | ||
dir.toFile().mkdirs(); | ||
FileWriter fileWriter = new FileWriter(dir.resolve(name).toFile(), StandardCharsets.UTF_8); | ||
PrintWriter printWriter = new PrintWriter(fileWriter); | ||
printWriter.print(content); | ||
printWriter.flush(); | ||
printWriter.close(); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
sidebar_position: 1 | ||
--- | ||
|
||
## Using the memory to share data | ||
|
||
Wasm only understands basic integer and float primitives. So passing more complex types across the boundary involves | ||
passing pointers. To read, write, or allocate memory in a module, Chicory gives you the `Memory` class. Let's look at an | ||
example where we have a module `count_vowels.wasm`, written in rust, that takes a string input and counts the number of vowels | ||
in the string: | ||
|
||
```bash | ||
curl https://raw.githubusercontent.com/dylibso/chicory/main/wasm-corpus/src/main/resources/compiled/count_vowels.rs.wasm > count_vowels.wasm | ||
``` | ||
|
||
<!-- | ||
```java | ||
//DEPS com.dylibso.chicory:docs-lib:999-SNAPSHOT | ||
//DEPS com.dylibso.chicory:runtime:999-SNAPSHOT | ||
``` | ||
--> | ||
|
||
<!-- | ||
```java | ||
docs.FileOps.copyFromWasmCorpus("count_vowels.rs.wasm", "count_vowels.wasm"); | ||
``` | ||
--> | ||
|
||
Build and instantiate this module: | ||
|
||
```java | ||
import com.dylibso.chicory.runtime.ExportFunction; | ||
import com.dylibso.chicory.runtime.Instance; | ||
import com.dylibso.chicory.wasm.Parser; | ||
|
||
Instance instance = Instance.builder(Parser.parse(new File("./count_vowels.wasm"))).build(); | ||
ExportFunction countVowels = instance.export("count_vowels"); | ||
``` | ||
|
||
To pass it a string, we first need to put the string in the module's memory. To make this easier and safe, | ||
the module gives us some extra exports to allow us to allocate and deallocate memory: | ||
|
||
```java | ||
ExportFunction alloc = instance.export("alloc"); | ||
ExportFunction dealloc = instance.export("dealloc"); | ||
``` | ||
|
||
Let's allocate Wasm memory for a string and put in the instance's memory. We can do this with `Memory#put`: | ||
|
||
```java | ||
import com.dylibso.chicory.runtime.Memory; | ||
Memory memory = instance.memory(); | ||
String message = "Hello, World!"; | ||
int len = message.getBytes().length; | ||
// allocate {len} bytes of memory, this returns a pointer to that memory | ||
int ptr = (int) alloc.apply(len)[0]; | ||
// We can now write the message to the module's memory: | ||
memory.writeString(ptr, message); | ||
``` | ||
|
||
Now we can call `countVowels` with this pointer to the string. It will do it's job and return the count. We will | ||
call `dealloc` to free that memory in the module. Though the module could do this itself if you want: | ||
|
||
```java | ||
var result = countVowels.apply(ptr, len)[0]; | ||
dealloc.apply(ptr, len); | ||
assert(3L == result); // 3 vowels in Hello, World! | ||
``` | ||
|
||
<!-- | ||
```java | ||
docs.FileOps.writeResult("readmes/memory/current", "countVowels.result", "" + result); | ||
``` | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,8 @@ | |
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.underConstruction { | ||
height: 30%; | ||
max-height: 500px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.