1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* \file wasmtime/types/table.hh
*/
#ifndef WASMTIME_TYPES_TABLE_HH
#define WASMTIME_TYPES_TABLE_HH
#include <optional>
#include <wasmtime/types/val.hh>
namespace wasmtime {
/**
* \brief Type information about a WebAssembly table.
*/
class TableType {
friend class Table;
struct deleter {
void operator()(wasm_tabletype_t *p) const { wasm_tabletype_delete(p); }
};
std::unique_ptr<wasm_tabletype_t, deleter> ptr;
public:
/// Non-owning reference to a `TableType`, must not be used after the original
/// owner is deleted.
class Ref {
friend class TableType;
const wasm_tabletype_t *ptr;
public:
/// Creates a reference from the raw underlying C API representation.
Ref(const wasm_tabletype_t *ptr) : ptr(ptr) {}
/// Creates a reference to the provided `TableType`.
Ref(const TableType &ty) : Ref(ty.ptr.get()) {}
/// Returns the minimum size of this table type, in elements.
uint32_t min() const { return wasm_tabletype_limits(ptr)->min; }
/// Returns the maximum size of this table type, in elements, if present.
std::optional<uint32_t> max() const {
const auto *limits = wasm_tabletype_limits(ptr);
if (limits->max == wasm_limits_max_default) {
return std::nullopt;
}
return limits->max;
}
/// Returns the type of value that is stored in this table.
ValType::Ref element() const { return wasm_tabletype_element(ptr); }
};
private:
Ref ref;
TableType(wasm_tabletype_t *ptr) : ptr(ptr), ref(ptr) {}
public:
/// Creates a new table type from the specified value type and minimum size.
/// The returned table will have no maximum size.
TableType(ValType ty, uint32_t min) : ptr(nullptr), ref(nullptr) {
wasm_limits_t limits;
limits.min = min;
limits.max = wasm_limits_max_default;
ptr.reset(wasm_tabletype_new(ty.ptr.release(), &limits));
ref = ptr.get();
}
/// Creates a new table type from the specified value type, minimum size, and
/// maximum size.
TableType(ValType ty, uint32_t min, uint32_t max) // NOLINT
: ptr(nullptr), ref(nullptr) {
wasm_limits_t limits;
limits.min = min;
limits.max = max;
ptr.reset(wasm_tabletype_new(ty.ptr.release(), &limits));
ref = ptr.get();
}
/// Clones the given reference into a new table type.
TableType(Ref other) : TableType(wasm_tabletype_copy(other.ptr)) {}
/// Copies another table type into this one.
TableType(const TableType &other)
: TableType(wasm_tabletype_copy(other.ptr.get())) {}
/// Copies another table type into this one.
TableType &operator=(const TableType &other) {
ptr.reset(wasm_tabletype_copy(other.ptr.get()));
return *this;
}
~TableType() = default;
/// Moves the table type resources from another type to this one.
TableType(TableType &&other) = default;
/// Moves the table type resources from another type to this one.
TableType &operator=(TableType &&other) = default;
/// \brief Returns the underlying `Ref`, a non-owning reference pointing to
/// this instance.
Ref *operator->() { return &ref; }
/// \brief Returns the underlying `Ref`, a non-owning reference pointing to
/// this instance.
Ref *operator*() { return &ref; }
};
}; // namespace wasmtime
#endif // WASMTIME_TYPES_TABLE_HH