1 /**
2  * \file wasmtime/table.h
3  *
4  * Wasmtime APIs for interacting with WebAssembly tables.
5  */
6 
7 #ifndef WASMTIME_TABLE_H
8 #define WASMTIME_TABLE_H
9 
10 #include <wasm.h>
11 #include <wasmtime/extern.h>
12 #include <wasmtime/store.h>
13 #include <wasmtime/error.h>
14 #include <wasmtime/val.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /**
21  * \brief Creates a new host-defined wasm table.
22  *
23  * \param store the store to create the table within
24  * \param ty the type of the table to create
25  * \param init the initial value for this table's elements
26  * \param table where to store the returned table
27  *
28  * This function does not take ownership of any of its parameters, but yields
29  * ownership of returned error. This function may return an error if the `init`
30  * value does not match `ty`, for example.
31  */
32 WASM_API_EXTERN wasmtime_error_t *wasmtime_table_new(
33     wasmtime_context_t *store,
34     const wasm_tabletype_t *ty,
35     const wasmtime_val_t *init,
36     wasmtime_table_t *table
37 );
38 
39 /**
40  * \brief Returns the type of this table.
41  *
42  * The caller has ownership of the returned #wasm_tabletype_t
43  */
44 WASM_API_EXTERN wasm_tabletype_t* wasmtime_table_type(
45     const wasmtime_context_t *store,
46     const wasmtime_table_t *table
47 );
48 
49 /**
50  * \brief Gets a value in a table.
51  *
52  * \param store the store that owns `table`
53  * \param table the table to access
54  * \param index the table index to access
55  * \param val where to store the table's value
56  *
57  * This function will attempt to access a table element. If a nonzero value is
58  * returned then `val` is filled in and is owned by the caller. Otherwise zero
59  * is returned because the `index` is out-of-bounds.
60  */
61 WASM_API_EXTERN bool wasmtime_table_get(
62     wasmtime_context_t *store,
63     const wasmtime_table_t *table,
64     uint32_t index,
65     wasmtime_val_t *val
66 );
67 
68 /**
69  * \brief Sets a value in a table.
70  *
71  * \param store the store that owns `table`
72  * \param table the table to write to
73  * \param index the table index to write
74  * \param value the value to store.
75  *
76  * This function will store `value` into the specified index in the table. This
77  * does not take ownership of any argument but yields ownership of the error.
78  * This function can fail if `value` has the wrong type for the table, or if
79  * `index` is out of bounds.
80  */
81 WASM_API_EXTERN wasmtime_error_t *wasmtime_table_set(
82     wasmtime_context_t *store,
83     const wasmtime_table_t *table,
84     uint32_t index,
85     const wasmtime_val_t *value
86 );
87 
88 /**
89  * \brief Returns the size, in elements, of the specified table
90  */
91 WASM_API_EXTERN uint32_t wasmtime_table_size(
92     const wasmtime_context_t *store,
93     const wasmtime_table_t *table
94 );
95 
96 /**
97  * \brief Grows a table.
98  *
99  * \param store the store that owns `table`
100  * \param table the table to grow
101  * \param delta the number of elements to grow the table by
102  * \param init the initial value for new table element slots
103  * \param prev_size where to store the previous size of the table before growth
104  *
105  * This function will attempt to grow the table by `delta` table elements. This
106  * can fail if `delta` would exceed the maximum size of the table or if `init`
107  * is the wrong type for this table. If growth is successful then `NULL` is
108  * returned and `prev_size` is filled in with the previous size of the table, in
109  * elements, before the growth happened.
110  *
111  * This function does not take ownership of any of its arguments.
112  */
113 WASM_API_EXTERN wasmtime_error_t *wasmtime_table_grow(
114     wasmtime_context_t *store,
115     const wasmtime_table_t *table,
116     uint32_t delta,
117     const wasmtime_val_t *init,
118     uint32_t *prev_size
119 );
120 
121 #ifdef __cplusplus
122 }  // extern "C"
123 #endif
124 
125 #endif // WASMTIME_TABLE_H
126 
127