Wasmtime
types/val.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_TYPES_VAL_HH
6#define WASMTIME_TYPES_VAL_HH
7
8#include <memory>
9#include <ostream>
10#include <wasm.h>
11#include <wasmtime/val.h>
12
13namespace wasmtime {
14
16enum class ValKind {
18 I32,
20 I64,
22 F32,
24 F64,
26 V128,
30 FuncRef,
32 AnyRef,
34 ExnRef,
35};
36
39#define WASMTIME_FOR_EACH_VAL_KIND(X) \
40 X(I32, "i32", WASM_I32) \
41 X(I64, "i64", WASM_I64) \
42 X(F32, "f32", WASM_F32) \
43 X(F64, "f64", WASM_F64) \
44 X(ExternRef, "externref", WASM_EXTERNREF) \
45 X(FuncRef, "funcref", WASM_FUNCREF) \
46 X(AnyRef, "anyref", WASMTIME_ANYREF) \
47 X(ExnRef, "exnref", WASMTIME_EXNREF) \
48 X(V128, "v128", WASMTIME_V128)
49
51inline std::ostream &operator<<(std::ostream &os, const ValKind &e) {
52 switch (e) {
53#define CASE_KIND_PRINT_NAME(kind, name, ignore) \
54 case ValKind::kind: \
55 os << name; \
56 break;
57 WASMTIME_FOR_EACH_VAL_KIND(CASE_KIND_PRINT_NAME)
58#undef CASE_KIND_PRINT_NAME
59 default:
60 abort();
61 }
62 return os;
63}
64
70class ValType {
71 friend class TableType;
72 friend class GlobalType;
73 friend class FuncType;
74 struct deleter {
75 void operator()(wasm_valtype_t *p) const { wasm_valtype_delete(p); }
76 };
77
78 std::unique_ptr<wasm_valtype_t, deleter> ptr;
79
80 static wasm_valkind_t kind_to_c(ValKind kind) {
81 switch (kind) {
82#define CASE_KIND_TO_C(kind, ignore, ckind) \
83 case ValKind::kind: \
84 return ckind;
85 WASMTIME_FOR_EACH_VAL_KIND(CASE_KIND_TO_C)
86#undef CASE_KIND_TO_C
87 default:
88 abort();
89 }
90 }
91
92public:
95 class Ref {
96 friend class ValType;
97
98 const wasm_valtype_t *ptr;
99
100 public:
102 Ref(const wasm_valtype_t *ptr) : ptr(ptr) {}
104 Ref(const ValType &ty) : Ref(ty.ptr.get()) {}
107 ValKind kind() const {
108 switch (wasm_valtype_kind(ptr)) {
109#define CASE_C_TO_KIND(kind, ignore, ckind) \
110 case ckind: \
111 return ValKind::kind;
112 WASMTIME_FOR_EACH_VAL_KIND(CASE_C_TO_KIND)
113#undef CASE_C_TO_KIND
114 }
115 std::abort();
116 }
117 };
121 class ListRef {
122 const wasm_valtype_vec_t *list;
124 public:
126 ListRef(const wasm_valtype_vec_t *list) : list(list) {}
127
129 typedef const Ref *iterator;
130
132 iterator begin() const {
133 return reinterpret_cast<Ref *>(&list->data[0]); // NOLINT
135
137 iterator end() const {
138 return reinterpret_cast<Ref *>(&list->data[list->size]); // NOLINT
140
142 size_t size() const { return list->size; }
143 };
144
145private:
146 Ref ref;
147 ValType(wasm_valtype_t *ptr) : ptr(ptr), ref(ptr) {}
149public:
151 ValType(ValKind kind) : ValType(wasm_valtype_new(kind_to_c(kind))) {}
153 ValType(Ref other) : ValType(wasm_valtype_copy(other.ptr)) {}
155 ValType(const ValType &other) : ValType(wasm_valtype_copy(other.ptr.get())) {}
157 ValType &operator=(const ValType &other) {
158 ptr.reset(wasm_valtype_copy(other.ptr.get()));
159 ref = other.ref;
160 return *this;
162 ~ValType() = default;
164 ValType(ValType &&other) = default;
166 ValType &operator=(ValType &&other) = default;
170 Ref *operator->() { return &ref; }
173 Ref *operator*() { return &ref; }
174};
175
176#undef WASMTIME_FOR_EACH_VAL_KIND
177
178}; // namespace wasmtime
179
180#endif // WASMTIME_TYPES_VAL_HH
Representation of a WebAssembly anyref value.
Definition: val.hh:112
Representation of a WebAssembly externref value.
Definition: val.hh:33
Non-owning reference to a list of ValType instances. Must not be used after the original owner is del...
Definition: types/val.hh:118
ListRef(const wasm_valtype_vec_t *list)
Creates a list from the raw underlying C API.
Definition: types/val.hh:123
size_t size() const
Returns how many types are in this list.
Definition: types/val.hh:139
iterator begin() const
Pointer to the beginning of iteration.
Definition: types/val.hh:129
iterator end() const
Pointer to the end of iteration.
Definition: types/val.hh:134
const Ref * iterator
This list iterates over a list of ValType::Ref instances.
Definition: types/val.hh:126
Non-owning reference to a ValType, must not be used after the original ValType is deleted.
Definition: types/val.hh:93
Ref(const ValType &ty)
Copy constructor.
Definition: types/val.hh:102
Ref(const wasm_valtype_t *ptr)
Instantiates from the raw C API representation.
Definition: types/val.hh:100
ValKind kind() const
Returns the corresponding "kind" for this type.
Definition: types/val.hh:105
Type information about a WebAssembly value.
Definition: types/val.hh:69
ValType & operator=(const ValType &other)
Copies the contents of another type into this one.
Definition: types/val.hh:154
Ref * operator->()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/val.hh:167
Ref * operator*()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/val.hh:170
std::ostream & operator<<(std::ostream &os, const Error &e)
Used to print an error.
Definition: error.hh:58
An object representing the type of a value.
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
Container for the v128 WebAssembly type.
Definition: val.hh:209
ValKind
Different kinds of types accepted by Wasmtime.
Definition: types/val.hh:16
@ F64
WebAssembly's f64 type.
@ ExnRef
WebAssembly's exnref type.
@ F32
WebAssembly's f32 type.
@ FuncRef
WebAssembly's funcref type from the reference types.
@ I32
WebAssembly's i32 type.
@ I64
WebAssembly's i64 type.
#define WASMTIME_FOR_EACH_VAL_KIND(X)
Definition: types/val.hh:39
void wasm_valtype_delete(wasm_valtype_t *)
Deletes a type.
uint8_t wasm_valkind_t
Different kinds of types supported in wasm.
Definition: wasm.h:185
wasm_valtype_t * wasm_valtype_copy(const wasm_valtype_t *)
Creates a new value which matches the provided one.
wasm_valkind_t wasm_valtype_kind(const wasm_valtype_t *)
Returns the associated kind for this value type.
wasm_valtype_t * wasm_valtype_new(wasm_valkind_t)
Creates a new value type from the specified kind.