Wasmtime
func.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_TYPES_FUNC_HH
6#define WASMTIME_TYPES_FUNC_HH
7
9
10namespace wasmtime {
11
15class FuncType {
16 friend class Func;
17 friend class Linker;
18
19 struct deleter {
20 void operator()(wasm_functype_t *p) const { wasm_functype_delete(p); }
21 };
22
23 std::unique_ptr<wasm_functype_t, deleter> ptr;
24
25public:
28 class Ref {
29 friend class FuncType;
30 const wasm_functype_t *ptr;
31
32 public:
34 Ref(const wasm_functype_t *ptr) : ptr(ptr) {}
36 Ref(const FuncType &ty) : Ref(ty.ptr.get()) {}
37
40
43 };
44
45private:
46 Ref ref;
47 FuncType(wasm_functype_t *ptr) : ptr(ptr), ref(ptr) {}
48
49public:
51 FuncType(std::initializer_list<ValType> params,
52 std::initializer_list<ValType> results)
53 : ref(nullptr) {
54 *this = FuncType::from_iters(params, results);
55 }
56
58 FuncType(Ref other) : FuncType(wasm_functype_copy(other.ptr)) {}
60 FuncType(const FuncType &other)
61 : FuncType(wasm_functype_copy(other.ptr.get())) {}
63 FuncType &operator=(const FuncType &other) {
64 ptr.reset(wasm_functype_copy(other.ptr.get()));
65 return *this;
66 }
67 ~FuncType() = default;
69 FuncType(FuncType &&other) = default;
71 FuncType &operator=(FuncType &&other) = default;
72
74 template <typename P, typename R>
75 static FuncType from_iters(P params, R results) {
76 wasm_valtype_vec_t param_vec;
77 wasm_valtype_vec_t result_vec;
78 wasm_valtype_vec_new_uninitialized(&param_vec, params.size());
79 wasm_valtype_vec_new_uninitialized(&result_vec, results.size());
80 size_t i = 0;
81
82 for (auto val : params) {
83 param_vec.data[i++] = val.ptr.release(); // NOLINT
84 }
85 i = 0;
86 for (auto val : results) {
87 result_vec.data[i++] = val.ptr.release(); // NOLINT
88 }
89
90 return wasm_functype_new(&param_vec, &result_vec);
91 }
92
95 Ref *operator->() { return &ref; }
98 Ref *operator*() { return &ref; }
99};
100
101}; // namespace wasmtime
102
103#endif // WASMTIME_TYPES_FUNC_HH
Definition: func.hh:28
Ref(const wasm_functype_t *ptr)
Creates a new reference from the underlying C API representation.
Definition: func.hh:34
ValType::ListRef results() const
Returns the list of types this function type returns.
Definition: func.hh:42
Ref(const FuncType &ty)
Creates a new reference to the given type.
Definition: func.hh:36
ValType::ListRef params() const
Returns the list of types this function type takes as parameters.
Definition: func.hh:39
Type information for a WebAssembly function.
Definition: func.hh:15
static FuncType from_iters(P params, R results)
Creates a new function type from the given list of parameters and results.
Definition: func.hh:75
FuncType(const FuncType &other)
Copies another type's information into this one.
Definition: func.hh:60
Ref * operator->()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: func.hh:95
Ref * operator*()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: func.hh:98
FuncType & operator=(const FuncType &other)
Copies another type's information into this one.
Definition: func.hh:63
FuncType(Ref other)
Copies a reference into a uniquely owned function type.
Definition: func.hh:58
FuncType(FuncType &&other)=default
Moves type information from another type into this one.
FuncType(std::initializer_list< ValType > params, std::initializer_list< ValType > results)
Creates a new function type from the given list of parameters and results.
Definition: func.hh:51
FuncType & operator=(FuncType &&other)=default
Moves type information from another type into this one.
Representation of a WebAssembly function.
Definition: wasmtime.hh:347
Helper class for linking modules together with name-based resolution.
Definition: wasmtime.hh:1061
Non-owning reference to a list of ValType instances. Must not be used after the original owner is del...
Definition: types/val.hh:116
An opaque object representing the type of a function.
A list of wasm_valtype_t values.
Definition: wasm.h:183
wasm_valtype_t ** data
Pointer to the base of this vector.
Definition: wasm.h:183
size_t size
Length of this vector.
Definition: wasm.h:183
wasm_functype_t * wasm_functype_copy(const wasm_functype_t *)
Creates a new value which matches the provided one.
void wasm_functype_delete(wasm_functype_t *)
Deletes a type.
const wasm_valtype_vec_t * wasm_functype_results(const wasm_functype_t *)
Returns the list of results of this function type.
const wasm_valtype_vec_t * wasm_functype_params(const wasm_functype_t *)
Returns the list of parameters of this function type.
wasm_functype_t * wasm_functype_new(wasm_valtype_vec_t *params, wasm_valtype_vec_t *results)
Creates a new function type with the provided parameter and result types.
void wasm_valtype_vec_new_uninitialized(wasm_valtype_vec_t *out, size_t)
Creates a vector with the given capacity.