Wasmtime
types/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>
9#include <wasmtime/error.hh>
10#include <wasmtime/table.h>
11#include <wasmtime/types/val.hh>
12
13namespace wasmtime {
14
18class TableType {
19 friend class Table;
20
21 struct deleter {
22 void operator()(wasm_tabletype_t *p) const { wasm_tabletype_delete(p); }
23 };
24
25 std::unique_ptr<wasm_tabletype_t, deleter> ptr;
26
27public:
30 class Ref {
31 friend class TableType;
32
33 const wasm_tabletype_t *ptr;
34
35 public:
37 Ref(const wasm_tabletype_t *ptr) : ptr(ptr) {}
39 Ref(const TableType &ty) : Ref(ty.ptr.get()) {}
40
42 uint64_t min() const { return wasmtime_tabletype_minimum(ptr); }
43
45 std::optional<uint64_t> max() const {
46 uint64_t max = 0;
48 return max;
49 }
50 return std::nullopt;
51 }
52
54 bool is_64() const { return wasmtime_tabletype_is64(ptr); }
55
58 };
59
60private:
61 Ref ref;
62 TableType(wasm_tabletype_t *ptr) : ptr(ptr), ref(ptr) {}
63
64public:
70 static Result<TableType> create(ValType ty, uint32_t min,
71 std::optional<uint32_t> max = std::nullopt) {
72 wasm_limits_t limits;
73 limits.min = min;
74 limits.max = max.value_or(wasm_limits_max_default);
75 wasm_tabletype_t *ptr = wasm_tabletype_new(ty.ptr.release(), &limits);
76 if (ptr == nullptr) {
77 return Error("invalid table type");
78 }
79 return TableType(ptr);
80 }
84 TableType(const TableType &other)
85 : TableType(wasm_tabletype_copy(other.ptr.get())) {}
87 TableType &operator=(const TableType &other) {
88 ptr.reset(wasm_tabletype_copy(other.ptr.get()));
89 ref = ptr.get();
90 return *this;
91 }
92 ~TableType() = default;
94 TableType(TableType &&other) = default;
96 TableType &operator=(TableType &&other) = default;
97
100 Ref *operator->() { return &ref; }
103 Ref *operator*() { return &ref; }
104};
105
106}; // namespace wasmtime
107
108#endif // WASMTIME_TYPES_TABLE_HH
Errors coming from Wasmtime.
Definition: error.hh:26
Fallible result type used for Wasmtime.
Definition: error.hh:70
Definition: types/table.hh:30
Ref(const wasm_tabletype_t *ptr)
Creates a reference from the raw underlying C API representation.
Definition: types/table.hh:37
Ref(const TableType &ty)
Creates a reference to the provided TableType.
Definition: types/table.hh:39
std::optional< uint64_t > max() const
Returns the maximum size of this table type, in elements, if present.
Definition: types/table.hh:45
ValType::Ref element() const
Returns the type of value that is stored in this table.
Definition: types/table.hh:57
uint64_t min() const
Returns the minimum size of this table type, in elements.
Definition: types/table.hh:42
bool is_64() const
Returns whether or not this is a 64-bit table type.
Definition: types/table.hh:54
Type information about a WebAssembly table.
Definition: types/table.hh:18
TableType(const TableType &other)
Copies another table type into this one.
Definition: types/table.hh:84
TableType & operator=(TableType &&other)=default
Moves the table type resources from another type to this one.
TableType(Ref other)
Clones the given reference into a new table type.
Definition: types/table.hh:82
Ref * operator->()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/table.hh:100
TableType & operator=(const TableType &other)
Copies another table type into this one.
Definition: types/table.hh:87
static Result< TableType > create(ValType ty, uint32_t min, std::optional< uint32_t > max=std::nullopt)
Definition: types/table.hh:70
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: types/table.hh:103
A WebAssembly table.
Definition: table.hh:31
Non-owning reference to a ValType, must not be used after the original ValType is deleted.
Definition: types/_val_class.hh:32
Type information about a WebAssembly value.
Definition: types/_val_class.hh:19
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.
WASM_API_EXTERN uint64_t wasmtime_tabletype_minimum(const wasm_tabletype_t *ty)
Returns the minimum size, in elements, of the specified table type.
WASM_API_EXTERN bool wasmtime_tabletype_maximum(const wasm_tabletype_t *ty, uint64_t *max)
Returns the maximum size, in elements, of the specified table type.
WASM_API_EXTERN bool wasmtime_tabletype_is64(const wasm_tabletype_t *ty)
Returns whether this type of table represents a 64-bit 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.
void wasm_tabletype_delete(wasm_tabletype_t *)
Deletes a type.