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