Wasmtime
_store_class.hh
1#ifndef WASMTIME_STORE_CLASS_HH
2#define WASMTIME_STORE_CLASS_HH
3
4#include <any>
5#include <memory>
6#include <optional>
7#include <wasmtime/conf.h>
8#include <wasmtime/engine.hh>
9#include <wasmtime/error.hh>
10#include <wasmtime/helpers.hh>
11#include <wasmtime/store.h>
12#include <wasmtime/wasi.hh>
13
14namespace wasmtime {
15
16class Caller;
17class Tag;
18class ExnRef;
19class Trap;
20
22enum class DeadlineKind {
27};
28
41class Store {
42 WASMTIME_OWN_WRAPPER(Store, wasmtime_store);
43
44private:
45 static void finalizer(void *ptr) {
46 std::unique_ptr<std::any> _ptr(static_cast<std::any *>(ptr));
47 }
48
49public:
51 explicit Store(Engine &engine)
52 : ptr(wasmtime_store_new(engine.capi(), nullptr, finalizer)) {}
53
65 class Context {
66 friend class Global;
67 friend class Table;
68 friend class Memory;
69 friend class Func;
70 friend class Instance;
71 friend class Linker;
72 friend class Val;
73 friend class Store;
74 friend class Tag;
76
77 public:
79 explicit Context(wasmtime_context_t *ptr) : ptr(ptr) {}
80
82 Context(Store &store) : Context(wasmtime_store_context(store.ptr.get())) {}
84 Context(Store *store) : Context(*store) {}
86 Context(Caller &caller);
88 Context(Caller *caller);
89
90#ifdef WASMTIME_FEATURE_GC
94 auto *error = wasmtime_context_gc(ptr);
95 if (error != nullptr) {
96 return Error(error);
97 }
98 return std::monostate();
99 }
100#endif
101
109 auto *error = wasmtime_context_set_fuel(ptr, fuel);
110 if (error != nullptr) {
111 return Error(error);
112 }
113 return std::monostate();
114 }
115
120 uint64_t fuel = 0;
121 auto *error = wasmtime_context_get_fuel(ptr, &fuel);
122 if (error != nullptr) {
123 return Error(error);
124 }
125 return fuel;
126 }
127
129 void set_data(std::any data) const {
130 finalizer(static_cast<std::any *>(wasmtime_context_get_data(ptr)));
132 ptr, std::make_unique<std::any>(std::move(data)).release());
133 }
134
136 std::any &get_data() const {
137 return *static_cast<std::any *>(wasmtime_context_get_data(ptr));
138 }
139
140#ifdef WASMTIME_FEATURE_WASI
147 auto *error = wasmtime_context_set_wasi(ptr, config.capi_release());
148 if (error != nullptr) {
149 return Error(error);
150 }
151 return std::monostate();
152 }
153#endif // WASMTIME_FEATURE_WASI
154
161 void set_epoch_deadline(uint64_t ticks_beyond_current) {
162 wasmtime_context_set_epoch_deadline(ptr, ticks_beyond_current);
163 }
164
165#ifdef WASMTIME_FEATURE_GC
172
176 inline std::optional<ExnRef> take_exception();
177
179 inline bool has_exception();
180#endif // WASMTIME_FEATURE_GC
181
183 const wasmtime_context_t *capi() const { return ptr; }
184
186 wasmtime_context_t *capi() { return ptr; }
187 };
188
219 void limiter(int64_t memory_size, int64_t table_elements, int64_t instances,
220 int64_t tables, int64_t memories) {
221 wasmtime_store_limiter(ptr.get(), memory_size, table_elements, instances,
222 tables, memories);
223 }
224
236 template <typename F,
237 std::enable_if_t<std::is_invocable_r_v<Result<DeadlineKind>, F,
238 Context, uint64_t &>,
239 bool> = true>
242 ptr.get(), raw_epoch_callback<std::remove_reference_t<F>>,
243 std::make_unique<std::remove_reference_t<F>>(std::forward<F>(f))
244 .release(),
245 raw_epoch_finalizer<std::remove_reference_t<F>>);
246 }
247
249 Context context() { return this; }
250
251#ifdef WASMTIME_FEATURE_GC
255#endif
256
257private:
258 template <typename F>
259 static wasmtime_error_t *
260 raw_epoch_callback(wasmtime_context_t *context, void *data,
261 uint64_t *epoch_deadline_delta,
262 wasmtime_update_deadline_kind_t *update_kind) {
263 auto &callback = *static_cast<F *>(data);
264 Context ctx(context);
265 auto result = callback(ctx, *epoch_deadline_delta);
266
267 if (!result) {
268 return result.err().capi_release();
269 }
270 *update_kind = static_cast<wasmtime_update_deadline_kind_t>(result.ok());
271 return nullptr;
272 }
273
274 template <typename F> static void raw_epoch_finalizer(void *data) {
275 std::unique_ptr<F> _ptr(static_cast<F *>(data));
276 }
277};
278
279} // namespace wasmtime
280
281#endif // WASMTIME_STORE_CLASS_HH
Structure provided to host functions to lookup caller information or acquire a Store::Context.
Definition: _func_class.hh:25
Global compilation state in Wasmtime.
Definition: engine.hh:22
Errors coming from Wasmtime.
Definition: error.hh:26
A WebAssembly exception object.
Definition: _exnref_class.hh:29
Representation of a WebAssembly function.
Definition: _func_class.hh:108
A WebAssembly global.
Definition: global.hh:28
A WebAssembly instance.
Definition: instance.hh:32
Helper class for linking modules together with name-based resolution.
Definition: linker.hh:26
A WebAssembly linear memory.
Definition: memory.hh:27
Fallible result type used for Wasmtime.
Definition: error.hh:70
An interior pointer into a Store.
Definition: _store_class.hh:65
Result< std::monostate > gc()
Definition: _store_class.hh:93
std::optional< ExnRef > take_exception()
Takes the pending exception from the store, if any.
void set_epoch_deadline(uint64_t ticks_beyond_current)
Definition: _store_class.hh:161
Result< std::monostate > set_wasi(WasiConfig config)
Definition: _store_class.hh:146
Trap throw_exception(ExnRef exn)
Sets the pending exception on the store and returns a Trap.
Context(wasmtime_context_t *ptr)
Creates a context from the raw C API pointer.
Definition: _store_class.hh:79
Result< std::monostate > set_fuel(uint64_t fuel)
Definition: _store_class.hh:108
Context(Store &store)
Creates a context referencing the provided Store.
Definition: _store_class.hh:82
bool has_exception()
Tests whether there is a pending exception on the store.
void set_data(std::any data) const
Set user specified data associated with this store.
Definition: _store_class.hh:129
Result< uint64_t > get_fuel() const
Definition: _store_class.hh:119
std::any & get_data() const
Get user specified data associated with this store.
Definition: _store_class.hh:136
Context(Store *store)
Creates a context referencing the provided Store.
Definition: _store_class.hh:84
const wasmtime_context_t * capi() const
Returns the underlying C API pointer.
Definition: _store_class.hh:183
wasmtime_context_t * capi()
Returns the underlying C API pointer.
Definition: _store_class.hh:186
Owner of all WebAssembly objects.
Definition: _store_class.hh:41
void epoch_deadline_callback(F &&f)
Configures epoch deadline callback to C function.
Definition: _store_class.hh:240
Store(Engine &engine)
Creates a new Store within the provided Engine.
Definition: _store_class.hh:51
Context context()
Explicit function to acquire a Context from this store.
Definition: _store_class.hh:249
void limiter(int64_t memory_size, int64_t table_elements, int64_t instances, int64_t tables, int64_t memories)
Provides limits for a store. Used by hosts to limit resource consumption of instances....
Definition: _store_class.hh:219
Result< std::monostate > gc()
Definition: _store_class.hh:254
A WebAssembly table.
Definition: table.hh:31
A WebAssembly tag.
Definition: tag.hh:26
Information about a WebAssembly trap.
Definition: trap.hh:113
Representation of a generic WebAssembly value.
Definition: _val_class.hh:54
Configuration for an instance of WASI.
Definition: wasi.hh:24
Build-time defines for how the C API was built.
WASM_API_EXTERN void wasmtime_store_limiter(wasmtime_store_t *store, int64_t memory_size, int64_t table_elements, int64_t instances, int64_t tables, int64_t memories)
Provides limits for a store. Used by hosts to limit resource consumption of instances....
WASM_API_EXTERN void wasmtime_context_set_epoch_deadline(wasmtime_context_t *context, uint64_t ticks_beyond_current)
Configures the relative deadline at which point WebAssembly code will trap or invoke the callback fun...
uint8_t wasmtime_update_deadline_kind_t
An enum for the behavior before extending the epoch deadline.
Definition: store.h:228
#define WASMTIME_UPDATE_DEADLINE_CONTINUE
Directly continue to updating the deadline and executing WebAssembly.
Definition: store.h:230
WASM_API_EXTERN void wasmtime_store_epoch_deadline_callback(wasmtime_store_t *store, wasmtime_error_t *(*func)(wasmtime_context_t *context, void *data, uint64_t *epoch_deadline_delta, wasmtime_update_deadline_kind_t *update_kind), void *data, void(*finalizer)(void *))
Configures epoch deadline callback to C function.
WASM_API_EXTERN wasmtime_error_t * wasmtime_context_gc(wasmtime_context_t *context)
Perform garbage collection within the given context.
WASM_API_EXTERN wasmtime_error_t * wasmtime_context_set_wasi(wasmtime_context_t *context, wasi_config_t *wasi)
Configures WASI state within the specified store.
WASM_API_EXTERN wasmtime_error_t * wasmtime_context_get_fuel(const wasmtime_context_t *context, uint64_t *fuel)
Returns the amount of fuel remaining in this context's store.
WASM_API_EXTERN void wasmtime_context_set_data(wasmtime_context_t *context, void *data)
Overwrites the user-specified data associated with this store.
WASM_API_EXTERN wasmtime_error_t * wasmtime_context_set_fuel(wasmtime_context_t *store, uint64_t fuel)
Set fuel to this context's store for wasm to consume while executing.
WASM_API_EXTERN wasmtime_context_t * wasmtime_store_context(wasmtime_store_t *store)
Returns the interior wasmtime_context_t pointer to this store.
#define WASMTIME_UPDATE_DEADLINE_YIELD
Yield control (via async support) then update the deadline.
Definition: store.h:232
WASM_API_EXTERN void * wasmtime_context_get_data(const wasmtime_context_t *context)
Returns the user-specified data associated with the specified store.
WASM_API_EXTERN wasmtime_store_t * wasmtime_store_new(wasm_engine_t *engine, void *data, void(*finalizer)(void *))
Creates a new store within the specified engine.
An interior pointer into a wasmtime_store_t which is used as "context" for many functions.
Errors generated by Wasmtime.
Storage of WebAssembly objects.