Skip to content

Commit be304e6

Browse files
committed
Add examples to the book as well
1 parent 9c22833 commit be304e6

18 files changed

+342
-1
lines changed

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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.
77

88
[apidoc]: https://bytecodealliance.github.io/wasmtime/api/wasmtime/
99
[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+
```rust
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+
```

docs/examples-c-memory.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Using linear memory
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/memory.c
7+
8+
This example shows off how to interact with wasm memory in a module. Be sure to
9+
read the documentation for [`Memory`] as well.
10+
11+
[`Memory`]: https://bytecodealliance.github.io/wasmtime/api/wasmtime/struct.Memory.html
12+
13+
## `memory.wat`
14+
15+
```wat
16+
{{#include ../examples/memory.wat}}
17+
```
18+
19+
20+
## `memory.c`
21+
22+
```c
23+
{{#include ../examples/memory.c}}
24+
```

docs/examples-c-multi-value.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Using multi-value
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/multi.c
7+
8+
This example shows off how to interact with a wasm module that uses multi-value
9+
exports and imports.
10+
11+
## `multi.wat`
12+
13+
```wat
14+
{{#include ../examples/multi.wat}}
15+
```
16+
17+
18+
## `multi.c`
19+
20+
```c
21+
{{#include ../examples/multi.c}}
22+
```

docs/examples-c-wasi.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# WASI
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/wasi/main.c
7+
8+
This example shows off how to instantiate a wasm module using WASI imports.
9+
10+
## Wasm Source code
11+
12+
```rust
13+
{{#include ../examples/wasi/wasm/wasi.c}}
14+
```
15+
16+
17+
## `wasi.c`
18+
19+
```c
20+
{{#include ../examples/wasi/main.c}}
21+
```

docs/examples-rust-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.rs
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.rs`
12+
13+
```rust
14+
{{#include ../examples/fib-debug/main.rs}}
15+
```

docs/examples-rust-embed.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Embedding in Rust
2+
3+
This section is intended to showcase the Rust embedding API for Wasmtime. This
4+
is done through the [`wasmtime` crate](https://crates.io/crates/wasmtime). In
5+
addition to browsing the following examples you can also browse the [specific
6+
section on Rust embedding](./embed-rust.md) or the [full API
7+
documentation](https://docs.rs/wasmtime).

docs/examples-rust-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.rs
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.rs`
19+
20+
```rust
21+
{{#include ../examples/gcd.rs}}
22+
```

docs/examples-rust-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.rs
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.rs`
19+
20+
```rust
21+
{{#include ../examples/hello.rs}}
22+
```

docs/examples-rust-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.rs
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.rs`
24+
25+
```rust
26+
{{#include ../examples/linking.rs}}
27+
```

docs/examples-rust-memory.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Using linear memory
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/memory.rs
7+
8+
This example shows off how to interact with wasm memory in a module. Be sure to
9+
read the documentation for [`Memory`] as well.
10+
11+
[`Memory`]: https://bytecodealliance.github.io/wasmtime/api/wasmtime/struct.Memory.html
12+
13+
## `memory.wat`
14+
15+
```wat
16+
{{#include ../examples/memory.wat}}
17+
```
18+
19+
20+
## `memory.rs`
21+
22+
```rust
23+
{{#include ../examples/memory.rs}}
24+
```

docs/examples-rust-multi-value.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Using multi-value
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/multi.rs
7+
8+
This example shows off how to interact with a wasm module that uses multi-value
9+
exports and imports.
10+
11+
## `multi.wat`
12+
13+
```wat
14+
{{#include ../examples/multi.wat}}
15+
```
16+
17+
18+
## `multi.rs`
19+
20+
```rust
21+
{{#include ../examples/multi.rs}}
22+
```

docs/examples-rust-wasi.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# WASI
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/wasi/main.rs
7+
8+
This example shows off how to instantiate a wasm module using WASI imports.
9+
10+
## Wasm Source code
11+
12+
```rust
13+
{{#include ../examples/wasi/wasm/wasi.rs}}
14+
```
15+
16+
17+
## `wasi.rs`
18+
19+
```rust
20+
{{#include ../examples/wasi/main.rs}}
21+
```

0 commit comments

Comments
 (0)