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 struct deleter {
72 void operator()(wasm_valtype_t *p) const { wasm_valtype_delete(p); }
73 };
74
75 std::unique_ptr<wasm_valtype_t, deleter> ptr;
76
77 static wasm_valkind_t kind_to_c(ValKind kind) {
78 switch (kind) {
79#define CASE_KIND_TO_C(kind, ignore, ckind) \
80 case ValKind::kind: \
81 return ckind;
82 WASMTIME_FOR_EACH_VAL_KIND(CASE_KIND_TO_C)
83#undef CASE_KIND_TO_C
84 default:
85 abort();
86 }
87 }
88
89public:
92 class Ref {
93 friend class ValType;
94
95 const wasm_valtype_t *ptr;
96
97 public:
99 Ref(const wasm_valtype_t *ptr) : ptr(ptr) {}
101 Ref(const ValType &ty) : Ref(ty.ptr.get()) {}
104 ValKind kind() const {
105 switch (wasm_valtype_kind(ptr)) {
106#define CASE_C_TO_KIND(kind, ignore, ckind) \
107 case ckind: \
108 return ValKind::kind;
109 WASMTIME_FOR_EACH_VAL_KIND(CASE_C_TO_KIND)
110#undef CASE_C_TO_KIND
111 }
112 std::abort();
113 }
114 };
118 class ListRef {
119 const wasm_valtype_vec_t *list;
121 public:
123 ListRef(const wasm_valtype_vec_t *list) : list(list) {}
124
126 typedef const Ref *iterator;
127
129 iterator begin() const {
130 return reinterpret_cast<Ref *>(&list->data[0]); // NOLINT
132
134 iterator end() const {
135 return reinterpret_cast<Ref *>(&list->data[list->size]); // NOLINT
137
139 size_t size() const { return list->size; }
140 };
141
142private:
143 Ref ref;
144 ValType(wasm_valtype_t *ptr) : ptr(ptr), ref(ptr) {}
146public:
148 ValType(ValKind kind) : ValType(wasm_valtype_new(kind_to_c(kind))) {}
150 ValType(Ref other) : ValType(wasm_valtype_copy(other.ptr)) {}
152 ValType(const ValType &other) : ValType(wasm_valtype_copy(other.ptr.get())) {}
154 ValType &operator=(const ValType &other) {
155 ptr.reset(wasm_valtype_copy(other.ptr.get()));
156 ref = other.ref;
157 return *this;
159 ~ValType() = default;
161 ValType(ValType &&other) = default;
163 ValType &operator=(ValType &&other) = default;
167 Ref *operator->() { return &ref; }
170 Ref *operator*() { return &ref; }
171};
172
173#undef WASMTIME_FOR_EACH_VAL_KIND
174
175}; // namespace wasmtime
176
177#endif // WASMTIME_TYPES_VAL_HH
Representation of a WebAssembly anyref value.
Definition: val.hh:105
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:115
ListRef(const wasm_valtype_vec_t *list)
Creates a list from the raw underlying C API.
Definition: types/val.hh:120
size_t size() const
Returns how many types are in this list.
Definition: types/val.hh:136
iterator begin() const
Pointer to the beginning of iteration.
Definition: types/val.hh:126
iterator end() const
Pointer to the end of iteration.
Definition: types/val.hh:131
const Ref * iterator
This list iterates over a list of ValType::Ref instances.
Definition: types/val.hh:123
Non-owning reference to a ValType, must not be used after the original ValType is deleted.
Definition: types/val.hh:90
Ref(const ValType &ty)
Copy constructor.
Definition: types/val.hh:99
Ref(const wasm_valtype_t *ptr)
Instantiates from the raw C API representation.
Definition: types/val.hh:97
ValKind kind() const
Returns the corresponding "kind" for this type.
Definition: types/val.hh:102
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:151
Ref * operator->()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/val.hh:164
Ref * operator*()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/val.hh:167
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:179
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.