Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
4138036
initial c++ skeleton
cpetig Apr 20, 2025
e55da99
reduce code (no host, no symmetric)
cpetig Apr 20, 2025
1547c53
c++ generator compiles
cpetig Apr 21, 2025
e6a3e44
skeletal c++ testing support
cpetig Apr 27, 2025
a807a30
proper include
cpetig Apr 27, 2025
013633c
post merge fixes
cpetig May 7, 2025
984e9a2
c++ numbers test works
cpetig May 8, 2025
c8a7e11
many-arguments c++ test works
cpetig May 8, 2025
e741941
more test changes
cpetig May 8, 2025
2ccd752
records test passes
cpetig May 10, 2025
a17ab72
records test passes for c++
cpetig May 10, 2025
0bb5a03
numbers ok with c++17
cpetig May 10, 2025
a4cae5d
many arguments cover both sides
cpetig May 10, 2025
1b8574e
strings test passes
cpetig May 10, 2025
7ff30cb
fix options test
cpetig May 10, 2025
66bb0c6
fix old string test
cpetig May 10, 2025
ca017da
list test (in progress)
cpetig May 10, 2025
e7ad032
fix list test
cpetig May 10, 2025
f077354
incomplete results test
cpetig May 10, 2025
3f8a7b6
better variant support
cpetig May 13, 2025
77afda0
small fix
cpetig May 13, 2025
c3621c5
use c++17 to compile
cpetig May 13, 2025
148b9eb
remove unnecessary move
cpetig May 18, 2025
b3ed215
variant lowering
cpetig May 18, 2025
3f34046
correct record lifting
cpetig May 18, 2025
6bd2c34
fix variant lifting
cpetig May 18, 2025
4c6cd5c
results test passing (but incomplete)
cpetig May 18, 2025
502d35e
results test fully operational
cpetig May 18, 2025
7c2e9f2
Opt out of language C++17 for cpp extension
cpetig May 18, 2025
dca3cd8
variants work
cpetig May 18, 2025
fcd9cea
variants runner works
cpetig May 18, 2025
04637a6
clean up lists test
cpetig May 18, 2025
fe8c0de
I need a better strategy to copy the headers to the target
cpetig May 19, 2025
13c97af
not elegant but working solution
cpetig May 19, 2025
dbdfb71
working resources
cpetig May 23, 2025
3d0c1fe
more complex resource test
cpetig May 23, 2025
1ca600d
remove unused functions
cpetig May 23, 2025
f875e2b
generate template files in the output directory
cpetig May 23, 2025
ac4d022
Merge branch 'main' of https://github.com/bytecodealliance/wit-bindge…
cpetig Jun 2, 2025
cd6c8f9
simplifications and clippy fixes
cpetig Jun 2, 2025
bb600b8
this was never meant to be checked in
cpetig Jun 2, 2025
91ca5de
cargo fmt again
cpetig Jun 2, 2025
6926a23
Merge branch 'main' of https://github.com/bytecodealliance/wit-bindge…
cpetig Jun 4, 2025
846fb06
Merge branch 'main' of https://github.com/bytecodealliance/wit-bindge…
cpetig Jun 12, 2025
21a8f36
fix publishing logic
cpetig Jun 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ wasm-compose = "0.234.0"

wit-bindgen-core = { path = 'crates/core', version = '0.42.1' }
wit-bindgen-c = { path = 'crates/c', version = '0.42.1' }
wit-bindgen-cpp = { path = 'crates/cpp', version = '0.42.1' }
wit-bindgen-rust = { path = "crates/rust", version = "0.42.1" }
wit-bindgen-csharp = { path = 'crates/csharp', version = '0.42.1' }
wit-bindgen-markdown = { path = 'crates/markdown', version = '0.42.1' }
Expand All @@ -57,6 +58,7 @@ clap = { workspace = true, features = ['wrap_help'] }
wit-bindgen-core = { workspace = true }
wit-bindgen-rust = { workspace = true, features = ['clap'], optional = true }
wit-bindgen-c = { workspace = true, features = ['clap'], optional = true }
wit-bindgen-cpp = { workspace = true, features = ['clap'], optional = true }
wit-bindgen-markdown = { workspace = true, features = ['clap'], optional = true }
wit-bindgen-moonbit = { workspace = true, features = ['clap'], optional = true }
wit-bindgen-csharp = { workspace = true, features = ['clap'], optional = true }
Expand All @@ -72,10 +74,12 @@ default = [
'markdown',
'go',
'csharp',
'cpp',
'moonbit',
'async',
]
c = ['dep:wit-bindgen-c']
cpp = ['dep:wit-bindgen-cpp']
rust = ['dep:wit-bindgen-rust']
markdown = ['dep:wit-bindgen-markdown']
go = []
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,14 @@ Then, you can generate the bindings for your project:
wit-bindgen-go generate <path-to-wit-pkg>
```

### Guest: C++-17+

The cpp crate contains code to generate C++ code which uses the std types
optional, string, string_view, vector, expected to represent generic
WIT types.

This relies on wasi-SDK for guest compilation.

### Guest: MoonBit

MoonBit can be compiled to WebAssembly using [its toolchain](https://moonbitlang.com/download):
Expand Down
1 change: 1 addition & 0 deletions ci/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::time::Duration;
const CRATES_TO_PUBLISH: &[&str] = &[
"wit-bindgen-core",
"wit-bindgen-c",
"wit-bindgen-cpp",
"wit-bindgen-rust",
"wit-bindgen-csharp",
"wit-bindgen-markdown",
Expand Down
4 changes: 2 additions & 2 deletions crates/c/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod component_type_object;
pub mod component_type_object;

use anyhow::Result;
use heck::*;
Expand Down Expand Up @@ -3864,7 +3864,7 @@ impl Source {
}
}

fn wasm_type(ty: WasmType) -> &'static str {
pub fn wasm_type(ty: WasmType) -> &'static str {
match ty {
WasmType::I32 => "int32_t",
WasmType::I64 => "int64_t",
Expand Down
27 changes: 27 additions & 0 deletions crates/cpp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "wit-bindgen-cpp"
authors = ["Christof Petig <[email protected]>"]
version = "0.42.1"
edition.workspace = true
repository = 'https://github.com/cpetig/wit-bindgen'
license = "Apache-2.0 WITH LLVM-exception"
description = """
C++ guest and host binding generator for WIT and the component model.
"""

[lib]
doctest = false
test = false

[dependencies]
wit-bindgen-core = { workspace = true }
wit-component = { workspace = true }
wasm-encoder = { workspace = true }
wasm-metadata = { workspace = true }
wit-bindgen-c = { workspace = true }
anyhow = { workspace = true }
heck = { workspace = true }
clap = { workspace = true, optional = true }

[dev-dependencies]
test-helpers = { path = '../test-helpers' }
98 changes: 98 additions & 0 deletions crates/cpp/DESIGN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Type mapping

| Code | Environment |
| --- | --- |
| G-- | guest side |
| H-- | host side |
| -I- | guest-import (guest calls) |
| -E- | guest-export (host calls) |
| --A | argument |
| --R | result |
| --S | in struct |

| mode | |
| --- | --- |
| v | passed by value |
| t | owernership transferred |
| p | cabi_post_ cleans up |

| API | | | ABI | |
| --- | --- | --- | --- | --- |
| 🕸 | old | | 📘 | canonical |
| 💎 | new | | 🪞 | symmetric |

| Code | mode | WIT Type | Rust type | C++ Type | Lower | Reason |
| --- | --- | --- | --- | --- | --- | --- |
| GIA | v | string | &str[^1] | string_view (17) | addr, len | |
| | | list | &[T] | wit::span [^5] | addr, len | |
| | | tuple | (...) | std::tuple | 0, 1, ...| |
| | | tuple<string, list> | (&str, &[T]) | std::tuple<...> | a,l,a,l |
| | | record{string, list} | &T | T const& | a,l,a,l |
| | | large-struct (>16 args) | &T | T const& | &t |
| | | result<string,list> | Result<&str, &[]> | std::expected<string_view, span> | d,a,l |
| | | option\<string> | Option\<&str> | optional<string_view> const& | d,a,l|
| | | list\<resrc> | &[\&Resrc]? | vector<string_view> const& | a,l|
| GIR | t | string | String | wit::string[^2] | &(addr, len) [^8] | |
| | | list | Vec | wit::vector | &(a,l) |
| | | result<string,list> | Result<String, Vec> | std::expected<wit::string, wit::vector> | &(d,a,l) |
| GEA | t | string | String | 🕸 wit::string&& | addr, len |
| | | | | 💎 string_view | |
| | | result<string,list> | Result<String, Vec> | 🕸 std::expected<wit::string, wit::vector>&& | d,a,l |
| | | | | 💎 std::expected<string_view, wit::span> | |
| GER | p | string | String | wit::string (or std?) | 📘 -> &(a,l) cabi_post_N:P/I#F [^7] |
| | | | | | 🪞 &(a,l) |
| | | result<string,list> | Result<String, Vec> | std::expected<wit::string, wit::vector> | 📘 -> &(d,a,l) cabi_post |
| --S | ? | string | String | wit::string | addr, len |
| HIA | v | string | | string_view | a,l |
| HIR | t | string | | wit::string[^3] | &(a,l) |
| HEA | t | string | | 🕸 wit::string[^4] | a,l |
| | | | | 💎 string_view [^6] | |
| HER | p | string | | 🕸 wit::guest_owned<string_view> | 📘 -> &(a,l) |
| | | | | 💎 wit::string [^6] | 🪞 &(a,l) |

[^1]: The host never frees memory (is never passed ownership)!

[^2]: A wit::string is identical to the canonical representation, so it can be part of structures. On the guest a wit::string owns the memory and frees it after use.
On the host a wit::string can be constructed(=allocated) with an exec_env argument. Thus, without an exec_env a wit::string on the host is inaccessible.
Complex (non-POD) struct elements on the host will need exec_env to decode or construct.

[^3]: A wit::string requires exec_env inside the host implementation. ~~Perhaps a flexible type (either std::string or wit::string would be possible), or make this a generation option?~~ std::string requires a copy, wit::string requires passing exec_env to the method (which is necessary for methods anyway).

[^4]: A host side wit::string doesn't own the data (not free in dtor), thus no move semantics.

[^5]: std::span requires C++-20, this alias should give minimal functionality with older compiler targets.

[^6]: Not implemented, for now symmetric is priority

[^7]: Here the callee (guest) allocates the memory for the set on its side

[^8]: Caller passes address of the return object as argument

## [Symmetric ABI](https://github.com/WebAssembly/component-model/issues/386)

The idea is to directly connect (link) components to each other.

Thus imported and exported functions and resources need to be compatible
at the ABI level.

For now for functions the guest import convention is used in both directions:

- The imported function ABI is used with the following properties

- (unchanged) List and string arguments are passed as Views, no free
required, lifetime is constrained until the end of the call

- (unchanged) Owned resources in arguments or results pass ownership
to the callee

- (unchanged) If there are too many (>1) flat results, a local
uninitialized ret_area is passed via the last argument

- (unchanged) Returned objects are owned.
For functional safety, i.e. avoiding all
allocations in the hot path, the hope is with [#385](https://github.com/WebAssembly/component-model/issues/385).

- The imported resource ABI is used also for exporting
with one modification:

Resource IDs become usize, so you can optimize the resource table away.
75 changes: 75 additions & 0 deletions crates/cpp/helper-types/wit-common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#pragma once

#include <assert.h>
#include <map>
#include <optional>
#include <stddef.h> // size_t
#include <stdint.h>
#if __cplusplus > 202001L
#include <span>
#else
#include <vector>
#endif

namespace wit {
#if __cplusplus > 202001L
using std::span;
#else
/// Minimal span (vector view) implementation for older C++ environments
template <class T> class span {
T const *address;
size_t length;

public:
T const *data() const { return address; }
size_t size() const { return length; }

typedef T const *const_iterator;

const_iterator begin() const { return address; }
const_iterator end() const { return address + length; }
bool empty() const { return !length; }
T const &operator[](size_t index) const { return address[index]; }
span(T *a, size_t l) : address(a), length(l) {}
// create from any compatible vector (borrows data!)
template <class U>
span(std::vector<U> const &vec) : address(vec.data()), length(vec.size()) {}
};
#endif

/// @brief Helper class to map between IDs and resources
/// @tparam R Type of the Resource
template <class R> class ResourceTable {
static std::map<int32_t, R> resources;

public:
static R *lookup_resource(int32_t id) {
auto result = resources.find(id);
return result == resources.end() ? nullptr : &result->second;
}
static int32_t store_resource(R &&value) {
auto last = resources.rbegin();
int32_t id = last == resources.rend() ? 0 : last->first + 1;
resources.insert(std::pair<int32_t, R>(id, std::move(value)));
return id;
}
static std::optional<R> remove_resource(int32_t id) {
auto iter = resources.find(id);
std::optional<R> result;
if (iter != resources.end()) {
result = std::move(iter->second);
resources.erase(iter);
}
return std::move(result);
}
};

/// @brief Replaces void in the error position of a result
struct Void {};

template<class To, class From>
constexpr To bit_cast(const From& from) noexcept {
union Bits { From from; To to; };
Bits b; b.from = from; return b.to;
}
} // namespace wit
Loading