Skip to content

Commit e245e6d

Browse files
authored
Add examples of linking and WASI (#1369)
* Add examples of linking and WASI This commit adds two example programs, one for linking two modules together and one for instantiating WASI. The linkage example additionally uses WASI to get some meaningful output at this time. cc #1272 * Add examples to the book as well * More links! * Ignore examples from rustdoc testsing * More example updates * More ignored
1 parent 07bd973 commit e245e6d

29 files changed

+867
-4
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ members = [
6767
"crates/misc/rust",
6868
"crates/wiggle",
6969
"examples/fib-debug/wasm",
70+
"examples/wasi/wasm",
7071
"fuzz",
7172
]
7273

crates/misc/run-examples/src/main.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ use std::collections::BTreeSet;
22
use std::process::Command;
33

44
fn main() {
5+
let example_to_run = std::env::args().nth(1);
56
let examples = std::fs::read_dir("examples").unwrap();
67
let examples = examples
7-
.map(|e| {
8+
.filter_map(|e| {
89
let e = e.unwrap();
910
let path = e.path();
1011
let dir = e.metadata().unwrap().is_dir();
11-
(path.file_stem().unwrap().to_str().unwrap().to_owned(), dir)
12+
if let Some("wat") = path.extension().and_then(|s| s.to_str()) {
13+
return None;
14+
}
15+
16+
Some((path.file_stem().unwrap().to_str().unwrap().to_owned(), dir))
1217
})
1318
.collect::<BTreeSet<_>>();
1419

@@ -21,14 +26,24 @@ fn main() {
2126
if example == "README" {
2227
continue;
2328
}
29+
if let Some(example_to_run) = &example_to_run {
30+
if !example.contains(&example_to_run[..]) {
31+
continue;
32+
}
33+
}
2434
if is_dir {
2535
println!("======== Rust wasm file `{}` ============", example);
36+
let target = if example == "fib-debug" {
37+
"wasm32-unknown-unknown"
38+
} else {
39+
"wasm32-wasi"
40+
};
2641
run(Command::new("cargo")
2742
.arg("build")
2843
.arg("-p")
2944
.arg(format!("example-{}-wasm", example))
3045
.arg("--target")
31-
.arg("wasm32-unknown-unknown"));
46+
.arg(target));
3247
}
3348
println!("======== Rust example `{}` ============", example);
3449
run(Command::new("cargo")

docs/SUMMARY.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@
77
- [Examples](./examples.md)
88
- [Markdown parser](./examples-markdown.md)
99
- [Profiling WebAssembly](./examples-profiling.md)
10+
- [Embedding in Rust](./examples-rust-embed.md)
11+
- [Hello, world!](./examples-rust-hello-world.md)
12+
- [Calculating the GCD](./examples-rust-gcd.md)
13+
- [Using linear memory](./examples-rust-memory.md)
14+
- [WASI](./examples-rust-wasi.md)
15+
- [Linking modules](./examples-rust-linking.md)
16+
- [Debugging](./examples-rust-debugging.md)
17+
- [Using multi-value](./examples-rust-multi-value.md)
18+
- [Embedding in C](./examples-c-embed.md)
19+
- [Hello, world!](./examples-c-hello-world.md)
20+
- [Calculating the GCD](./examples-c-gcd.md)
21+
- [Using linear memory](./examples-c-memory.md)
22+
- [WASI](./examples-c-wasi.md)
23+
- [Linking modules](./examples-c-linking.md)
24+
- [Debugging](./examples-c-debugging.md)
25+
- [Using multi-value](./examples-c-multi-value.md)
1026
- [Using WebAssembly from your lanugage](./lang.md)
1127
- [Python](./lang-python.md)
1228
- [.NET](./lang-dotnet.md)

docs/embed-rust.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
This document shows an example of how to embed Wasmtime using the [Rust
44
API][apidoc] to execute a simple wasm program. Be sure to also check out the
55
[full API documentation][apidoc] for a full listing of what the [`wasmtime`
6-
crate][crate] has to offer.
6+
crate][wasmtime] has to offer and the [book examples for
7+
Rust](./examples-rust-embed.md) for more information.
78

89
[apidoc]: https://bytecodealliance.github.io/wasmtime/api/wasmtime/
910
[wasmtime]: https://crates.io/crates/wasmtime

docs/examples-c-debugging.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Debugging
2+
3+
You can also [browse this source code online][code] and clone the wasmtime
4+
repository to run the example locally.
5+
6+
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/fib-debug/main.c
7+
8+
This example shows off how to set up a module for dynamic runtime debugging via
9+
a native debugger like GDB or LLDB.
10+
11+
## `main.c`
12+
13+
```c
14+
{{#include ../examples/fib-debug/main.c}}
15+
```

docs/examples-c-embed.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Embedding in C
2+
3+
This section is intended to showcase the C embedding API for Wasmtime. The C
4+
embedding API is based on the [proposed wasm C embedding API][proposal] (namely
5+
[`wasm.h`]) and has a few extension headers (like [`wasi.h`] and
6+
[`wasmtime.h`]) which are intended to eventually become part of the standard
7+
themselves one day.
8+
9+
[proposal]: https://github.com/webassembly/wasm-c-api
10+
[`wasm.h`]: https://github.com/WebAssembly/wasm-c-api/blob/master/include/wasm.h
11+
[`wasi.h`]: https://github.com/bytecodealliance/wasmtime/blob/master/crates/c-api/include/wasi.h
12+
[`wasmtime.h`]: https://github.com/bytecodealliance/wasmtime/blob/master/crates/c-api/include/wasmtime.h

docs/examples-c-gcd.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Calculating the GCD
2+
3+
You can also [browse this source code online][code] and clone the wasmtime
4+
repository to run the example locally.
5+
6+
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/gcd.c
7+
8+
This example shows off how run a wasm program which calculates the GCD of two
9+
numbers.
10+
11+
## `gcd.wat`
12+
13+
```wat
14+
{{#include ../examples/gcd.wat}}
15+
```
16+
17+
18+
## `gcd.c`
19+
20+
```c
21+
{{#include ../examples/gcd.c}}
22+
```

docs/examples-c-hello-world.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Hello, world!
2+
3+
You can also [browse this source code online][code] and clone the wasmtime
4+
repository to run the example locally.
5+
6+
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/hello.c
7+
8+
This example shows off how to instantiate a simple wasm module and interact with
9+
it.
10+
11+
## `hello.wat`
12+
13+
```wat
14+
{{#include ../examples/hello.wat}}
15+
```
16+
17+
18+
## `hello.c`
19+
20+
```c
21+
{{#include ../examples/hello.c}}
22+
```

docs/examples-c-linking.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Linking modules
2+
3+
You can also [browse this source code online][code] and clone the wasmtime
4+
repository to run the example locally.
5+
6+
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/linking.c
7+
8+
This example shows off how to compile and instantiate modules which link
9+
together.
10+
11+
## `linking1.wat`
12+
13+
```wat
14+
{{#include ../examples/linking1.wat}}
15+
```
16+
17+
## `linking2.wat`
18+
19+
```wat
20+
{{#include ../examples/linking2.wat}}
21+
```
22+
23+
## `linking.c`
24+
25+
```c
26+
{{#include ../examples/linking.c}}
27+
```

0 commit comments

Comments
 (0)