Skip to content

Commit eaa01d7

Browse files
tschneidereitalexcrichtonfitzgen
authored
[docs] Add C++ chapter and missing examples (#11569)
* [docs] Add C++ chapter and missing examples This adds a chapter for C++ embedding. The chapter intro is rudimentary, but there are a bunch more examples now: I added C++ ports for everything that already had both a Rust and a C version available, matching the Rust version's behavior where applicable. * [example] Fix paths in `examples/wasip1/main.c` * [docs] Add entries for additional Rust and C embedding examples These examples already existed, but didn't have entries in the docs. It'll probably make sense to restructure all of this at some point and move the examples into language-specific folders. * [docs] Remove references to Conan packages from README.md These packages haven't been updated since Wasmtime 18, so we shouldn't link to them for now. * Format cpp files * Format even more cpp files * Fix some warnings in C++ headers * Simplify threads.cc example * Update docs/examples-c-serialize.md Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com> * [example] Don't try, do. * [docs] Restore CMake link in README * [docs] Improve examples/README.md * [docs] Address review feedback --------- Co-authored-by: Alex Crichton <alex@alexcrichton.com> Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
1 parent 1df1255 commit eaa01d7

41 files changed

Lines changed: 1003 additions & 25 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ the implementation.
139139
Languages supported by the Bytecode Alliance:
140140

141141
* **[Rust]** - the [`wasmtime` crate]
142-
* **[C]** - the [`wasm.h`, `wasi.h`, and `wasmtime.h` headers][c-headers], [CMake](crates/c-api/CMakeLists.txt) or [`wasmtime` Conan package]
143-
* **C++** - the [`wasmtime.hh` header][c-headers] or the [`wasmtime-cpp` Conan package]
142+
* **[C]** - the [`wasm.h`, `wasi.h`, and `wasmtime.h` headers][c-headers], [CMake](crates/c-api/CMakeLists.txt)
143+
* **C++** - the [`wasmtime.hh` header][c-headers]
144144
* **[Python]** - the [`wasmtime` PyPI package]
145145
* **[.NET]** - the [`Wasmtime` NuGet package]
146146
* **[Go]** - the [`wasmtime-go` repository]
@@ -161,8 +161,6 @@ Languages supported by the community:
161161
[`Wasmtime` NuGet package]: https://www.nuget.org/packages/Wasmtime
162162
[Go]: https://bytecodealliance.github.io/wasmtime/lang-go.html
163163
[`wasmtime-go` repository]: https://pkg.go.dev/github.com/bytecodealliance/wasmtime-go
164-
[`wasmtime` Conan package]: https://conan.io/center/wasmtime
165-
[`wasmtime-cpp` Conan package]: https://conan.io/center/wasmtime-cpp
166164
[Ruby]: https://bytecodealliance.github.io/wasmtime/lang-ruby.html
167165
[`wasmtime` gem]: https://rubygems.org/gems/wasmtime
168166
[Elixir]: https://docs.wasmtime.dev/lang-elixir.html

crates/c-api/include/wasmtime.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@
1919
* \example gcd.cc
2020
* \example linking.cc
2121
* \example memory.cc
22+
* \example multimemory.cc
2223
* \example interrupt.cc
24+
* \example multi.cc
25+
* \example anyref.cc
2326
* \example externref.cc
27+
* \example serialize.cc
28+
* \example threads.cc
2429
*/
2530

2631
/**

crates/c-api/include/wasmtime/func.hh

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ template <typename T> struct WasmType {
6363
static const ValKind kind = ValKind::valkind; \
6464
static void store(Store::Context cx, wasmtime_val_raw_t *p, \
6565
const native &t) { \
66+
(void)cx; \
6667
p->field = t; \
6768
} \
6869
static native load(Store::Context cx, wasmtime_val_raw_t *p) { \
70+
(void)cx; \
6971
return p->field; \
7072
} \
7173
};
@@ -108,9 +110,13 @@ template <> struct WasmType<V128> {
108110
static const bool valid = true;
109111
static const ValKind kind = ValKind::V128;
110112
static void store(Store::Context cx, wasmtime_val_raw_t *p, const V128 &t) {
113+
(void)cx;
111114
memcpy(&p->v128[0], &t.v128[0], sizeof(wasmtime_v128));
112115
}
113-
static V128 load(Store::Context cx, wasmtime_val_raw_t *p) { return p->v128; }
116+
static V128 load(Store::Context cx, wasmtime_val_raw_t *p) {
117+
(void)cx;
118+
return p->v128;
119+
}
114120
};
115121

116122
/// A "trait" for a list of types and operations on them, used for `Func::wrap`
@@ -139,8 +145,14 @@ template <> struct WasmTypeList<std::monostate> {
139145
static const size_t size = 0;
140146
static bool matches(ValType::ListRef types) { return types.size() == 0; }
141147
static void store(Store::Context cx, wasmtime_val_raw_t *storage,
142-
const std::monostate &t) {}
148+
const std::monostate &t) {
149+
(void)cx;
150+
(void)storage;
151+
(void)t;
152+
}
143153
static std::monostate load(Store::Context cx, wasmtime_val_raw_t *storage) {
154+
(void)cx;
155+
(void)storage;
144156
return std::monostate();
145157
}
146158
static std::vector<ValType> types() { return {}; }
@@ -167,8 +179,8 @@ template <typename... T> struct WasmTypeList<std::tuple<T...>> {
167179
t);
168180
}
169181
static std::tuple<T...> load(Store::Context cx, wasmtime_val_raw_t *storage) {
170-
size_t n = 0;
171-
return std::tuple<T...>{WasmType<T>::load(cx, &storage[n++])...}; // NOLINT
182+
(void)cx;
183+
return std::tuple<T...>{WasmType<T>::load(cx, storage++)...}; // NOLINT
172184
}
173185
static std::vector<ValType> types() { return {WasmType<T>::kind...}; }
174186
};
@@ -195,6 +207,8 @@ template <> struct WasmHostRet<void> {
195207
template <typename F, typename... A>
196208
static std::optional<Trap> invoke(F f, Caller cx, wasmtime_val_raw_t *raw,
197209
A... args) {
210+
(void)cx;
211+
(void)raw;
198212
f(args...);
199213
return std::nullopt;
200214
}
@@ -331,6 +345,7 @@ class Func {
331345
raw_callback_unchecked(void *env, wasmtime_caller_t *caller,
332346
wasmtime_val_raw_t *args_and_results,
333347
size_t nargs_and_results) {
348+
(void)nargs_and_results;
334349
using HostFunc = WasmHostFunc<F>;
335350
Caller cx(caller);
336351
F *func = reinterpret_cast<F *>(env); // NOLINT

crates/c-api/include/wasmtime/global.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public:
6161
Val get(Store::Context cx) const {
6262
Val val;
6363
wasmtime_global_get(cx.ptr, &global, &val.val);
64-
return std::move(val);
64+
return val;
6565
}
6666

6767
/// Sets this global to a new value.

docs/SUMMARY.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,43 @@
1111
- [Hello, world!](./examples-rust-hello-world.md)
1212
- [Calculating the GCD](./examples-rust-gcd.md)
1313
- [Using Linear Memory](./examples-rust-memory.md)
14-
- [WASI](./examples-rust-wasi.md)
14+
- [WASIp2](./examples-rust-wasip2.md)
1515
- [Linking Modules](./examples-rust-linking.md)
1616
- [Debugging](./examples-rust-debugging.md)
1717
- [Core Dumps](./examples-rust-core-dumps.md)
1818
- [Using Multi-Value](./examples-rust-multi-value.md)
19+
- [`anyref` Reference Type](./examples-rust-anyref.md)
20+
- [`externref` Reference Type](./examples-rust-externref.md)
21+
- [Multiple Memories](./examples-rust-multimemory.md)
22+
- [Serializing Modules](./examples-rust-serialize.md)
23+
- [Multithreaded Embedding](./examples-rust-multithreaded-embedding.md)
24+
- [Memory Protection Keys](./examples-rust-mpk.md)
1925
- [C](./lang-c.md)
2026
- [Hello, World!](./examples-c-hello-world.md)
2127
- [Calculating the GCD](./examples-c-gcd.md)
2228
- [Using Linear Memory](./examples-c-memory.md)
23-
- [WASI](./examples-c-wasi.md)
29+
- [WASIp1](./examples-c-wasi.md)
2430
- [Linking Modules](./examples-c-linking.md)
2531
- [Debugging](./examples-c-debugging.md)
2632
- [Using Multi-Value](./examples-c-multi-value.md)
33+
- [`anyref` Reference Type](./examples-c-anyref.md)
34+
- [`externref` Reference Type](./examples-c-externref.md)
35+
- [Multiple Memories](./examples-c-multimemory.md)
36+
- [Serializing Modules](./examples-c-serialize.md)
37+
- [Multithreaded Embedding](./examples-c-multithreaded-embedding.md)
38+
- [C++](./lang-cpp.md)
39+
- [Hello, World!](./examples-cpp-hello-world.md)
40+
- [Calculating the GCD](./examples-cpp-gcd.md)
41+
- [Using Linear Memory](./examples-cpp-memory.md)
42+
- [WASIp1](./examples-cpp-wasi.md)
43+
- [Linking Modules](./examples-cpp-linking.md)
44+
- [Using Multi-Value](./examples-cpp-multi-value.md)
45+
- [`anyref` Reference Type](./examples-cpp-anyref.md)
46+
- [`externref` Reference Type](./examples-cpp-externref.md)
47+
- [Multiple Memories](./examples-cpp-multimemory.md)
48+
- [Serializing Modules](./examples-cpp-serialize.md)
49+
- [Multithreaded Embedding](./examples-cpp-multithreaded-embedding.md)
50+
- [Async Host Functions](./examples-cpp-async.md)
2751
- [Python](./lang-python.md)
2852
- [.NET](./lang-dotnet.md)
2953
- [Go](./lang-go.md)

docs/examples-c-anyref.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Working with `anyref`
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/main/examples/anyref.c
7+
8+
This example demonstrates using `anyref` values.
9+
10+
## `anyref.wat`
11+
12+
```wat
13+
{{#include ../examples/anyref.wat}}
14+
```
15+
16+
## `anyref.c`
17+
18+
```c
19+
{{#include ../examples/anyref.c}}
20+
```

docs/examples-c-embed.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/examples-c-externref.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Working with `externref`
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/main/examples/externref.c
7+
8+
This example shows how to pass opaque host references into and out of WebAssembly using `externref`.
9+
10+
## `externref.wat`
11+
12+
```wat
13+
{{#include ../examples/externref.wat}}
14+
```
15+
16+
## `externref.c`
17+
18+
```c
19+
{{#include ../examples/externref.c}}
20+
```

docs/examples-c-multimemory.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Working with Multiple Memories
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/main/examples/multimemory.c
7+
8+
This example demonstrates instantiating and interacting with a module that has multiple linear memories.
9+
10+
## `multimemory.wat`
11+
12+
```wat
13+
{{#include ../examples/multimemory.wat}}
14+
```
15+
16+
## `multimemory.c`
17+
18+
```c
19+
{{#include ../examples/multimemory.c}}
20+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Multithreaded Embedding
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/main/examples/threads.c
7+
8+
This example demonstrates using Wasmtime in multithreaded runtimes.
9+
10+
## `threads.wat`
11+
12+
```wat
13+
{{#include ../examples/threads.wat}}
14+
```
15+
16+
## `threads.c`
17+
18+
```c
19+
{{#include ../examples/threads.c}}
20+
```

0 commit comments

Comments
 (0)