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
This commit is contained in:
Alex Crichton
2020-03-20 18:10:53 -05:00
committed by GitHub
parent 07bd973027
commit e245e6dd9c
29 changed files with 867 additions and 4 deletions

View File

@@ -7,6 +7,22 @@
- [Examples](./examples.md)
- [Markdown parser](./examples-markdown.md)
- [Profiling WebAssembly](./examples-profiling.md)
- [Embedding in Rust](./examples-rust-embed.md)
- [Hello, world!](./examples-rust-hello-world.md)
- [Calculating the GCD](./examples-rust-gcd.md)
- [Using linear memory](./examples-rust-memory.md)
- [WASI](./examples-rust-wasi.md)
- [Linking modules](./examples-rust-linking.md)
- [Debugging](./examples-rust-debugging.md)
- [Using multi-value](./examples-rust-multi-value.md)
- [Embedding in C](./examples-c-embed.md)
- [Hello, world!](./examples-c-hello-world.md)
- [Calculating the GCD](./examples-c-gcd.md)
- [Using linear memory](./examples-c-memory.md)
- [WASI](./examples-c-wasi.md)
- [Linking modules](./examples-c-linking.md)
- [Debugging](./examples-c-debugging.md)
- [Using multi-value](./examples-c-multi-value.md)
- [Using WebAssembly from your lanugage](./lang.md)
- [Python](./lang-python.md)
- [.NET](./lang-dotnet.md)

View File

@@ -3,7 +3,8 @@
This document shows an example of how to embed Wasmtime using the [Rust
API][apidoc] to execute a simple wasm program. Be sure to also check out the
[full API documentation][apidoc] for a full listing of what the [`wasmtime`
crate][crate] has to offer.
crate][wasmtime] has to offer and the [book examples for
Rust](./examples-rust-embed.md) for more information.
[apidoc]: https://bytecodealliance.github.io/wasmtime/api/wasmtime/
[wasmtime]: https://crates.io/crates/wasmtime

View File

@@ -0,0 +1,15 @@
# Debugging
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/fib-debug/main.c
This example shows off how to set up a module for dynamic runtime debugging via
a native debugger like GDB or LLDB.
## `main.c`
```c
{{#include ../examples/fib-debug/main.c}}
```

12
docs/examples-c-embed.md Normal file
View File

@@ -0,0 +1,12 @@
# Embedding in C
This section is intended to showcase the C embedding API for Wasmtime. The C
embedding API is based on the [proposed wasm C embedding API][proposal] (namely
[`wasm.h`]) and has a few extension headers (like [`wasi.h`] and
[`wasmtime.h`]) which are intended to eventually become part of the standard
themselves one day.
[proposal]: https://github.com/webassembly/wasm-c-api
[`wasm.h`]: https://github.com/WebAssembly/wasm-c-api/blob/master/include/wasm.h
[`wasi.h`]: https://github.com/bytecodealliance/wasmtime/blob/master/crates/c-api/include/wasi.h
[`wasmtime.h`]: https://github.com/bytecodealliance/wasmtime/blob/master/crates/c-api/include/wasmtime.h

22
docs/examples-c-gcd.md Normal file
View File

@@ -0,0 +1,22 @@
# Calculating the GCD
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/gcd.c
This example shows off how run a wasm program which calculates the GCD of two
numbers.
## `gcd.wat`
```wat
{{#include ../examples/gcd.wat}}
```
## `gcd.c`
```c
{{#include ../examples/gcd.c}}
```

View File

@@ -0,0 +1,22 @@
# Hello, world!
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/hello.c
This example shows off how to instantiate a simple wasm module and interact with
it.
## `hello.wat`
```wat
{{#include ../examples/hello.wat}}
```
## `hello.c`
```c
{{#include ../examples/hello.c}}
```

View File

@@ -0,0 +1,27 @@
# Linking modules
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/linking.c
This example shows off how to compile and instantiate modules which link
together.
## `linking1.wat`
```wat
{{#include ../examples/linking1.wat}}
```
## `linking2.wat`
```wat
{{#include ../examples/linking2.wat}}
```
## `linking.c`
```c
{{#include ../examples/linking.c}}
```

24
docs/examples-c-memory.md Normal file
View File

@@ -0,0 +1,24 @@
# Using linear memory
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/memory.c
This example shows off how to interact with wasm memory in a module. Be sure to
read the documentation for [`Memory`] as well.
[`Memory`]: https://bytecodealliance.github.io/wasmtime/api/wasmtime/struct.Memory.html
## `memory.wat`
```wat
{{#include ../examples/memory.wat}}
```
## `memory.c`
```c
{{#include ../examples/memory.c}}
```

View File

@@ -0,0 +1,22 @@
# Using multi-value
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/multi.c
This example shows off how to interact with a wasm module that uses multi-value
exports and imports.
## `multi.wat`
```wat
{{#include ../examples/multi.wat}}
```
## `multi.c`
```c
{{#include ../examples/multi.c}}
```

21
docs/examples-c-wasi.md Normal file
View File

@@ -0,0 +1,21 @@
# WASI
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/wasi/main.c
This example shows off how to instantiate a wasm module using WASI imports.
## Wasm Source code
```rust,ignore
{{#include ../examples/wasi/wasm/wasi.c}}
```
## `wasi.c`
```c
{{#include ../examples/wasi/main.c}}
```

View File

@@ -0,0 +1,15 @@
# Debugging
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/fib-debug/main.rs
This example shows off how to set up a module for dynamic runtime debugging via
a native debugger like GDB or LLDB.
## `main.rs`
```rust,ignore
{{#include ../examples/fib-debug/main.rs}}
```

View File

@@ -0,0 +1,7 @@
# Embedding in Rust
This section is intended to showcase the Rust embedding API for Wasmtime. This
is done through the [`wasmtime` crate](https://crates.io/crates/wasmtime). In
addition to browsing the following examples you can also browse the [specific
section on Rust embedding](./embed-rust.md) or the [full API
documentation](https://docs.rs/wasmtime).

22
docs/examples-rust-gcd.md Normal file
View File

@@ -0,0 +1,22 @@
# Calculating the GCD
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/gcd.rs
This example shows off how run a wasm program which calculates the GCD of two
numbers.
## `gcd.wat`
```wat
{{#include ../examples/gcd.wat}}
```
## `gcd.rs`
```rust,ignore
{{#include ../examples/gcd.rs}}
```

View File

@@ -0,0 +1,22 @@
# Hello, world!
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/hello.rs
This example shows off how to instantiate a simple wasm module and interact with
it.
## `hello.wat`
```wat
{{#include ../examples/hello.wat}}
```
## `hello.rs`
```rust,ignore
{{#include ../examples/hello.rs}}
```

View File

@@ -0,0 +1,27 @@
# Linking modules
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/linking.rs
This example shows off how to compile and instantiate modules which link
together.
## `linking1.wat`
```wat
{{#include ../examples/linking1.wat}}
```
## `linking2.wat`
```wat
{{#include ../examples/linking2.wat}}
```
## `linking.rs`
```rust,ignore
{{#include ../examples/linking.rs}}
```

View File

@@ -0,0 +1,24 @@
# Using linear memory
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/memory.rs
This example shows off how to interact with wasm memory in a module. Be sure to
read the documentation for [`Memory`] as well.
[`Memory`]: https://bytecodealliance.github.io/wasmtime/api/wasmtime/struct.Memory.html
## `memory.wat`
```wat
{{#include ../examples/memory.wat}}
```
## `memory.rs`
```rust,ignore
{{#include ../examples/memory.rs}}
```

View File

@@ -0,0 +1,22 @@
# Using multi-value
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/multi.rs
This example shows off how to interact with a wasm module that uses multi-value
exports and imports.
## `multi.wat`
```wat
{{#include ../examples/multi.wat}}
```
## `multi.rs`
```rust,ignore
{{#include ../examples/multi.rs}}
```

View File

@@ -0,0 +1,21 @@
# WASI
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/master/examples/wasi/main.rs
This example shows off how to instantiate a wasm module using WASI imports.
## Wasm Source code
```rust
{{#include ../examples/wasi/wasm/wasi.rs}}
```
## `wasi.rs`
```rust,ignore
{{#include ../examples/wasi/main.rs}}
```