Skip to content

C++17 bindings (uses "modern" data types for option,result,resource) #1283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -40,6 +40,7 @@ wit-component = "0.229.0"

wit-bindgen-core = { path = 'crates/core', version = '0.41.0' }
wit-bindgen-c = { path = 'crates/c', version = '0.41.0' }
wit-bindgen-cpp = { path = 'crates/cpp', version = '0.41.0' }
wit-bindgen-rust = { path = "crates/rust", version = "0.41.0" }
wit-bindgen-csharp = { path = 'crates/csharp', version = '0.41.0' }
wit-bindgen-markdown = { path = 'crates/markdown', version = '0.41.0' }
Expand All @@ -56,6 +57,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 @@ -71,10 +73,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
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 @@ -3136,7 +3136,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.41.0"
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.
69 changes: 69 additions & 0 deletions crates/cpp/helper-types/wit-common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#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 {};
} // namespace wit
Loading
Loading