Wasmtime
store.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_STORE_HH
6#define WASMTIME_STORE_HH
7
8#include <any>
9#include <memory>
10#include <optional>
11#include <wasmtime/conf.h>
12#include <wasmtime/engine.hh>
13#include <wasmtime/error.hh>
14#include <wasmtime/store.h>
15#include <wasmtime/wasi.hh>
16
17namespace wasmtime {
18
19class Caller;
20
22enum class DeadlineKind {
27};
28
41class Store {
42 struct deleter {
43 void operator()(wasmtime_store_t *p) const { wasmtime_store_delete(p); }
44 };
45
46 std::unique_ptr<wasmtime_store_t, deleter> ptr;
47
48 static void finalizer(void *ptr) {
49 std::unique_ptr<std::any> _ptr(static_cast<std::any *>(ptr));
50 }
51
52public:
54 explicit Store(Engine &engine)
55 : ptr(wasmtime_store_new(engine.ptr.get(), nullptr, finalizer)) {}
56
68 class Context {
69 friend class Global;
70 friend class Table;
71 friend class Memory;
72 friend class Func;
73 friend class Instance;
74 friend class Linker;
75 friend class ExternRef;
76 friend class AnyRef;
77 friend class Val;
78 friend class Store;
80
81 public:
83 explicit Context(wasmtime_context_t *ptr) : ptr(ptr) {}
84
86 Context(Store &store) : Context(wasmtime_store_context(store.ptr.get())) {}
88 Context(Store *store) : Context(*store) {}
90 Context(Caller &caller);
92 Context(Caller *caller);
93
96 void gc() { wasmtime_context_gc(ptr); }
97
105 auto *error = wasmtime_context_set_fuel(ptr, fuel);
106 if (error != nullptr) {
107 return Error(error);
108 }
109 return std::monostate();
110 }
111
116 uint64_t fuel = 0;
117 auto *error = wasmtime_context_get_fuel(ptr, &fuel);
118 if (error != nullptr) {
119 return Error(error);
120 }
121 return fuel;
122 }
123
125 void set_data(std::any data) const {
126 finalizer(static_cast<std::any *>(wasmtime_context_get_data(ptr)));
128 ptr, std::make_unique<std::any>(std::move(data)).release());
129 }
130
132 std::any &get_data() const {
133 return *static_cast<std::any *>(wasmtime_context_get_data(ptr));
134 }
135
136#ifdef WASMTIME_FEATURE_WASI
143 auto *error = wasmtime_context_set_wasi(ptr, config.ptr.release());
144 if (error != nullptr) {
145 return Error(error);
146 }
147 return std::monostate();
148 }
149#endif // WASMTIME_FEATURE_WASI
150
157 void set_epoch_deadline(uint64_t ticks_beyond_current) {
158 wasmtime_context_set_epoch_deadline(ptr, ticks_beyond_current);
159 }
160
162 const wasmtime_context_t *capi() const { return ptr; }
163
165 wasmtime_context_t *capi() { return ptr; }
166 };
167
198 void limiter(int64_t memory_size, int64_t table_elements, int64_t instances,
199 int64_t tables, int64_t memories) {
200 wasmtime_store_limiter(ptr.get(), memory_size, table_elements, instances,
201 tables, memories);
202 }
203
215 template <typename F,
216 std::enable_if_t<std::is_invocable_r_v<Result<DeadlineKind>, F,
217 Context, uint64_t &>,
218 bool> = true>
221 ptr.get(), raw_epoch_callback<std::remove_reference_t<F>>,
222 std::make_unique<std::remove_reference_t<F>>(std::forward<F>(f))
223 .release(),
224 raw_epoch_finalizer<std::remove_reference_t<F>>);
225 }
226
228 Context context() { return this; }
229
232 void gc() { context().gc(); }
233
235 const wasmtime_store_t *capi() const { return ptr.get(); }
236
238 wasmtime_store_t *capi() { return ptr.get(); }
239
240private:
241 template <typename F>
242 static wasmtime_error_t *
243 raw_epoch_callback(wasmtime_context_t *context, void *data,
244 uint64_t *epoch_deadline_delta,
245 wasmtime_update_deadline_kind_t *update_kind) {
246 auto &callback = *static_cast<F *>(data);
247 Context ctx(context);
248 auto result = callback(ctx, *epoch_deadline_delta);
249
250 if (!result) {
251 return result.err().release();
252 }
253 *update_kind = static_cast<wasmtime_update_deadline_kind_t>(result.ok());
254 return nullptr;
255 }
256
257 template <typename F> static void raw_epoch_finalizer(void *data) {
258 std::unique_ptr<F> _ptr(static_cast<F *>(data));
259 }
260};
261
262} // namespace wasmtime
263
264#endif // WASMTIME_STORE_HH
Representation of a WebAssembly anyref value.
Definition: val.hh:105
Structure provided to host functions to lookup caller information or acquire a Store::Context.
Definition: func.hh:28
Global compilation state in Wasmtime.
Definition: engine.hh:21
Errors coming from Wasmtime.
Definition: error.hh:25
Representation of a WebAssembly externref value.
Definition: val.hh:28
Representation of a WebAssembly function.
Definition: func.hh:336
A WebAssembly global.
Definition: global.hh:28
A WebAssembly instance.
Definition: instance.hh:31
Helper class for linking modules together with name-based resolution.
Definition: linker.hh:25
A WebAssembly linear memory.
Definition: memory.hh:27
Fallible result type used for Wasmtime.
Definition: error.hh:82
An interior pointer into a Store.
Definition: store.hh:68
void gc()
Definition: store.hh:96
void set_epoch_deadline(uint64_t ticks_beyond_current)
Definition: store.hh:157
Result< std::monostate > set_wasi(WasiConfig config)
Definition: store.hh:142
Context(wasmtime_context_t *ptr)
Creates a context from the raw C API pointer.
Definition: store.hh:83
Result< std::monostate > set_fuel(uint64_t fuel)
Definition: store.hh:104
Context(Store &store)
Creates a context referencing the provided Store.
Definition: store.hh:86
void set_data(std::any data) const
Set user specified data associated with this store.
Definition: store.hh:125
Result< uint64_t > get_fuel() const
Definition: store.hh:115
std::any & get_data() const
Get user specified data associated with this store.
Definition: store.hh:132
Context(Store *store)
Creates a context referencing the provided Store.
Definition: store.hh:88
const wasmtime_context_t * capi() const
Returns the underlying C API pointer.
Definition: store.hh:162
wasmtime_context_t * capi()
Returns the underlying C API pointer.
Definition: store.hh:165
Owner of all WebAssembly objects.
Definition: store.hh:41
wasmtime_store_t * capi()
Returns the underlying C API pointer.
Definition: store.hh:238
void epoch_deadline_callback(F &&f)
Configures epoch deadline callback to C function.
Definition: store.hh:219
Store(Engine &engine)
Creates a new Store within the provided Engine.
Definition: store.hh:54
Context context()
Explicit function to acquire a Context from this store.
Definition: store.hh:228
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.hh:198
void gc()
Definition: store.hh:232
const wasmtime_store_t * capi() const
Returns the underlying C API pointer.
Definition: store.hh:235
A WebAssembly table.
Definition: table.hh:31
Representation of a generic WebAssembly value.
Definition: val.hh:204
Configuration for an instance of WASI.
Definition: wasi.hh:23
Build-time defines for how the C API was built.
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....
void wasmtime_context_set_data(wasmtime_context_t *context, void *data)
Overwrites the user-specified data associated with this store.
uint8_t wasmtime_update_deadline_kind_t
An enum for the behavior before extending the epoch deadline.
Definition: store.h:212
#define WASMTIME_UPDATE_DEADLINE_CONTINUE
Directly continue to updating the deadline and executing WebAssembly.
Definition: store.h:214
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.
void wasmtime_context_gc(wasmtime_context_t *context)
Perform garbage collection within the given context.
void wasmtime_store_delete(wasmtime_store_t *store)
Deletes a store.
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...
wasmtime_context_t * wasmtime_store_context(wasmtime_store_t *store)
Returns the interior wasmtime_context_t pointer to this store.
void * wasmtime_context_get_data(const wasmtime_context_t *context)
Returns the user-specified data associated with the specified store.
wasmtime_store_t * wasmtime_store_new(wasm_engine_t *engine, void *data, void(*finalizer)(void *))
Creates a new store within the specified engine.
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.
wasmtime_error_t * wasmtime_context_set_wasi(wasmtime_context_t *context, wasi_config_t *wasi)
Configures WASI state within the specified store.
#define WASMTIME_UPDATE_DEADLINE_YIELD
Yield control (via async support) then update the deadline.
Definition: store.h:216
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.
DeadlineKind
An enum for the behavior before extending the epoch deadline.
Definition: store.hh:22
@ Yield
Yield control (via async support) then update the deadline.
@ Continue
Directly continue to updating the deadline and executing WebAssembly.
An interior pointer into a wasmtime_store_t which is used as "context" for many functions.
Errors generated by Wasmtime.
Storage of WebAssembly objects.