Wasmtime
table.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_TABLE_HH
6#define WASMTIME_TABLE_HH
7
8#include <optional>
9
10#include <wasmtime/error.hh>
11#include <wasmtime/store.hh>
12#include <wasmtime/table.h>
14#include <wasmtime/types/val.hh>
15#include <wasmtime/val.hh>
16
17namespace wasmtime {
18
31class Table {
32 friend class Instance;
33 wasmtime_table_t table;
34
35public:
37 Table(wasmtime_table_t table) : table(table) {}
38
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
60 return wasmtime_table_type(cx.ptr, &table);
61 }
62
64 uint64_t size(Store::Context cx) const {
65 return wasmtime_table_size(cx.ptr, &table);
66 }
67
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
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();
89 }
90
100 const Val &init) const {
101 uint64_t prev = 0;
102 auto *error = wasmtime_table_grow(cx.ptr, &table, delta, &init.val, &prev);
103 if (error != nullptr) {
104 return Error(error);
105 }
106 return prev;
107 }
108
110 const wasmtime_table_t &capi() const { return table; }
111};
112
113} // namespace wasmtime
114
115#endif // WASMTIME_TABLE_HH
Errors coming from Wasmtime.
Definition: error.hh:25
A WebAssembly instance.
Definition: instance.hh:31
Fallible result type used for Wasmtime.
Definition: error.hh:82
An interior pointer into a Store.
Definition: store.hh:60
Type information about a WebAssembly table.
Definition: types/table.hh:16
A WebAssembly table.
Definition: table.hh:31
Table(wasmtime_table_t table)
Creates a new table from the raw underlying C API representation.
Definition: table.hh:37
Result< std::monostate > set(Store::Context cx, uint64_t idx, const Val &val) const
Definition: table.hh:82
const wasmtime_table_t & capi() const
Returns the raw underlying C API table this is using.
Definition: table.hh:110
static Result< Table > create(Store::Context cx, const TableType &ty, const Val &init)
Creates a new host-defined table.
Definition: table.hh:48
Result< uint64_t > grow(Store::Context cx, uint64_t delta, const Val &init) const
Definition: table.hh:99
uint64_t size(Store::Context cx) const
Returns the size, in elements, that the table currently has.
Definition: table.hh:64
TableType type(Store::Context cx) const
Returns the type of this table.
Definition: table.hh:59
std::optional< Val > get(Store::Context cx, uint64_t idx) const
Definition: table.hh:71
Representation of a generic WebAssembly value.
Definition: val.hh:156
Representation of a table in Wasmtime.
Definition: extern.h:43
wasm_tabletype_t * wasmtime_table_type(const wasmtime_context_t *store, const wasmtime_table_t *table)
Returns the type of this table.
wasmtime_error_t * wasmtime_table_set(wasmtime_context_t *store, const wasmtime_table_t *table, uint64_t index, const wasmtime_val_t *value)
Sets a value in a table.
wasmtime_error_t * wasmtime_table_new(wasmtime_context_t *store, const wasm_tabletype_t *ty, const wasmtime_val_t *init, wasmtime_table_t *table)
Creates a new host-defined wasm table.
bool wasmtime_table_get(wasmtime_context_t *store, const wasmtime_table_t *table, uint64_t index, wasmtime_val_t *val)
Gets a value in a table.
wasmtime_error_t * wasmtime_table_grow(wasmtime_context_t *store, const wasmtime_table_t *table, uint64_t delta, const wasmtime_val_t *init, uint64_t *prev_size)
Grows a table.
uint64_t wasmtime_table_size(const wasmtime_context_t *store, const wasmtime_table_t *table)
Returns the size, in elements, of the specified table.