Wasmtime
types/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 friend class TagType;
19
20 struct deleter {
21 void operator()(wasm_functype_t *p) const { wasm_functype_delete(p); }
22 };
23
24 std::unique_ptr<wasm_functype_t, deleter> ptr;
25
26public:
29 class Ref {
30 friend class FuncType;
31 const wasm_functype_t *ptr;
32
33 public:
35 Ref(const wasm_functype_t *ptr) : ptr(ptr) {}
37 Ref(const FuncType &ty) : Ref(ty.ptr.get()) {}
38
41
44 };
45
46private:
47 Ref ref;
48 FuncType(wasm_functype_t *ptr) : ptr(ptr), ref(ptr) {}
49
50public:
52 FuncType(std::initializer_list<ValType> params,
53 std::initializer_list<ValType> results)
54 : ref(nullptr) {
55 *this = FuncType::from_iters(params, results);
56 }
57
59 FuncType(Ref other) : FuncType(wasm_functype_copy(other.ptr)) {}
61 FuncType(const FuncType &other)
62 : FuncType(wasm_functype_copy(other.ptr.get())) {}
64 FuncType &operator=(const FuncType &other) {
65 ptr.reset(wasm_functype_copy(other.ptr.get()));
66 return *this;
67 }
68 ~FuncType() = default;
70 FuncType(FuncType &&other) = default;
72 FuncType &operator=(FuncType &&other) = default;
73
75 template <typename P, typename R>
76 static FuncType from_iters(P params, R results) {
77 wasm_valtype_vec_t param_vec;
78 wasm_valtype_vec_t result_vec;
79 wasm_valtype_vec_new_uninitialized(&param_vec, params.size());
80 wasm_valtype_vec_new_uninitialized(&result_vec, results.size());
81 size_t i = 0;
82
83 for (auto val : params) {
84 param_vec.data[i++] = val.ptr.release(); // NOLINT
85 }
86 i = 0;
87 for (auto val : results) {
88 result_vec.data[i++] = val.ptr.release(); // NOLINT
89 }
90
91 return wasm_functype_new(&param_vec, &result_vec);
92 }
93
96 Ref *operator->() { return &ref; }
99 Ref *operator*() { return &ref; }
100};
101
102}; // namespace wasmtime
103
104#endif // WASMTIME_TYPES_FUNC_HH
Definition: types/func.hh:29
Ref(const wasm_functype_t *ptr)
Creates a new reference from the underlying C API representation.
Definition: types/func.hh:35
ValType::ListRef results() const
Returns the list of types this function type returns.
Definition: types/func.hh:43
Ref(const FuncType &ty)
Creates a new reference to the given type.
Definition: types/func.hh:37
ValType::ListRef params() const
Returns the list of types this function type takes as parameters.
Definition: types/func.hh:40
Type information for a WebAssembly function.
Definition: types/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: types/func.hh:76
FuncType(const FuncType &other)
Copies another type's information into this one.
Definition: types/func.hh:61
Ref * operator->()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/func.hh:96
Ref * operator*()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/func.hh:99
FuncType & operator=(const FuncType &other)
Copies another type's information into this one.
Definition: types/func.hh:64
FuncType(Ref other)
Copies a reference into a uniquely owned function type.
Definition: types/func.hh:59
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: types/func.hh:52
FuncType & operator=(FuncType &&other)=default
Moves type information from another type into this one.
Representation of a WebAssembly function.
Definition: func.hh:336
Helper class for linking modules together with name-based resolution.
Definition: linker.hh:26
Type information for a WebAssembly exception tag.
Definition: tag.hh:20
Non-owning reference to a list of ValType instances. Must not be used after the original owner is del...
Definition: types/val.hh:115
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.