Skip to content

Commit 6f8f631

Browse files
alexcrichtondicej
andauthored
[39.0.0] Fill out type information for components in C API (#11988)
* Simplify C++ binding definitions (#11936) This commit adds a shared macro to simplify ownership management in the C++ API and to additionally have a uniform API across types. This is inspired by the component model work where I felt like I was copy/pasting quite a lot and wanted to cut down on that. * Fill out type information for components in C API (#11937) * Fill out type information for components in C API I've concluded that I'll want this for wasmtime-py so this fills out type reflection for various items in the C API. This then additionally extends the C++ API as well. prtest:full * Update crates/c-api/include/wasmtime/component/types/func.h Co-authored-by: Joel Dice <[email protected]> --------- Co-authored-by: Joel Dice <[email protected]> --------- Co-authored-by: Joel Dice <[email protected]>
1 parent efd56f6 commit 6f8f631

Some content is hidden

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

51 files changed

+3460
-506
lines changed

crates/c-api/include/wasmtime/component.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <wasmtime/component/func.h>
66
#include <wasmtime/component/instance.h>
77
#include <wasmtime/component/linker.h>
8+
#include <wasmtime/component/types.h>
89
#include <wasmtime/component/val.h>
910

1011
#endif // WASMTIME_COMPONENT_H

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <wasmtime/component/func.hh>
88
#include <wasmtime/component/instance.hh>
99
#include <wasmtime/component/linker.hh>
10+
#include <wasmtime/component/types.hh>
1011
#include <wasmtime/component/val.hh>
1112

1213
#endif // WASMTIME_COMPONENT_HH

crates/c-api/include/wasmtime/component/component.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define WASMTIME_COMPONENT_COMPONENT_H
55

66
#include <wasm.h>
7+
#include <wasmtime/component/types/component.h>
78
#include <wasmtime/conf.h>
89
#include <wasmtime/error.h>
910

@@ -102,6 +103,15 @@ wasmtime_component_deserialize_file(const wasm_engine_t *engine,
102103
WASM_API_EXTERN wasmtime_component_t *
103104
wasmtime_component_clone(const wasmtime_component_t *component);
104105

106+
/**
107+
* \brief Returns the type of this component.
108+
*
109+
* The returned pointer must be deallocatd with
110+
* `wasmtime_component_type_delete`.
111+
*/
112+
WASM_API_EXTERN wasmtime_component_type_t *
113+
wasmtime_component_type(const wasmtime_component_t *component);
114+
105115
/**
106116
* \brief Deletes a #wasmtime_component_t created by #wasmtime_component_new
107117
*

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

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <string_view>
1515
#include <vector>
1616
#include <wasmtime/component/component.h>
17+
#include <wasmtime/component/types/component.hh>
1718
#include <wasmtime/engine.hh>
1819
#include <wasmtime/error.hh>
1920
#include <wasmtime/span.hh>
@@ -29,76 +30,14 @@ namespace component {
2930
* instances.
3031
*/
3132
class ExportIndex {
32-
friend class Component;
33-
34-
struct deleter {
35-
void operator()(wasmtime_component_export_index_t *p) const {
36-
wasmtime_component_export_index_delete(p);
37-
}
38-
};
39-
40-
std::unique_ptr<wasmtime_component_export_index_t, deleter> ptr;
41-
42-
public:
43-
/// \brief Constructs an ExportIndex from the underlying C API struct.
44-
explicit ExportIndex(wasmtime_component_export_index_t *raw) : ptr(raw) {}
45-
46-
/// Copies another index into this one.
47-
ExportIndex(const ExportIndex &other)
48-
: ptr(wasmtime_component_export_index_clone(other.ptr.get())) {}
49-
/// Copies another index into this one.
50-
ExportIndex &operator=(const ExportIndex &other) {
51-
ptr.reset(wasmtime_component_export_index_clone(other.ptr.get()));
52-
return *this;
53-
}
54-
55-
~ExportIndex() = default;
56-
/// Moves resources from another component into this one.
57-
ExportIndex(ExportIndex &&other) = default;
58-
/// Moves resources from another component into this one.
59-
ExportIndex &operator=(ExportIndex &&other) = default;
60-
61-
/// \brief Returns the underlying C API pointer.
62-
const wasmtime_component_export_index_t *capi() const { return ptr.get(); }
63-
64-
/// \brief Returns the underlying C API pointer.
65-
wasmtime_component_export_index_t *capi() { return ptr.get(); }
33+
WASMTIME_CLONE_WRAPPER(ExportIndex, wasmtime_component_export_index);
6634
};
6735

6836
/**
6937
* \brief Representation of a compiled WebAssembly component.
7038
*/
7139
class Component {
72-
struct deleter {
73-
void operator()(wasmtime_component_t *p) const {
74-
wasmtime_component_delete(p);
75-
}
76-
};
77-
78-
std::unique_ptr<wasmtime_component_t, deleter> ptr;
79-
80-
Component(wasmtime_component_t *raw) : ptr(raw) {}
81-
82-
public:
83-
/// Copies another component into this one.
84-
Component(const Component &other)
85-
: ptr(wasmtime_component_clone(other.ptr.get())) {}
86-
/// Copies another component into this one.
87-
Component &operator=(const Component &other) {
88-
ptr.reset(wasmtime_component_clone(other.ptr.get()));
89-
return *this;
90-
}
91-
~Component() = default;
92-
/// Moves resources from another component into this one.
93-
Component(Component &&other) = default;
94-
/// Moves resources from another component into this one.
95-
Component &operator=(Component &&other) = default;
96-
97-
/// \brief Returns the underlying C API pointer.
98-
const wasmtime_component_t *capi() const { return ptr.get(); }
99-
100-
/// \brief Returns the underlying C API pointer.
101-
wasmtime_component_t *capi() { return ptr.get(); }
40+
WASMTIME_CLONE_WRAPPER(Component, wasmtime_component);
10241

10342
#ifdef WASMTIME_FEATURE_COMPILER
10443
/**
@@ -221,6 +160,11 @@ public:
221160
}
222161
return std::nullopt;
223162
};
163+
164+
/// \brief Returns the type of this component.
165+
ComponentType type() const {
166+
return ComponentType(wasmtime_component_type(ptr.get()));
167+
}
224168
};
225169

226170
} // namespace component

crates/c-api/include/wasmtime/component/func.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#ifndef WASMTIME_COMPONENT_FUNC_H
44
#define WASMTIME_COMPONENT_FUNC_H
55

6+
#include <wasmtime/component/types/func.h>
67
#include <wasmtime/component/val.h>
78
#include <wasmtime/conf.h>
89
#include <wasmtime/error.h>
@@ -33,6 +34,14 @@ typedef struct wasmtime_component_func {
3334
uint32_t __private2;
3435
} wasmtime_component_func_t;
3536

37+
/// \brief Returns the type of this function.
38+
///
39+
/// The caller must deallocate the returned pointer with
40+
/// `wasmtime_component_func_type_delete`.
41+
WASM_API_EXTERN wasmtime_component_func_type_t *
42+
wasmtime_component_func_type(const wasmtime_component_func_t *func,
43+
wasmtime_context_t *context);
44+
3645
/**
3746
* \brief Invokes \p func with the \p args given and returns the result.
3847
*

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <string_view>
1111
#include <wasmtime/component/func.h>
12+
#include <wasmtime/component/types/func.hh>
1213
#include <wasmtime/component/val.hh>
1314
#include <wasmtime/error.hh>
1415
#include <wasmtime/span.hh>
@@ -54,6 +55,11 @@ public:
5455
}
5556
return std::monostate();
5657
}
58+
59+
/// \brief Returns the type of this function.
60+
FuncType type(Store::Context cx) const {
61+
return FuncType(wasmtime_component_func_type(&func, cx.capi()));
62+
}
5763
};
5864

5965
} // namespace component

crates/c-api/include/wasmtime/component/linker.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <wasm.h>
77
#include <wasmtime/component/component.h>
88
#include <wasmtime/component/instance.h>
9+
#include <wasmtime/component/types/func.h>
910
#include <wasmtime/conf.h>
1011
#include <wasmtime/error.h>
1112
#include <wasmtime/module.h>
@@ -134,8 +135,8 @@ WASM_API_EXTERN wasmtime_error_t *wasmtime_component_linker_instance_add_module(
134135

135136
/// Type of the callback used in #wasmtime_component_linker_instance_add_func
136137
typedef wasmtime_error_t *(*wasmtime_component_func_callback_t)(
137-
void *, wasmtime_context_t *, const wasmtime_component_val_t *, size_t,
138-
wasmtime_component_val_t *, size_t);
138+
void *, wasmtime_context_t *, const wasmtime_component_func_type_t *,
139+
wasmtime_component_val_t *, size_t, wasmtime_component_val_t *, size_t);
139140

140141
/**
141142
* \brief Define a function within this instance.

crates/c-api/include/wasmtime/component/linker.hh

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,7 @@ namespace component {
2626
* `Linker` can also be used to link in WASI functions to instantiate a module.
2727
*/
2828
class LinkerInstance {
29-
struct deleter {
30-
void operator()(wasmtime_component_linker_instance_t *p) const {
31-
wasmtime_component_linker_instance_delete(p);
32-
}
33-
};
34-
35-
std::unique_ptr<wasmtime_component_linker_instance_t, deleter> ptr;
36-
37-
public:
38-
/// Creates a new linker instance from the given C API pointer.
39-
explicit LinkerInstance(wasmtime_component_linker_instance_t *ptr)
40-
: ptr(ptr) {}
41-
42-
/// \brief Returns the underlying C API pointer.
43-
const wasmtime_component_linker_instance_t *capi() const { return ptr.get(); }
44-
45-
/// \brief Returns the underlying C API pointer.
46-
wasmtime_component_linker_instance_t *capi() { return ptr.get(); }
29+
WASMTIME_OWN_WRAPPER(LinkerInstance, wasmtime_component_linker_instance);
4730

4831
/**
4932
* \brief Adds a module to this linker instance under the specified name.
@@ -78,17 +61,22 @@ private:
7861
template <typename F>
7962
static wasmtime_error_t *
8063
raw_callback(void *env, wasmtime_context_t *store,
81-
const wasmtime_component_val_t *args, size_t nargs,
64+
const wasmtime_component_func_type_t *ty_const,
65+
wasmtime_component_val_t *args, size_t nargs,
8266
wasmtime_component_val_t *results, size_t nresults) {
8367
static_assert(alignof(Val) == alignof(wasmtime_component_val_t));
8468
static_assert(sizeof(Val) == sizeof(wasmtime_component_val_t));
69+
wasmtime_component_func_type_t *ty =
70+
const_cast<wasmtime_component_func_type_t *>(ty_const);
8571
F *func = reinterpret_cast<F *>(env);
86-
Span<const Val> args_span(Val::from_capi(args), nargs);
72+
Span<Val> args_span(Val::from_capi(args), nargs);
8773
Span<Val> results_span(Val::from_capi(results), nresults);
8874
Result<std::monostate> result =
89-
(*func)(Store::Context(store), args_span, results_span);
75+
(*func)(Store::Context(store), *FuncType::from_capi(&ty), args_span,
76+
results_span);
77+
9078
if (!result) {
91-
return result.err().release();
79+
return result.err().capi_release();
9280
}
9381
return nullptr;
9482
}
@@ -102,7 +90,7 @@ public:
10290
template <typename F,
10391
std::enable_if_t<
10492
std::is_invocable_r_v<Result<std::monostate>, F, Store::Context,
105-
Span<const Val>, Span<Val>>,
93+
const FuncType &, Span<Val>, Span<Val>>,
10694
bool> = true>
10795
Result<std::monostate> add_func(std::string_view name, F &&f) {
10896
auto *error = wasmtime_component_linker_instance_add_func(
@@ -127,7 +115,7 @@ private:
127115
F *func = reinterpret_cast<F *>(env);
128116
Result<std::monostate> result = (*func)(Store::Context(store), rep);
129117
if (!result) {
130-
return result.err().release();
118+
return result.err().capi_release();
131119
}
132120
return nullptr;
133121
}
@@ -160,15 +148,8 @@ public:
160148
* \brief Class used to instantiate a `Component` into an instance.
161149
*/
162150
class Linker {
163-
struct deleter {
164-
void operator()(wasmtime_component_linker_t *p) const {
165-
wasmtime_component_linker_delete(p);
166-
}
167-
};
168-
169-
std::unique_ptr<wasmtime_component_linker_t, deleter> ptr;
151+
WASMTIME_OWN_WRAPPER(Linker, wasmtime_component_linker);
170152

171-
public:
172153
/// Creates a new linker which will instantiate in the given engine.
173154
explicit Linker(Engine &engine)
174155
: ptr(wasmtime_component_linker_new(engine.capi())) {}
@@ -234,12 +215,6 @@ public:
234215
return std::monostate();
235216
}
236217
#endif // WASMTIME_FEATURE_WASI
237-
238-
/// \brief Returns the underlying C API pointer.
239-
const wasmtime_component_linker_t *capi() const { return ptr.get(); }
240-
241-
/// \brief Returns the underlying C API pointer.
242-
wasmtime_component_linker_t *capi() { return ptr.get(); }
243218
};
244219

245220
} // namespace component
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// \file wasmtime/component/types.h
2+
3+
#ifndef WASMTIME_COMPONENT_TYPES_H
4+
#define WASMTIME_COMPONENT_TYPES_H
5+
6+
#include <wasmtime/component/types/component.h>
7+
#include <wasmtime/component/types/func.h>
8+
#include <wasmtime/component/types/instance.h>
9+
#include <wasmtime/component/types/module.h>
10+
#include <wasmtime/component/types/resource.h>
11+
#include <wasmtime/component/types/val.h>
12+
13+
#endif // WASMTIME_COMPONENT_TYPES_H
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// \file wasmtime/component/types.hh
2+
3+
#ifndef WASMTIME_COMPONENT_TYPES_HH
4+
#define WASMTIME_COMPONENT_TYPES_HH
5+
6+
#include <wasmtime/component/types/component.hh>
7+
#include <wasmtime/component/types/func.hh>
8+
#include <wasmtime/component/types/instance.hh>
9+
#include <wasmtime/component/types/module.hh>
10+
#include <wasmtime/component/types/val.hh>
11+
12+
#endif // WASMTIME_COMPONENT_TYPES_HH

0 commit comments

Comments
 (0)