Wasmtime
table.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_TYPES_TABLE_HH
6#define WASMTIME_TYPES_TABLE_HH
7
8#include <optional>
10
11namespace wasmtime {
12
16class TableType {
17 friend class Table;
18
19 struct deleter {
20 void operator()(wasm_tabletype_t *p) const { wasm_tabletype_delete(p); }
21 };
22
23 std::unique_ptr<wasm_tabletype_t, deleter> ptr;
24
25public:
28 class Ref {
29 friend class TableType;
30
31 const wasm_tabletype_t *ptr;
32
33 public:
35 Ref(const wasm_tabletype_t *ptr) : ptr(ptr) {}
37 Ref(const TableType &ty) : Ref(ty.ptr.get()) {}
38
40 uint32_t min() const { return wasm_tabletype_limits(ptr)->min; }
41
43 std::optional<uint32_t> max() const {
44 const auto *limits = wasm_tabletype_limits(ptr);
45 if (limits->max == wasm_limits_max_default) {
46 return std::nullopt;
47 }
48 return limits->max;
49 }
50
53 };
54
55private:
56 Ref ref;
57 TableType(wasm_tabletype_t *ptr) : ptr(ptr), ref(ptr) {}
58
59public:
62 TableType(ValType ty, uint32_t min) : ptr(nullptr), ref(nullptr) {
63 wasm_limits_t limits;
64 limits.min = min;
65 limits.max = wasm_limits_max_default;
66 ptr.reset(wasm_tabletype_new(ty.ptr.release(), &limits));
67 ref = ptr.get();
68 }
71 TableType(ValType ty, uint32_t min, uint32_t max) // NOLINT
72 : ptr(nullptr), ref(nullptr) {
73 wasm_limits_t limits;
74 limits.min = min;
75 limits.max = max;
76 ptr.reset(wasm_tabletype_new(ty.ptr.release(), &limits));
77 ref = ptr.get();
78 }
82 TableType(const TableType &other)
83 : TableType(wasm_tabletype_copy(other.ptr.get())) {}
85 TableType &operator=(const TableType &other) {
86 ptr.reset(wasm_tabletype_copy(other.ptr.get()));
87 return *this;
88 }
89 ~TableType() = default;
91 TableType(TableType &&other) = default;
93 TableType &operator=(TableType &&other) = default;
94
97 Ref *operator->() { return &ref; }
100 Ref *operator*() { return &ref; }
101};
102
103}; // namespace wasmtime
104
105#endif // WASMTIME_TYPES_TABLE_HH
Definition: table.hh:28
Ref(const wasm_tabletype_t *ptr)
Creates a reference from the raw underlying C API representation.
Definition: table.hh:35
Ref(const TableType &ty)
Creates a reference to the provided TableType.
Definition: table.hh:37
std::optional< uint32_t > max() const
Returns the maximum size of this table type, in elements, if present.
Definition: table.hh:43
ValType::Ref element() const
Returns the type of value that is stored in this table.
Definition: table.hh:52
uint32_t min() const
Returns the minimum size of this table type, in elements.
Definition: table.hh:40
Type information about a WebAssembly table.
Definition: table.hh:16
TableType(const TableType &other)
Copies another table type into this one.
Definition: table.hh:82
TableType & operator=(TableType &&other)=default
Moves the table type resources from another type to this one.
TableType(ValType ty, uint32_t min, uint32_t max)
Definition: table.hh:71
TableType(Ref other)
Clones the given reference into a new table type.
Definition: table.hh:80
Ref * operator->()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: table.hh:97
TableType(ValType ty, uint32_t min)
Definition: table.hh:62
TableType & operator=(const TableType &other)
Copies another table type into this one.
Definition: table.hh:85
TableType(TableType &&other)=default
Moves the table type resources from another type to this one.
Ref * operator*()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: table.hh:100
A WebAssembly table.
Definition: wasmtime.hh:766
Non-owning reference to a ValType, must not be used after the original ValType is deleted.
Definition: types/val.hh:91
Type information about a WebAssembly value.
Definition: types/val.hh:66
Limits for tables/memories in wasm modules.
Definition: wasm.h:164
uint32_t min
Definition: wasm.h:165
uint32_t max
Definition: wasm.h:166
An opaque object representing the type of a table.
const wasm_valtype_t * wasm_tabletype_element(const wasm_tabletype_t *)
Returns the element type of this table.
wasm_tabletype_t * wasm_tabletype_new(wasm_valtype_t *, const wasm_limits_t *)
Creates a new table type.
wasm_tabletype_t * wasm_tabletype_copy(const wasm_tabletype_t *)
Creates a new value which matches the provided one.
const wasm_limits_t * wasm_tabletype_limits(const wasm_tabletype_t *)
Returns the limits of this table.
void wasm_tabletype_delete(wasm_tabletype_t *)
Deletes a type.