* \brief A WebAssembly table. * * This class represents a WebAssembly table, either created through * instantiating a module or a host table. Tables are contiguous vectors of * WebAssembly reference types, currently either `externref` or `funcref`. * * Note that this type does not itself own any resources. It points to resources * owned within a `Store` and the `Store` must be passed in as
| 29 | * is passed in then the process will be aborted. |
| 30 | */ |
| 31 | class Table { |
| 32 | friend class Instance; |
| 33 | wasmtime_table_t table; |
| 34 | |
| 35 | public: |
| 36 | /// Creates a new table from the raw underlying C API representation. |
| 37 | Table(wasmtime_table_t table) : table(table) {} |
| 38 | |
| 39 | /** |
| 40 | * \brief Creates a new host-defined table. |
| 41 | * |
| 42 | * \param cx the store in which to create the table. |
| 43 | * \param ty the type of the table to be created |
| 44 | * \param init the initial value for all table slots. |
| 45 | * |
| 46 | * Returns an error if `init` has the wrong value for the `ty` specified. |
| 47 | */ |
| 48 | static Result<Table> create(Store::Context cx, const TableType &ty, |
| 49 | const Val &init) { |
| 50 | wasmtime_table_t table; |
| 51 | auto *error = wasmtime_table_new(cx.ptr, ty.ptr.get(), &init.val, &table); |
| 52 | if (error != nullptr) { |
| 53 | return Error(error); |
| 54 | } |
| 55 | return Table(table); |
| 56 | } |
| 57 | |
| 58 | /// Returns the type of this table. |
| 59 | TableType type(Store::Context cx) const { |
| 60 | return wasmtime_table_type(cx.ptr, &table); |
| 61 | } |
| 62 | |
| 63 | /// Returns the size, in elements, that the table currently has. |
| 64 | uint64_t size(Store::Context cx) const { |
| 65 | return wasmtime_table_size(cx.ptr, &table); |
| 66 | } |
| 67 | |
| 68 | /// Loads a value from the specified index in this table. |
| 69 | /// |
| 70 | /// Returns `std::nullopt` if `idx` is out of bounds. |
| 71 | std::optional<Val> get(Store::Context cx, uint64_t idx) const { |
| 72 | Val val; |
| 73 | if (wasmtime_table_get(cx.ptr, &table, idx, &val.val)) { |
| 74 | return std::optional(std::move(val)); |
| 75 | } |
| 76 | return std::nullopt; |
| 77 | } |
| 78 | |
| 79 | /// Stores a value into the specified index in this table. |
| 80 | /// |
| 81 | /// Returns an error if `idx` is out of bounds or if `val` has the wrong type. |
| 82 | Result<std::monostate> set(Store::Context cx, uint64_t idx, |
| 83 | const Val &val) const { |
| 84 | auto *error = wasmtime_table_set(cx.ptr, &table, idx, &val.val); |
| 85 | if (error != nullptr) { |
| 86 | return Error(error); |
| 87 | } |
| 88 | return std::monostate(); |
no outgoing calls
no test coverage detected