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,
33};
34
37#define WASMTIME_FOR_EACH_VAL_KIND(X) \
38 X(I32, "i32", WASM_I32) \
39 X(I64, "i64", WASM_I64) \
40 X(F32, "f32", WASM_F32) \
41 X(F64, "f64", WASM_F64) \
42 X(ExternRef, "externref", WASM_EXTERNREF) \
43 X(FuncRef, "funcref", WASM_FUNCREF) \
44 X(AnyRef, "anyref", WASMTIME_ANYREF) \
45 X(V128, "v128", WASMTIME_V128)
46
48inline std::ostream &operator<<(std::ostream &os, const ValKind &e) {
49 switch (e) {
50#define CASE_KIND_PRINT_NAME(kind, name, ignore) \
51 case ValKind::kind: \
52 os << name; \
53 break;
54 WASMTIME_FOR_EACH_VAL_KIND(CASE_KIND_PRINT_NAME)
55#undef CASE_KIND_PRINT_NAME
56 default:
57 abort();
58 }
59 return os;
60}
61
67class ValType {
68 friend class TableType;
69 friend class GlobalType;
70 friend class FuncType;
71
72 struct deleter {
73 void operator()(wasm_valtype_t *p) const { wasm_valtype_delete(p); }
74 };
75
76 std::unique_ptr<wasm_valtype_t, deleter> ptr;
77
78 static wasm_valkind_t kind_to_c(ValKind kind) {
79 switch (kind) {
80#define CASE_KIND_TO_C(kind, ignore, ckind) \
81 case ValKind::kind: \
82 return ckind;
83 WASMTIME_FOR_EACH_VAL_KIND(CASE_KIND_TO_C)
84#undef CASE_KIND_TO_C
85 default:
86 abort();
87 }
88 }
89
90public:
93 class Ref {
94 friend class ValType;
95
96 const wasm_valtype_t *ptr;
97
98 public:
100 Ref(const wasm_valtype_t *ptr) : ptr(ptr) {}
102 Ref(const ValType &ty) : Ref(ty.ptr.get()) {}
105 ValKind kind() const {
106 switch (wasm_valtype_kind(ptr)) {
107#define CASE_C_TO_KIND(kind, ignore, ckind) \
108 case ckind: \
109 return ValKind::kind;
110 WASMTIME_FOR_EACH_VAL_KIND(CASE_C_TO_KIND)
111#undef CASE_C_TO_KIND
112 }
113 std::abort();
114 }
115 };
119 class ListRef {
120 const wasm_valtype_vec_t *list;
122 public:
124 ListRef(const wasm_valtype_vec_t *list) : list(list) {}
125
127 typedef const Ref *iterator;
128
130 iterator begin() const {
131 return reinterpret_cast<Ref *>(&list->data[0]); // NOLINT
133
135 iterator end() const {
136 return reinterpret_cast<Ref *>(&list->data[list->size]); // NOLINT
138
140 size_t size() const { return list->size; }
141 };
142
143private:
144 Ref ref;
145 ValType(wasm_valtype_t *ptr) : ptr(ptr), ref(ptr) {}
147public:
149 ValType(ValKind kind) : ValType(wasm_valtype_new(kind_to_c(kind))) {}
151 ValType(Ref other) : ValType(wasm_valtype_copy(other.ptr)) {}
153 ValType(const ValType &other) : ValType(wasm_valtype_copy(other.ptr.get())) {}
155 ValType &operator=(const ValType &other) {
156 ptr.reset(wasm_valtype_copy(other.ptr.get()));
157 ref = other.ref;
158 return *this;
160 ~ValType() = default;
162 ValType(ValType &&other) = default;
164 ValType &operator=(ValType &&other) = default;
168 Ref *operator->() { return &ref; }
171 Ref *operator*() { return &ref; }
172};
173
174#undef WASMTIME_FOR_EACH_VAL_KIND
175
176}; // namespace wasmtime
177
178#endif // WASMTIME_TYPES_VAL_HH
Representation of a WebAssembly anyref value.
Definition: val.hh:82
Representation of a WebAssembly externref value.
Definition: val.hh:28
Non-owning reference to a list of ValType instances. Must not be used after the original owner is del...
Definition: types/val.hh:116
ListRef(const wasm_valtype_vec_t *list)
Creates a list from the raw underlying C API.
Definition: types/val.hh:121
size_t size() const
Returns how many types are in this list.
Definition: types/val.hh:137
iterator begin() const
Pointer to the beginning of iteration.
Definition: types/val.hh:127
iterator end() const
Pointer to the end of iteration.
Definition: types/val.hh:132
const Ref * iterator
This list iterates over a list of ValType::Ref instances.
Definition: types/val.hh:124
Non-owning reference to a ValType, must not be used after the original ValType is deleted.
Definition: types/val.hh:91
Ref(const ValType &ty)
Copy constructor.
Definition: types/val.hh:100
Ref(const wasm_valtype_t *ptr)
Instantiates from the raw C API representation.
Definition: types/val.hh:98
ValKind kind() const
Returns the corresponding "kind" for this type.
Definition: types/val.hh:103
Type information about a WebAssembly value.
Definition: types/val.hh:66
ValType & operator=(const ValType &other)
Copies the contents of another type into this one.
Definition: types/val.hh:152
Ref * operator->()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/val.hh:165
Ref * operator*()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/val.hh:168
std::ostream & operator<<(std::ostream &os, const Error &e)
Used to print an error.
Definition: error.hh:71
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:135
ValKind
Different kinds of types accepted by Wasmtime.
Definition: types/val.hh:16
@ F64
WebAssembly's f64 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:37
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.